2

You get the notification info in the code if you tap the notification from notification center, but what if user won't tap this, and will just tap the app icon on the home screen?

For example because the notification payload contained also badge property. Then user can tap the app icon, because there is badge number shining, but how do you manage the waiting notifications in the code in this case?

If there is no method for this, then the badge property in notification payload is bit useless, isn't it. Because if user taps the icon with badge number he expects that something will happen, but app is unable to do anything with pending notifications, thus to do anything. Or?

luky
  • 2,263
  • 3
  • 22
  • 40
  • Possible duplicate of [Push notification data not getting when app launched directly by clicking app icon](https://stackoverflow.com/questions/33471064/push-notification-data-not-getting-when-app-launched-directly-by-clicking-app-ic) – Bastian May 24 '17 at 14:25

1 Answers1

6

It seems that getDeliveredNotifications method allows this.

- (void)applicationWillEnterForeground:(UIApplication *)application {
    NSLog(@"msg applicationWillEnterForeground");
#if defined(__IPHONE_10_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0
    // https://stackoverflow.com/a/52840551
    [[UNUserNotificationCenter currentNotificationCenter] getDeliveredNotificationsWithCompletionHandler:^(NSArray<UNNotification *> * _Nonnull notifications) {
        NSLog(@"msg getDeliveredNotificationsWithCompletionHandler count %lu", [notifications count]);
        
        for (UNNotification* notification in notifications) {
            // do something with object
            NSLog(@"msg noti %@", notification.request);
        }
        
    }];
#endif
}
miken32
  • 42,008
  • 16
  • 111
  • 154
luky
  • 2,263
  • 3
  • 22
  • 40