简体   繁体   English

如何从Apple Push Notifications获取userInfo

[英]How to get userInfo from Apple Push Notifications

My enterprise app is using Apple Push Notifications, and it works fine under most circumstances. 我的企业应用程序正在使用Apple Push Notifications,并且在大多数情况下都可以正常工作。 What I would like to do is get the user information from a notification in a method other than the userDidRespondToPush . 我想做的是使用userDidRespondToPush之外的方法从通知中获取用户信息。

If there is a notification waiting, the system puts a badge on the icon on the iPhone's home screen. 如果有通知等待,系统会在iPhone主屏幕上的图标上放置徽章。 If a user responds to the notification itself, the app will trigger userRespondsToPush . 如果用户自己响应通知,则该应用将触发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. 这意味着我将需要使用userDidRespondToPush以外的方法获取userInfo ,以便应用程序知道向用户显示哪些信息。

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. 我只需要这一小功能即可工作。

If your application supports iOS 10 and above, then you should be able to retrieve pending notifications using the UNUserNotificationCenter class. 如果您的应用程序支持iOS 10及更高版本,则您应该能够使用UNUserNotificationCenter类检索待处理的通知。

- (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;
}

I guess you want to know the app launched from RemoteNotifiication Or started normally from app icon. 我想您想知道从RemoteNotifiication启动的应用程序,或者通常是从应用程序图标启动的应用程序。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    NSDictionary *remoteNotif = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];

    return YES;
}

if remote Notification exists, means app started from RemoteNotification, otherwise NO. 如果存在远程通知,则表示应用是从RemoteNotification启动的,否则为否。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM