didReceiveRemoteNotification not being called when I tap on app icon after receiving a push notification while on background
Asked Answered
P

4

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?

Pinelli answered 14/2, 2014 at 10:19 Comment(3)
were you able to solve this problem?Astrid
are you solved this issue?Kirsch
@MarioFrade I updated proxi answer with new document, and your problem is the expected behaviourMythopoeia
A
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
Aft answered 18/2, 2014 at 8:6 Comment(4)
didFinishLaunchingWithOptions doesnt gets called when app was in background and was not killed and I use icon to launch the appRicercar
didReceiveRemoteNotification: should be invoked in this caseAft
hi , @Ricercar , 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 ?Gauguin
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 implementedAft
D
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.

Derryberry answered 4/7, 2014 at 17:39 Comment(3)
and in didFinishLaunchingWithOptions: you can use this code for get push's payload: <code> NSDictionary* userInfo = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey]; </code>Hircine
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 ?Gauguin
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 . \Gauguin
H
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
Hircine answered 29/8, 2015 at 9:52 Comment(0)
V
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 ;)

Veriee answered 1/3, 2014 at 16:42 Comment(2)
I have similar problem. This did not solve the issue.Astrid
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.Isocrates

© 2022 - 2024 — McMap. All rights reserved.