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];
}
Related
I have a peculiar station in one of my apps. Alt ought I call:
[UIApplication sharedApplication].applicationIconBadgeNumber=<value>
the badge keeps on not being shown.
In the didFinishLaunchingWithOptions of the app delegate I have regularly called:
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound) categories:nil];
[application registerUserNotificationSettings:settings];
[application registerForRemoteNotifications];
I also tried to set it by means of a notification, yet the notification is shown but the badge is not updated.
What might be the issue or how could I know better?
First of all register UIUserNotificationSettings
UIUserNotificationSettings* notificationSettings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeBadge categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:notificationSettings];
[UIApplication sharedApplication].applicationIconBadgeNumber = <value>;
It was much much easier. Apparently iOS upon an update removed the "show badge icon" from the app settings. Once enabled the badge updates correctly. Thanks for your support anyway.
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.
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..
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
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];
}