如何从 Apple 推送通知中获取用户信息 [英] How to get userInfo from Apple Push Notifications
问题描述
我的企业应用程序正在使用 Apple 推送通知,并且在大多数情况下都可以正常工作.我想要做的是从 userDidRespondToPush
以外的方法中的通知中获取用户信息.
My enterprise app is using Apple Push Notifications, and it works fine under most circumstances. What I would like to do is get the user information from a notification in a method other than the userDidRespondToPush
.
如果有通知等待,系统会在 iPhone 主屏幕的图标上放置一个徽章.如果用户响应通知本身,应用程序将触发 userRespondsToPush
.但是如果用户没有滑动通知并正常启动应用程序,通过点击带有徽章的应用程序图标,我希望应用程序响应就像用户滑动通知一样.这意味着我需要在 userDidRespondToPush
以外的方法中获取 userInfo
以便应用程序知道向用户显示什么信息.
If there is a notification waiting, the system puts a badge on the icon on the iPhone's home screen. If a user responds to the notification itself, the app will trigger userRespondsToPush
. But if the user doesn't swipe the notification and starts the app normally, by tapping on the app's icon with a badge showing, I'd like to have the app respond as if the user did swipe the notification. That means I would need to get the userInfo
in a method other than userDidRespondToPush
so the app will know what information to show the user.
推送通知对我来说还很陌生,但我已经很幸运地让它工作了.我只需要这个小功能即可工作.
Push Notifications is pretty new to me, but I've had some pretty good luck getting it working. I just need this one little feature to work.
推荐答案
如果您的应用程序支持 iOS 10 及更高版本,那么您应该能够使用 UNUserNotificationCenter
类检索待处理的通知.
If your application supports iOS 10 and above, then you should be able to retrieve pending notifications using the UNUserNotificationCenter
class.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[[UNUserNotificationCenter currentNotificationCenter] getPendingNotificationRequestsWithCompletionHandler:^(NSArray<UNNotificationRequest *> * _Nonnull requests) {
for (UNNotificationRequest *request in requests) {
NSDictionary *userInfo = request.content.userInfo;
NSLog(@"push user info: %@", userInfo);
// Handle the payload here.
}
}];
return YES;
}
这篇关于如何从 Apple 推送通知中获取用户信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!