Push Notification not working in iOS 8 using Parse.com

i have a code that works in iOS 7, i receive all the Push Notifications.

When implementing the new iOS 8 Push Notification using Parse.com, i can't make it work.

Here is the code:

    - (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

Is there anything i`m doing wrong? The payload is correct, bacause its working on iOS 7. And the category is set.

Please me let me know!

Upvotes: 0

Views: 1573

Answers (1)

brbgyn
brbgyn

Reputation: 421

The code for iOS 8 has Changed:

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

Upvotes: 3

Related Questions