如何在ios目标c中应用处于活动状态时获取推送通知?

时间:2022-09-02 10:54:41

I am working on an app in which I have implemented apple push notification. When my app is in background state then I am able to receive push notifications but when my app is in active state then I am not able to get any push notifications, can anypne help on this?

我正在开发一个应用程序,我已经实现了苹果推送通知。当我的应用程序处于后台状态时,我能够接收推送通知但是当我的应用程序处于活动状态时,我无法获得任何推送通知,可以对此进行任何帮助吗?

3 个解决方案

#1


1  

Try to check inside didReceiveRemoteNotification

尝试检查一下didReceiveRemoteNotification

 - (void)application:(UIApplication *)application 
    didReceiveRemoteNotification:(NSDictionary *)userInfo 
    fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))completionHandler

delegate method to receive remote notification in all state.

委托方法在所有状态下接收远程通知。

If you want to show the notification alert in active state. use HDNotificationView to show notification alert.

如果要在活动状态下显示通知警报。使用HDNotificationView显示通知警报。

like this:

喜欢这个:

if(application.applicationState == UIApplicationStateActive) {

[HDNotificationView showNotificationViewWithImage:[UIImage imageNamed:@"Icon-40.png"]
                                                            title:title
                                                          message:message
                                                       isAutoHide:YES
                                                          onTouch:^{
                                                              /// On touch handle. You can hide notification view or do something
                                                              [HDNotificationView hideNotificationViewOnComplete:nil];
                                                          }];
}

#2


0  

Add an UIAlertView inside didReceiveRemoteNotification and check if you are receiving notification in active state. Check below code

在didReceiveRemoteNotification中添加UIAlertView并检查您是否正在接收处于活动状态的通知。检查以下代码

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo  {

        if (application.applicationState == UIApplicationStateActive) {                              
            UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Received a Notification" message:[NSString stringWithFormat:@"My App received  notification while it is running:\n%@",[[userInfo objectForKey:@"aps"] objectForKey:@"alert"]]delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
            [alertView show];          
        }    
    }

#3


0  

When Push Notification received to your app Application can be one the below 5 states Reference:: https://developer.apple.com/library/content/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/TheAppLifeCycle/TheAppLifeCycle.html

当您的应用程序收到推送通知应用程序可以是以下5个状态之一参考:: https://developer.apple.com/library/content/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/TheAppLifeCycle/TheAppLifeCycle.html

1)Not running

1)没有运行

2)Inactive

2)非活动

3)Background

3)背景

4)Suspended

4)悬

5)Active

5)活动

If application state is 1,2,3,4 then iOS will handle and will display banner In case of 5(Active state) our app needs to handle this case In this case below method will be called

如果应用程序状态是1,2,3,4那么iOS将处理并将显示banner如果是5(活动状态)我们的应用程序需要处理这种情况在这种情况下,将调用下面的方法

Reference:: https://developer.apple.com/documentation/uikit/uiapplicationdelegate/1623013-application?language=objc

参考:: https://developer.apple.com/documentation/uikit/uiapplicationdelegate/1623013-application?language=objc

- (void)application:(UIApplication *)application 
didReceiveRemoteNotification:(NSDictionary *)userInfo 
fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))completionHandler;

here you need to check state of app using application.state == Active then you can do what ever you want ideal case here we need to display alert .

在这里你需要使用application.state == Active来检查应用程序的状态然后你可以做你想要的理想情况,我们需要显示警报。

#1


1  

Try to check inside didReceiveRemoteNotification

尝试检查一下didReceiveRemoteNotification

 - (void)application:(UIApplication *)application 
    didReceiveRemoteNotification:(NSDictionary *)userInfo 
    fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))completionHandler

delegate method to receive remote notification in all state.

委托方法在所有状态下接收远程通知。

If you want to show the notification alert in active state. use HDNotificationView to show notification alert.

如果要在活动状态下显示通知警报。使用HDNotificationView显示通知警报。

like this:

喜欢这个:

if(application.applicationState == UIApplicationStateActive) {

[HDNotificationView showNotificationViewWithImage:[UIImage imageNamed:@"Icon-40.png"]
                                                            title:title
                                                          message:message
                                                       isAutoHide:YES
                                                          onTouch:^{
                                                              /// On touch handle. You can hide notification view or do something
                                                              [HDNotificationView hideNotificationViewOnComplete:nil];
                                                          }];
}

#2


0  

Add an UIAlertView inside didReceiveRemoteNotification and check if you are receiving notification in active state. Check below code

在didReceiveRemoteNotification中添加UIAlertView并检查您是否正在接收处于活动状态的通知。检查以下代码

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo  {

        if (application.applicationState == UIApplicationStateActive) {                              
            UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Received a Notification" message:[NSString stringWithFormat:@"My App received  notification while it is running:\n%@",[[userInfo objectForKey:@"aps"] objectForKey:@"alert"]]delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
            [alertView show];          
        }    
    }

#3


0  

When Push Notification received to your app Application can be one the below 5 states Reference:: https://developer.apple.com/library/content/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/TheAppLifeCycle/TheAppLifeCycle.html

当您的应用程序收到推送通知应用程序可以是以下5个状态之一参考:: https://developer.apple.com/library/content/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/TheAppLifeCycle/TheAppLifeCycle.html

1)Not running

1)没有运行

2)Inactive

2)非活动

3)Background

3)背景

4)Suspended

4)悬

5)Active

5)活动

If application state is 1,2,3,4 then iOS will handle and will display banner In case of 5(Active state) our app needs to handle this case In this case below method will be called

如果应用程序状态是1,2,3,4那么iOS将处理并将显示banner如果是5(活动状态)我们的应用程序需要处理这种情况在这种情况下,将调用下面的方法

Reference:: https://developer.apple.com/documentation/uikit/uiapplicationdelegate/1623013-application?language=objc

参考:: https://developer.apple.com/documentation/uikit/uiapplicationdelegate/1623013-application?language=objc

- (void)application:(UIApplication *)application 
didReceiveRemoteNotification:(NSDictionary *)userInfo 
fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))completionHandler;

here you need to check state of app using application.state == Active then you can do what ever you want ideal case here we need to display alert .

在这里你需要使用application.state == Active来检查应用程序的状态然后你可以做你想要的理想情况,我们需要显示警报。