didReceiveRemoteNotification not being called when

2019-03-18 13:19发布

问题:

When my app is on background and I receive a remote notification, two things can happen:

  1. I tap on the push notification banner, my apps comes to foreground and didReceiveRemoteNotification is called.

  2. I tap on my app icon from the springboard, my app comes to foreground and didReceiveRemoteNotification IS NOT called.

So, in the scenario 1, I can update my counter of unread messages inside the app in response to didReceiveRemoteNotification. In the scenario 2, I can't.

How can I solve this using Quickblox?

回答1:

As one possible variant:

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    NSDictionary *userInfo = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
    if (userInfo) {
       [self handleRemoteNotifications:userInfo];
    }

    // Override point for customization after application launch.
    return YES;
}

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
   [self handleRemoteNotifications:userInfo];
} 

#pragma mark - Remote notifications handling

 -(void)handleRemoteNotifications:(NSDictionary *)userInfo {
   // do your stuff
}

@end


回答2:

When app is not running, in didFinishLaunchingWithOptions: you can use this code for get push's payload:

   - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{    
    NSDictionary* userInfo = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey]; 

NSString *myKey = [userInfo objectForKey:@"myKeyFromPayload"];
    }

Remember to set permission in plist

For the remote push you can use in your appdelegate:

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


回答3:

The issue is probably that application:didReceiveRemoteNotification: is not called if the app is not running. To quote Apple documentation:

This document is outdated

If the app is not running when a push notification arrives, the method launches the app and provides the appropriate information in the launch options dictionary. The app does not call this method to handle that push notification. Instead, your implementation of the application:willFinishLaunchingWithOptions: or application:didFinishLaunchingWithOptions: method needs to get the push notification payload data and respond appropriately.

This is the new document

Use this method to process incoming remote notifications for your app. Unlike the application:didReceiveRemoteNotification: method, which is called only when your app is running in the foreground, the system calls this method when your app is running in the foreground or background. In addition, if you enabled the remote notifications background mode, the system launches your app (or wakes it from the suspended state) and puts it in the background state when a remote notification arrives. However, the system does not automatically launch your app if the user has force-quit it. In that situation, the user must relaunch your app or restart the device before the system attempts to launch your app automatically again.



回答4:

You have to enable Remote Notifications in the Background Modes.

To do so, automatically: (Xcode5)

- Go to your Project settings -> Capabilities -> Background Modes
- Tick "Remote Notifications"

To do so, manually:

- Open your %appname%-Info.plist
- Right click and tick "Show Raw Keys/Values"
- Right click and choose "Add Row"
- Type in "UIBackgroundModes" (Key)
- The key will be created, and the type is an Array
- Add new item in the array with the value of "remote-notification" (Value) and press enter
- Now you have 1 item in your array called: "Item 0", if you had any other items in there, just add this item (remote-notification) to the array.

Be sure to use these methods frankWhite used :)

Hope this helps ;)