[英]Get pending notifications when app enters foreground
如果您从通知中心点击通知,您会在代码中获得通知信息,但如果用户不点击此通知,而只会点击主屏幕上的应用程序图标怎么办?
例如,因为通知有效负载还包含徽章属性。 然后用户可以点击应用程序图标,因为有徽章编号闪烁,但是在这种情况下如何管理代码中的等待通知?
如果没有办法解决这个问题,那么通知负载中的 badge 属性就有点没用了,不是吗。 因为如果用户点击带有徽章编号的图标,他预计会发生某些事情,但应用程序无法对待处理的通知执行任何操作,因此无法执行任何操作。 或者?
似乎 getDeliveredNotifications 方法允许这样做。
- (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
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.