[objective-c]iOS︰ 推送通知不显示字典信息在 iOS 10

发布时间: 2016/10/4 1:19:53
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

我正在在 XCode 8 Beta,iOS 的推送通知 10 版本。推式通知收到设备上有了当我点击通知时,发射了 UNUserNotificationCenterDelegate 委托,打开应用程序,但它不在用户信息中显示任何响应。我需要更改参数在 iOS 10 服务器端发送推送。下面是我的代码。

    UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound categories:nil];
    [application registerUserNotificationSettings:settings];

    UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
    center.delegate = self;
    [center requestAuthorizationWithOptions:(UNAuthorizationOptionBadge | UNAuthorizationOptionSound | UNAuthorizationOptionAlert) completionHandler:^(BOOL granted, NSError * _Nullable error) {
        if (!error) {
            NSLog(@"request authorization succeeded!");
        }
    }];


- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler {

    NSLog(@"Notification is triggered");
    completionHandler(UNNotificationPresentationOptionAlert);
}

- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)())completionHandler {

    NSLog(@"Tapped in notification");
    NSLog(@"%@",response.notification);
    NSString *actionIdentifier = response.actionIdentifier;

    if ([actionIdentifier isEqualToString:@"com.apple.UNNotificationDefaultActionIdentifier"] ||
        [actionIdentifier isEqualToString:@"com.apple.UNNotificationDismissActionIdentifier"]) {
        return;
    }
}

在 iOS 9,

aps =     {
    alert = "Multi extra param push.";
    badge = 0;
    sound = default;
};
t = I;
url = "http://www.google.com";

在 iOS 10

alert = "Multi extra param push.";
badge = 0;
sound = default;

解决方法 1:

- (void)userNotificationCenter:(UNUserNotificationCenter *)center
       willPresentNotification:(UNNotification *)notification
         withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler {
    NSLog(@"%@", notification.request.content.userInfo);
}

- (void)userNotificationCenter:(UNUserNotificationCenter *)center
didReceiveNotificationResponse:(UNNotificationResponse *)response
         withCompletionHandler:(void(^)())completionHandler {
    NSLog(@"%@", response.notification.request.content.userInfo);
}
官方微信
官方QQ群
31647020