如何从Apple推送通知中获取userInfo

时间:2018-04-19 20:21:21

标签: ios objective-c apple-push-notifications

我的企业应用正在使用Apple推送通知,并且在大多数情况下都能正常运行。我想要做的是通过userDidRespondToPush以外的方法从通知中获取用户信息。

如果有通知等待,系统会在iPhone主屏幕上的图标上放置徽章。如果用户响应通知本身,该应用将触发userRespondsToPush。但是,如果用户没有正常滑动通知并正常启动应用,只需点击带有徽章的应用图标,我就会让应用响应,就像用户刷了一样通知。这意味着我需要在userInfo以外的方法中获取userDidRespondToPush,以便应用知道向用户显示哪些信息。

推送通知对我来说是一个新手,但是我已经有一些运气好运了。我只需要这个小功能就可以了。

2 个答案:

答案 0 :(得分:0)

如果您的应用程序支持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;
}

答案 1 :(得分:0)

我想您想知道从RemoteNotifiication启动的应用程序,或者从应用程序图标正常启动。

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

    NSDictionary *remoteNotif = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];

    return YES;
}

如果存在远程通知,则表示应用程序从RemoteNotification启动,否则为NO。

相关问题