Update Parse deviceToken AFTER registration - ios

Currently I'm sitting with quite a few empty deviceToken fields. I figured out this is because there was an issue with adding groups before a installation object was created - it would block the subsequent creation efforts.
What I'm trying to do now is get that deviceToken again and update it in Parse but the problem is, didRegisterForRemoteNotificationsWithDeviceToken is never run again after the first time...
Any way to get the device token AFTER the initial call to didRegisterForRemoteNotificationsWithDeviceToken?

This does work for iOS 8:
if ([application respondsToSelector:#selector(registerUserNotificationSettings:)]) {
UIUserNotificationSettings* notificationSettings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:notificationSettings];
[[UIApplication sharedApplication] registerForRemoteNotifications];
} else {
[[UIApplication sharedApplication] registerForRemoteNotificationTypes: (UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];
}

didRegisterForRemoteNotificationsWithDeviceToken can be called repeatedly. Simply call the push registration code in your AppDelegate.m, in the method didFinishLaunchingWithOptions.
Here's what I use:
//Check for iOS 7 vs 8
if ([application respondsToSelector:#selector(registerUserNotificationSettings:)]) {
UIUserNotificationSettings* notificationSettings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:notificationSettings];
[[UIApplication sharedApplication] registerForRemoteNotifications];
} else {
[[UIApplication sharedApplication] registerForRemoteNotificationTypes: (UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];
}
That's it - if you call this each time, you'll get a delegate callback to didRegisterForRemoteNotificationsWithDeviceToken on each load.

Related

UIUserNotificationSettings settingsForTypes: crashing iOS7 Devices

For some reason whenever I try and register for remote notifications on an iOS 7 device my app immediately crashes. I'm running the following block of code:
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_8_0
if ([[UIApplication sharedApplication] respondsToSelector:#selector(registerUserNotificationSettings:)]) {
UIUserNotificationSettings* notificationSettings = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound) categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:notificationSettings];
[[UIApplication sharedApplication] registerForRemoteNotifications];
} else {
[[UIApplication sharedApplication] registerForRemoteNotificationTypes: (UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];
}
#else
[[UIApplication sharedApplication] registerForRemoteNotificationTypes: (UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];
#endif
Which should allow me to register for notifications in both iOS 8 and 7 without a hitch (as a number of examples I've already found and looked at suggest) but for whatever reason it still crashes. I've narrowed the crash down to to the third line of code I posted--more specifically the following statement: [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound) categories:nil]; If I set it to nil the app runs perfectly fine, but if I leave the line in it just starts crashing.
I've tried putting a log inside the respondsToSelector if statement and the code is definitely not running on iOS 7 devices so I'm somewhat at a loss on what to do.
EDIT:
After a few more hours of troubleshooting I finally managed to find a fix. I needed to import UIKit.framework into my project under Linked Frameworks and Libraries and set it to optional.
I don't know about the #if, cause I've never used them, but I have a very similar code that's working, so you could try this:
if ([[UIApplication sharedApplication] respondsToSelector:#selector(registerUserNotificationSettings:)]) {
// use registerUserNotificationSettings
[[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];
[[UIApplication sharedApplication] registerForRemoteNotifications];
} else {
// use registerForRemoteNotificationTypes:
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound)];
}
If that doesn't work for you, perhaps something else is causing the crash.. ninja-style..

Using Remote notification with deployment target 8.1 and XCODE 6.1

I want to use remote notification with deployment target 8.1 and xcode 6.1
i am using following code
if ([application respondsToSelector:#selector(registerUserNotificationSettings:)]) {
UIUserNotificationSettings* notificationSettings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:notificationSettings];
[[UIApplication sharedApplication] registerForRemoteNotifications];
} else {
[[UIApplication sharedApplication] registerForRemoteNotificationTypes: (UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];
}
But it shows me deprecated methods in else part....so now how can i get rid of it...
and the other code what i tried is:
You could ignore deprecate warning like this:
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_8_0
if ([application respondsToSelector:#selector(registerUserNotificationSettings:)]) {
UIUserNotificationSettings* notificationSettings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:notificationSettings];
[[UIApplication sharedApplication] registerForRemoteNotifications];
} else {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
[[UIApplication sharedApplication] registerForRemoteNotificationTypes: (UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];
#pragma clang diagnostic pop
}
#else
[[UIApplication sharedApplication] registerForRemoteNotificationTypes: (UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];
#endif
See this post:
Hope the following code might serve your need
if ([application respondsToSelector:#selector(isRegisteredForRemoteNotifications)])
{
// iOS 8 Notifications
[application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];
[application registerForRemoteNotifications];
}
else
{
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeNewsstandContentAvailability |
UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];
}
The method registerForRemoteNotificationTypes: is depricated from iOS 8.0. Since you are compiling using iOS 8.1 SDK you will get the warning. It is an expected behavior and your application will work properly as your application will use registerForRemoteNotificationTypes: if and only if your iOS version is less than 8.0 where the method registerUserNotificationSettings: is defined.
If you need to avoid any depreciation warning you can use -Wno-deprecated-declarations flag to suppress the warning.
ok. I can understand your problem you have used deployment target 8.1.
just change deployment target less than or equal to 7, otherwise remove else part because no need to define this in 8.1 target

Push notification doesn't update immediately

I am working with push notification on iOS 8+. I try to update UIUserNotificationSettings values and get it back to compare, but it does not the same. Could someone help me resolve/explain this issue for me. Many thank.
Here is my code:
UIUserNotificationType types = UIUserNotificationTypeAlert | UIUserNotificationTypeSound;
NSLog(#"Before register: %u", types);
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:types categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
UIUserNotificationSettings* currentSeting = [[UIApplication sharedApplication] currentUserNotificationSettings];
NSLog(#"After register: %u", currentSeting.types);
Here is output console:
Before register: 6
After register: 0
Try checking it in the didRegisterUserNotificationSettings delegate method like this:
-(void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings*)notificationSettings
{
UIUserNotificationSettings* currentSeting = [[UIApplication sharedApplication] currentUserNotificationSettings];
}

Testing if push notification is turned on

NOTE: This was apparently due to some sort of corruption in the Settings, the code was apparently fine all along...
Following the conversation in this thread: Remote Notification iOS 8, I added this boilerplate code to my app:
if ([[UIApplication sharedApplication] respondsToSelector:#selector(registerUserNotificationSettings:)]) {
[[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIRemoteNotificationTypeBadge categories:nil]];
} else {
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:UIRemoteNotificationTypeAlert];
}
if ([[inLaunchOptions allKeys] containsObject:UIApplicationLaunchOptionsRemoteNotificationKey]) {
[appTracking registerPush:[inLaunchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey]];
}
I also added the application:didRegisterUserNotificationSettings handler, which calls a single line of code, [application registerForRemoteNotifications]. This code is called when the app starts.
Reading that thread, it is suggested that this is all that is required, and that this will cause application:didRegisterForRemoteNotificationsWithDeviceToken to be called. But that is definitely not happening in my app.
Did I miss a step, or simply mis-understand how to set this up?
Try This :
if ([application respondsToSelector:#selector(registerUserNotificationSettings:)]) {
UIUserNotificationType userNotificationTypes = (UIUserNotificationTypeAlert |
UIUserNotificationTypeBadge |
UIUserNotificationTypeSound);
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:userNotificationTypes
categories:nil];
[application registerUserNotificationSettings:settings];
[application registerForRemoteNotifications];
} else {
[application registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge |
UIRemoteNotificationTypeAlert |
UIRemoteNotificationTypeSound)];
}
you need to add the following line [application registerForRemoteNotifications] after [[UIApplication sharedApplication] registerForRemoteNotificationTypes:UIRemoteNotificationTypeAlert]; iOS 8 condition
Good Luck

Push notifications are not working properly

Me and my team suffering from one month because of this issue, problem is apple push notifications are working for some time in all installed devices, but after that even one device also not getting any notifications, this is happening continuously please solve this problem. Where is that problem and how to resolve this issue, please help me. I've written the below code in didFinishLaunchingWithOptions method of AppDelegate
UIUserNotificationType userNotificationTypes = (UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound);
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:userNotificationTypes categories:nil];
[application registerUserNotificationSettings:settings];
[application registerForRemoteNotifications];
if you want to register for iOS 8 and iOS 7 need to do so in didFinishLaunchingWithOptions:
if ([[UIApplication sharedApplication] respondsToSelector:#selector(registerUserNotificationSettings:)])
{
// iOS 8 Notifications
// use registerUserNotificationSettings
[[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];
[[UIApplication sharedApplication] registerForRemoteNotifications];
} else {
// iOS < 8 Notifications
// use registerForRemoteNotifications
[[UIApplication sharedApplication] registerForRemoteNotificationTypes: UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert];
}

Resources