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

luk*_*uky 2 objective-c ios

如果您点击通知中心的通知,您会在代码中获得通知信息,但是如果用户不点击它,而只是点击主屏幕上的应用程序图标怎么办?

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

如果没有这方面的方法,那么通知负载中的徽章属性就有点无用了,不是吗。因为如果用户点击带有徽章编号的图标,他预计会发生一些事情,但是应用程序无法对挂起的通知做任何事情,因此可以做任何事情。或者?

luk*_*uky 6

似乎 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
}
Run Code Online (Sandbox Code Playgroud)