推送通知使用Parse.com在iOS 8中无效

时间:2014-09-15 19:23:47

标签: parse-platform push ios8

我有一个适用于iOS 7的代码,我会收到所有推送通知。

使用Parse.com实施新的iOS 8推送通知时,我无法使其正常工作。

以下是代码:

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions     
    // Register for push notifications
    [Parse setApplicationId:@"XXXX" clientKey:@"XXX"]; // REMOVED IDS FOR SECURITY REAS

    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0) {

        UIMutableUserNotificationAction *viewAction = [[UIMutableUserNotificationAction alloc] init];
        viewAction.identifier = @"medphone-view";
        viewAction.title = @"Ver";
        viewAction.activationMode = UIUserNotificationActivationModeForeground;
        viewAction.destructive = NO;

        UIMutableUserNotificationAction *dismissAction = [[UIMutableUserNotificationAction alloc] init];
        dismissAction.identifier = @"medphone-dismiss";
        dismissAction.title = @"Excluir";
        dismissAction.activationMode = UIUserNotificationActivationModeBackground;
        dismissAction.destructive = YES;

        UIMutableUserNotificationCategory *category = [[UIMutableUserNotificationCategory alloc] init];
        category.identifier = @"medphone";
        [category setActions:[NSArray arrayWithObjects:viewAction, dismissAction, nil] forContext:UIUserNotificationActionContextDefault];

        NSSet *categories = [NSSet setWithObjects:category, nil];

        UIUserNotificationType types = UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert;
        UIUserNotificationSettings *mySettings = [UIUserNotificationSettings settingsForTypes:types categories:categories];
        [application registerUserNotificationSettings:mySettings];
        [application registerForRemoteNotifications];

    } else {
        [application registerForRemoteNotificationTypes:
         UIRemoteNotificationTypeBadge |
         UIRemoteNotificationTypeAlert |
         UIRemoteNotificationTypeSound];
    }

    if (launchOptions) { //launchOptions is not nil
        NSDictionary *userInfo = [launchOptions valueForKey:@"UIApplicationLaunchOptionsRemoteNotificationKey"];
        NSLog(@"Push info %@", userInfo);
        NSDictionary *apsInfo = [userInfo objectForKey:@"aps"];

        if (apsInfo) { //apsInfo is not nil
            NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
            [prefs setValue:userInfo forKey:@"PUSHDATA"];
            [prefs setBool:YES forKey:@"PUSH"];
            [prefs synchronize];
            NSLog(@"entrou no UIApplicationLaunchOptionsRemoteNotificationKey %@", apsInfo);
        }
    }

    return YES;
}

And these other methods:

    - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
    PFInstallation *currentInstallation = [PFInstallation currentInstallation];
    [currentInstallation setDeviceTokenFromData:deviceToken];
    currentInstallation.channels = @[@"global"];
    [currentInstallation saveInBackground];
}

    - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
    [[NSNotificationCenter defaultCenter] postNotificationName:@"pushNotification" object:userInfo];
    NSLog(@"entrou no didReceiveRemoteNotification %@", userInfo);
}

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

    - (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forRemoteNotification:(NSDictionary *)userInfo completionHandler:(void(^)())completionHandler {
    NSLog(@"entrou no UIApplicationLaunchOptionsRemoteNotificationKey %@", userInfo);

    //handle the actions
    if ([identifier isEqualToString:@"medphone-view"]) {
        NSLog(@"ver");
    } else if ([identifier isEqualToString:@"medphone-dismiss"]) {
        NSLog(@"dismmis");
    }
    completionHandler();
}



#endif

有什么我做错了吗?有效负载是正确的,因为它在iOS 7上工作。并且类别已设置。

请告诉我!

1 个答案:

答案 0 :(得分:3)

iOS 8的代码已更改:

if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
{ [[UIApplication sharedApplication] registerUserNotificationSettings:
   [UIUserNotificationSettings settingsForTypes:
                  (UIUserNotificationTypeSound | 
                   UIUserNotificationTypeAlert | 
                    UIUserNotificationTypeBadge) categories:nil]];

    [[UIApplication sharedApplication] registerForRemoteNotifications];
}
else
{
    [[UIApplication sharedApplication] registerForRemoteNotificationTypes:
     (UIUserNotificationTypeBadge | 
      UIUserNotificationTypeSound | 
      UIUserNotificationTypeAlert)];
}