简体   繁体   English

推送通知在iOS 8上停止工作

[英]Push notifications stopped working on iOS 8

My iOS app stopped receiving push notifications although I upgraded the code as per the documentations and this. 我的iOS应用程序停止接收推送通知,尽管我已按照文档及相关内容升级了代码

Here's the code I'm using: 这是我正在使用的代码:

In my AppDelegate's didFinishLaunchingWithOptions : 在我的AppDelegate's didFinishLaunchingWithOptions

if ([[UIApplication sharedApplication] respondsToSelector:@selector(registerUserNotificationSettings:)]) {

    UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert |
     UIUserNotificationTypeBadge | UIUserNotificationTypeSound  categories:nil];
    [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
    [[UIApplication sharedApplication] registerForRemoteNotifications];
}
else {
    [[UIApplication sharedApplication] registerForRemoteNotificationTypes:UIRemoteNotificationTypeAlert |
     UIRemoteNotificationTypeBadge |
     UIRemoteNotificationTypeSound];
}

The didRegisterForRemoteNotificationsWithDeviceToken method is getting called, as it was before, so everything seems fine. didRegisterForRemoteNotificationsWithDeviceToken方法被调用,因此一切似乎都很好。

Also, my test device has the notifications enabled. 另外,我的测试设备已启用通知。

But when sending a push from Parse.com it no longer arrives. 但是,当从Parse.com发送推送时,该推送将不再到达。

EDIT 1: 编辑1:

None of the answers work. 没有答案有效。 I updated my Parse.com framework to version 1.6.2 (the latest) which doesn't help either, and I'm copying my code again - this time the updated version based on the answers: 我将Parse.com框架更新为版本1.6.2(最新版本),该版本也无济于事,并且我再次复制了代码-这次基于答案的更新版本:

Inside didFinishLaunchingWithOptions : didFinishLaunchingWithOptions内部:

if ([application respondsToSelector:@selector(registerUserNotificationSettings:)]) {
    UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:(UIRemoteNotificationTypeBadge
                                                                                         |UIRemoteNotificationTypeSound |UIRemoteNotificationTypeAlert) categories:nil];
    [application registerUserNotificationSettings:settings];
    //        [application registerForRemoteNotifications];
} else {
    UIRemoteNotificationType myTypes = UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound;
    [application registerForRemoteNotificationTypes:myTypes];
}

And these are the delegate methods: 这些是委托方法:

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)newDeviceToken {
    NSLog(@"didRegisterForRemoteNotificationsWithDeviceToken CALLED");

    // Store the deviceToken in the current installation and save it to Parse.
    PFInstallation *currentInstallation = [PFInstallation currentInstallation];
    [currentInstallation setDeviceTokenFromData:newDeviceToken];
    [currentInstallation addUniqueObject:@"Test8Channel" forKey:@"channels"];

    if([PFUser currentUser]/* && [[PFUser currentUser] objectId] != nil*/) {
        [currentInstallation addUniqueObject:[PFUser currentUser] forKey:kOwnerKey];
    }
    [currentInstallation saveInBackground];

}

#ifdef IS_OS_8_OR_LATER
- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings {
    [application registerForRemoteNotifications];
    NSLog(@"didRegisterUserNotificationSettings CALLED");
}

- (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString*)identifier forRemoteNotification:(NSDictionary *)userInfo completionHandler:(void(^)())completionHandler {
NSLog(@"handleActionWithIdentifier CALLED");
//handle the actions
if ([identifier isEqualToString:@"declineAction"]){
    NSLog(@"handleActionWithIdentifier %@", @"declineAction");
}
else if ([identifier isEqualToString:@"answerAction"]){
    NSLog(@"handleActionWithIdentifier %@", @"answerAction");
}
}
#endif


- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
    if (error.code == 3010) {
        NSLog(@"Push notifications are not supported in the iOS Simulator.");
    } else {
        // show some alert or otherwise handle the failure to register.
        NSLog(@"application:didFailToRegisterForRemoteNotificationsWithError: %@", error);
    }
}

Both didRegisterUserNotificationSettings and didRegisterForRemoteNotificationsWithDeviceToken are getting called and it seems fine. didRegisterUserNotificationSettingsdidRegisterForRemoteNotificationsWithDeviceToken都被调用了,看起来还不错。 But the push doesn't arrive. 但是没有到位。

EDIT 2: 编辑2:

I'm noticing that if I call both 我注意到如果我同时打电话给我

[application registerUserNotificationSettings:settings];

and

[application registerForRemoteNotifications];

inside the if ([application respondsToSelector:@selector(registerUserNotificationSettings:)]) { if ([application respondsToSelector:@selector(registerUserNotificationSettings:)]) {

the delegate's didRegisterForRemoteNotificationsWithDeviceToken is getting called twice. 委托的didRegisterForRemoteNotificationsWithDeviceToken被调用两次。 I'm not sure how meaningful this is. 我不确定这有多有意义。

Getting desperate here. 在这里变得绝望。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    [self enableNotifications:application];
    return YES;
}


#define iOS8_OR_NEWER ( [ [ [ UIDevice currentDevice ] systemVersion ] floatValue ] >= 8.0 )

- (void)enableNotifications:(UIApplication *)application
{
    if (iOS8_OR_NEWER) {
        UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert) categories:nil];
        [application registerUserNotificationSettings:settings];
        [application registerForRemoteNotifications];
    } else {
        [application registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];
    }
}

#ifdef __IPHONE_8_0
- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings
{
    [application registerForRemoteNotifications];
}

- (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forRemoteNotification:(NSDictionary *)userInfo completionHandler:(void(^)())completionHandler
{
    // https://nrj.io/simple-interactive-notifications-in-ios-8
}
#endif

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
    NSLog(@"Push Token : %@", deviceToken);
}

- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error
{
    NSLog(@"Failed to get token, error: %@", error);
}

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
    NSLog(@"didReceiveRemoteNotification : %@", userInfo);
}
    //-- Set Notification
    if ([application respondsToSelector:@selector(isRegisteredForRemoteNotifications)])
    {
        // iOS 8 Notifications
        [application  registerUserNotificationSettings:[UIUserNotificationSettings  settingsForTypes:(UIUserNotificationTypeSound |  UIUserNotificationTypeAlert | UIUserNotificationTypeBadge)  categories:nil]];
        [application registerForRemoteNotifications];
    }
    else
    {
        // iOS < 8 Notifications
        [application registerForRemoteNotificationTypes:
         (UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound)];
    }

Use this simple conditional block for handle it. 使用此简单的条件块进行处理。 because some methods/classes are updated with version 因为某些方法/类随版本更新

if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
{
    //ios8
    if ([[UIApplication sharedApplication] respondsToSelector:@selector(registerUserNotificationSettings:)])
    {

        UIUserNotificationSettings* notificationSettings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound categories:nil];

        [[UIApplication sharedApplication] registerUserNotificationSettings:notificationSettings];

        .......
    }
}
else
{
    // ios7
    if ([[UIApplication sharedApplication] respondsToSelector:@selector(registerForRemoteNotificationTypes:)])
    {
        [[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound)];
      .........
    }
}

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

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