1

I am not sure if this is possible, but I need to grab all of the push notification userinfo when the user opens up the App. I can get all of the push notification userinfo when the App is opened or in the background, but not when the App is completely closed. Any way around this? The code below is how I get the userInfo currently.

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

    id data = [userInfo objectForKey:@"data"];

       NSLog(@"data%@",data);

   }
user3606054
  • 591
  • 1
  • 5
  • 14

4 Answers4

1

The method you are implementing cannot handle both cases. See the "Local and Push Notification Programming Guide":

If your app is frontmost, the application:didReceiveRemoteNotification: or application:didReceiveLocalNotification: method is called on its app delegate. If your app is not frontmost or not running, you handle the notifications by checking the options dictionary passed to the application:didFinishLaunchingWithOptions: of your app delegate...

Jonah
  • 17,918
  • 1
  • 43
  • 70
1

Unfortunately, it's not currently possible client side with that method to query old notifications that have occurred while the app was completely closed. See this question: didReceiveRemoteNotification when in background.

A way around it is to keep track of which notifications you send from your server per user. When didReceiveRemoteNotification: is called, you can take that notification and compare it against the server's messages for the current user. If one of them matches, mark it some way on the server. That way, if there are messages sent when your app is backgrounded, you can query for messages that haven't been marked from the server and get all 'missed' notifications.

Community
  • 1
  • 1
Dylan Gattey
  • 1,713
  • 13
  • 34
0
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    
//Notifications
    NSDictionary *userInfo = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
    if(userInfo){
        //open from notification message
    }
return YES;
}
Fadi Abuzant
  • 476
  • 8
  • 13
0

You can add this code to your AppDelegate's applicationWillEnterForeground method:

-(void)applicationWillEnterForeground:(UIApplication *)application {

    // this method is called when staring an app that was closed / killed / never run before (after applicationDidFinishLaunchingWithOptions) and every time the app is reopened or change status from background to foreground (ex. returning from a mobile call or after the user switched to other app and then came back)

    [[UNUserNotificationCenter currentNotificationCenter] getDeliveredNotificationsWithCompletionHandler:^(NSArray<UNNotification *> * _Nonnull notifications) {
        NSLog(@"AppDelegate-getDeliveredNotificationsWithCompletionHandler there were %lu notifications in notification center", (unsigned long)[notifications count]); 

        for (UNNotification* notification in notifications) {

            NSDictionary *userInfo = notification.request.content.userInfo;
            if (userInfo) {
                NSLog(@"Processed a notification in getDeliveredNotificationsWithCompletionHandler, with this info: %@", userInfo);
                [self showPushNotificationInAlertController:userInfo]; // this is my method to display the notification in an UIAlertController
            }
        }
        UIApplication.sharedApplication.applicationIconBadgeNumber = 0;

    }];
} 
}

Remove this line from the method application didFinishLaunchingWithOptions: if you had included it there, because it clears the badge number and also all notifications in notifications center:

UIApplication.sharedApplication.applicationIconBadgeNumber = 0;

This is currently working in iOS 12, hadn't had the chance to test it in earlier versions.

Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
alvaro
  • 2,676
  • 2
  • 20
  • 19