【问题标题】:Get push id when app is in background state iOS当应用程序处于后台状态iOS时获取推送ID
【发布时间】:2022-09-23 03:42:43
【问题描述】:
我需要存储远程进入的推送的标识符。如果应用程序在前台,将触发 willPresentNotification 方法,我可以轻松地从 notification.request.identifier 中获取推送 ID。但是,当推送到后台状态的应用程序时,didReceiveRemoteNotification 会触发,但它没有给我这个推送 ID。求助,在哪里可以得到特定推送的特定ID?
标签:
ios
swift
objective-c
push-notification
push
【解决方案1】:
如此处所述:https://developer.apple.com/documentation/usernotifications/unusernotificationcenter/1649520-getdeliverednotifications
,UNUserNotificationCenter 类提供了一个方法,getDeliveredNotificationsWithCompletionHandler:, 以获取通知中心中的所有应用通知。
当您的应用程序在后台时到达并显示的通知也在那里,并且它是该数组中的第一个对象,因为它收到了最顶部的通知。
原谅objective-c代码,但这里有一个例子:
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))completionHandler{
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
[center getDeliveredNotificationsWithCompletionHandler:^(NSArray *notifications){
UNNotification *lastReceivedNotification = [notifications firstObject];
NSString *identifier = [[lastReceivedNotification request] identifier];
// do something
}];
//...
}