推送通知在iOS 8上停止运行

时间:2015-01-25 09:14:56

标签: ios parse-platform ios8 push-notification

虽然我按照文档和this.

升级了代码,但我的iOS应用停止接收推送通知

这是我正在使用的代码:

在我的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];
}

didRegisterForRemoteNotificationsWithDeviceToken方法被调用,就像之前一样,所以一切都很好。

此外,我的测试设备已启用通知。

但是当从Parse.com发送推送时,它不再到达。

编辑1:

没有一个答案有效。我将我的Parse.com框架更新到版本1.6.2(最新版本),这也没有帮助,我再次复制我的代码 - 这次是基于答案的更新版本:

内部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];
}

这些是委托方法:

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

didRegisterUserNotificationSettingsdidRegisterForRemoteNotificationsWithDeviceToken都被调用,看起来很好。但推动没有到来。

编辑2:

我注意到,如果我同时打电话

[application registerUserNotificationSettings:settings];

[application registerForRemoteNotifications];

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

代表的didRegisterForRemoteNotificationsWithDeviceToken被调用两次。我不确定这是多么有意义。

在这里绝望。

3 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

    //-- 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)];
    }

答案 2 :(得分:0)

使用这个简单的条件块来处理它。因为某些方法/类是使用版本

更新的
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)];
      .........
    }
}