11

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?

Augustine
  • 1,704
  • 1
  • 17
  • 21
Mario Frade
  • 273
  • 1
  • 2
  • 11

4 Answers4

3

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
frankWhite
  • 1,354
  • 12
  • 18
  • didFinishLaunchingWithOptions doesnt gets called when app was in background and was not killed and I use icon to launch the app – sheetal Oct 08 '15 at 19:44
  • didReceiveRemoteNotification: should be invoked in this case – frankWhite Oct 09 '15 at 09:00
  • hi , @sheetal , i have a similar scenario but slightly modification required , like when i get the push notification ,the app should get active in background and do the task given in , ( like user's location update code ) ,- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo , but my app would not get active or called until user taps on the notification . so , how would i get this done while app is in background ? dose anyone know the solution for this ? – Moxarth Nov 28 '17 at 06:58
  • Hello @Moxarth, sorry for late answer but I think it's better to create separated question topic because for example I don't know hot it's can be implemented – frankWhite Dec 10 '17 at 18:09
2

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.

onmyway133
  • 38,911
  • 23
  • 231
  • 237
proxi
  • 1,203
  • 10
  • 18
  • 2
    and in didFinishLaunchingWithOptions: you can use this code for get push's payload: NSDictionary* userInfo = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey]; – Alessio Campanelli Aug 29 '15 at 09:44
  • hi , i have a similar scenario but slightly modification required , like when i get the push notification ,the app should get active in background and do the task given in , ( like user's location update code ) ,- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo , but my app would not get active until user taps on the notification . so , how would i get this done while app is in background ? dose anyone know the solution for this ? – Moxarth Nov 28 '17 at 09:21
  • my never wakes up nor get activated when it got notification when app is in background state . we have to tap the notification in order to make it active . \ – Moxarth Nov 28 '17 at 09:22
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
Alessio Campanelli
  • 902
  • 10
  • 18
1

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 ;)

emotality
  • 11,710
  • 4
  • 34
  • 55
  • 4
    I have similar problem. This did not solve the issue. – Nameet Mar 04 '14 at 14:40
  • From docs: “Apps that use push notifications to notify the user that new content is available can fetch the content in the background. To support this mode, include the UIBackgroundModes key with the remote-notification value in your app’s Info.plist file. You must also implement the application:didReceiveRemoteNotification:fetchCompletionHandler: method in your app delegate.” So I don’t think this is relevant in general. – mxcl Nov 23 '14 at 18:29