当应用进入前台时获取待处理通知

时间:2017-05-24 14:23:17

标签: ios objective-c

如果您点按通知中心的通知,您会在代码中收到通知信息,但如果用户不点按此内容,只需点按主屏幕上的应用图标该怎么办?

例如,因为通知有效负载还包含徽章属性。然后用户可以点击应用程序图标,因为有徽章编号闪亮,但在这种情况下如何管理代码中的等待通知?

如果没有这方法,那么通知有效负载中的徽章属性就没用了,不是吗。因为如果用户点击带有徽章编号的图标,他预计会发生某些事情,但是应用程序无法对待处理的通知执行任何操作,因此可以执行任何操作。或?

1 个答案:

答案 0 :(得分:4)

似乎getDeliveredNotifications方法允许这样做。

- (void)applicationWillEnterForeground:(UIApplication *)application {
    NSLog(@"msg applicationWillEnterForeground");
#if defined(__IPHONE_10_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0
    [[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
}
相关问题