I have coded my app to receive Parse notifications in iOS8 and recently discovered these wouldn't work in iOS& so had to change my code to as follows in AppDelegate.m:
viewDidLoad:
if ([application respondsToSelector:@selector(registerUserNotificationSettings:)]) {
NSLog(@"Requesting permission for push notifications...iOS8"); // iOS 8
UIUserNotificationType userNotificationTypes = (UIUserNotificationTypeAlert |
UIUserNotificationTypeBadge |
UIUserNotificationTypeSound);
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:userNotificationTypes categories:nil];
[application registerUserNotificationSettings:settings];
[application registerForRemoteNotifications];
} else {
NSLog(@"Registering device for push notifications..."); // iOS 7 and earlier
[UIApplication.sharedApplication registerForRemoteNotificationTypes:
UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge |
UIRemoteNotificationTypeSound];
}
and:
- (void)application:(UIApplication *)application
didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
// Store the deviceToken in the current Installation and save it to Parse.
PFInstallation *currentInstallation = [PFInstallation currentInstallation];
[currentInstallation setDeviceTokenFromData:deviceToken];
[currentInstallation saveInBackground];
}
I tested on both version of iOS and it seems to be working fine. However if I change the deployment target in Xcode to iOS8 or above I get these compiler warnings:
AppDelegate.m:62:10: 'UIRemoteNotificationTypeAlert' is deprecated: first deprecated in iOS 8.0 - Use UIUserNotificationType for user notifications and registerForRemoteNotifications for receiving remote notifications instead.
AppDelegate.m:62:42: 'UIRemoteNotificationTypeBadge' is deprecated: first deprecated in iOS 8.0 - Use UIUserNotificationType for user notifications and registerForRemoteNotifications for receiving remote notifications instead.
AppDelegate.m:63:10: 'UIRemoteNotificationTypeSound' is deprecated: first deprecated in iOS 8.0 - Use UIUserNotificationType for user notifications and registerForRemoteNotifications for receiving remote notifications instead.
/AppDelegate.m:61:42: 'registerForRemoteNotificationTypes:' is deprecated: first deprecated in iOS 8.0 - Please use registerForRemoteNotifications and registerUserNotificationSettings: instead
Is it normal to see these warnings and is it just because I'm working in a higher deployment target? Or do I have to change the code somewhere? Seems odd that I would have to see these warnings. Any pointers would be really appreciated. Thanks!
If you changed the deployment target to iOS 8, so no need for checking for iOS 8 or not (Before iOS 8 as your app will work in iOS 8 devices only), thats why it gives you these warnings. So just register like this:
[application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];
Yes, That method is deprecated in iOS8 but you can check condition on iOS7 and iOS8 by these lines of code to manage deprecation:
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)];
}
and addition to that add these line of code below it.
if ([launchOptions valueForKey:@"UIApplicationLaunchOptionsRemoteNotificationKey"]) {
[self application:application didReceiveRemoteNotification:launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey]];
}
and to register device on parse.com please check once your applicationKey and ClientKey is correct other than that your code block for register device is correct, but it won't affect that deprecations of iOS at all.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With