Local notifications not working for some users (iOS 8) - ios

I have an app which uses local notifications and ti used to work fine in previous versions. I have updated the app for iOS 8 and tested and worked fine. After submitting the update to app store, a small number of users are complaining that they don't get any local notifications. However, a larger number of users that I've checked are fine and don't observe any issues.
For the users with the error (at least one of them), they can not see the "Notifications" item in the "Settings->myApp" screen; The whole option is missing not that it is disabled. "Location" and "Use Cellular Data" are in that screen but not the Notifications. I have tried to change the settings under "Settings->Notifications->myApp" and it work as expected.
Any suggestions for how to debug this issue would be very helpful. Thanks!

Try this for Objective-C:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions: (NSDictionary *)launchOptions
{
// are you running on iOS8?
if ([application respondsToSelector:#selector(registerUserNotificationSettings:)])
{
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeBadge|UIUserNotificationTypeAlert|UIUserNotificationTypeSound) categories:nil];
[application registerUserNotificationSettings:settings];
}
else // iOS 7 or earlier
{
UIRemoteNotificationType myTypes = UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound;
[application registerForRemoteNotificationTypes:myTypes];
}
}
For Swift:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool {
// Override point for customization after application launch.
if(UIApplication.instancesRespondToSelector(Selector("registerUserNotificationSettings:")))
{
application.registerUserNotificationSettings(UIUserNotificationSettings(forTypes: UIUserNotificationType.Sound | UIUserNotificationType.Alert | UIUserNotificationType.Badge, categories: nil))
}
else
{
//
}
return true
}

First You have to Register to Use Local Push Notification
UIApplication *application = [UIApplication sharedApplication];
if ([application respondsToSelector:#selector(registerUserNotificationSettings:)]) {
[application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound categories:nil]];
}
then you can send Local Push Notification By This
UILocalNotification *notification = [UILocalNotification new];
notification.alertBody = #"Local Push !!!";
notification.applicationIconBadgeNumber=1;
[[UIApplication sharedApplication] presentLocalNotificationNow:notification];

void EnableLocalNotificationIOS8
{
UIApplication *app = [UIApplication sharedApplication];
if ([app respondsToSelector:#selector(registerUserNotificationSettings:)])
{
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound categories:nil];
[app registerUserNotificationSettings:settings];
[app registerForRemoteNotifications];
}
}

Related

Push notification token not received on iOS 8. Works fine on iOS 7 [duplicate]

How can I get the Device Token for remote notification in iOS 8?
I used the method didRegisterForRemoteNotificationsWithDeviceToken in AppDelegate in iOS < 8, and it returned the device token. But in iOS 8, it does not.
Read the code in UIApplication.h.
You will know how to do that.
First:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
add Code like this
if ([application respondsToSelector:#selector(registerUserNotificationSettings:)]) {
#ifdef __IPHONE_8_0
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeAlert
| UIUserNotificationTypeBadge
| UIUserNotificationTypeSound) categories:nil];
[application registerUserNotificationSettings:settings];
#endif
} else {
UIRemoteNotificationType myTypes = UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound;
[application registerForRemoteNotificationTypes:myTypes];
}
if you not using both Xcode 5 and Xcode 6 ,try this code
if ([application respondsToSelector:#selector(registerUserNotificationSettings:)]) {
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:(UIRemoteNotificationTypeBadge
|UIRemoteNotificationTypeSound
|UIRemoteNotificationTypeAlert) categories:nil];
[application registerUserNotificationSettings:settings];
} else {
UIRemoteNotificationType myTypes = UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound;
[application registerForRemoteNotificationTypes:myTypes];
}
(Thanks for #zeiteisen #dmur 's remind)
Second:
Add this Function
#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
{
//handle the actions
if ([identifier isEqualToString:#"declineAction"]){
}
else if ([identifier isEqualToString:#"answerAction"]){
}
}
#endif
And your can get the deviceToken in
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
if it still not work , use this function and NSLog the error
-(void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error
The way to register for iOS 8 and keep supporting older versions
UIApplication *application = [UIApplication sharedApplication];
if ([application respondsToSelector:#selector(registerUserNotificationSettings:)]) {
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeBadge
|UIUserNotificationTypeSound
|UIUserNotificationTypeAlert) categories:nil];
[application registerUserNotificationSettings:settings];
} else {
UIRemoteNotificationType myTypes = UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound;
[application registerForRemoteNotificationTypes:myTypes];
}
and in the app delegate add
- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings {
[application registerForRemoteNotifications];
}
iOS8 can receive silent notificaions without asking for permission. Call
- (void)registerForRemoteNotifications. After this application:didRegisterForRemoteNotificationsWithDeviceToken: will be called
Note: the callback with the token is only called if the application has successfully registered for user notifications with the function below or if Background App Refresh is enabled.
Check the Settings for you app if any notification type is enabled. If not, you will not get a device token.
You are now able to obtain silent notifications with
aps {
content-available: 1
}
in the notification payload
But notifications that appear still needs permission. Call
UIUserNotificationType types = UIUserNotificationTypeSound | UIUserNotificationTypeBadge | UIUserNotificationTypeAlert;
UIUserNotificationSettings *notificationSettings = [UIUserNotificationSettings settingsForTypes:types categories:nil];
[application registerUserNotificationSettings:notificationSettings];
This code should ask for permission.
You should now be ready to get push notifications
In my case I had made the necessary updates to request push notification access for iOS 7 and iOS 8, however I had not implemented the new callback for when an iOS 8 user grants access. I needed to add this method to my app delegate.
- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings {
[application registerForRemoteNotifications];
}
If you are using Xamarin.iOS to build your mobile application, you can use this snippet of code to request push notification registration
if (UIDevice.CurrentDevice.CheckSystemVersion(8,0))
{
UIUserNotificationType userNotificationTypes = UIUserNotificationType.Alert | UIUserNotificationType.Badge | UIUserNotificationType.Sound;
UIUserNotificationSettings settings = UIUserNotificationSettings.GetSettingsForTypes(userNotificationTypes, null);
UIApplication.SharedApplication.RegisterUserNotificationSettings(settings);
}
else
{
UIRemoteNotificationType notificationTypes = UIRemoteNotificationType.Alert | UIRemoteNotificationType.Badge | UIRemoteNotificationType.Sound;
UIApplication.SharedApplication.RegisterForRemoteNotificationTypes(notificationTypes);
}
Also you will need to override DidRegisterUserNotificationSettings method to get the device token returned from the APNS server:
public override void DidRegisterUserNotificationSettings(UIApplication application, UIUserNotificationSettings notificationSettings)
{
application.RegisterForRemoteNotifications();
}
The answer of Madao (https://stackoverflow.com/a/24488562/859742) is right but....
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:(UIRemoteNotificationTypeBadge
should be more "correct"
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert) categories:nil];
Those flags have the same bit mask values and thats why both would work but UIUserNotificationSettings requires UIUserNotificationType not UIRemoteNotificationType.
Apart from that I would call
[application registerUserNotificationSettings:settings];
In the AppDelegate method (depending on the granted rights),
- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings
I thing the better way to keep backward compatibility we can go with this approach, it is working for my case hope work for you. Also pretty easy to understand.
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)];
}
UIUserNotificationType types = UIUserNotificationTypeBadge |
UIUserNotificationTypeSound | UIUserNotificationTypeAlert;
UIUserNotificationSettings *mySettings =
[UIUserNotificationSettings settingsForTypes:types categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:mySettings];
[application registerForRemoteNotifications];

iOS8 can receive push notification but not iOS7

/*--- Setup Push Notification ---*/
//For iOS 8
if ([UIApplication instancesRespondToSelector:#selector(registerUserNotificationSettings:)] && [UIApplication instancesRespondToSelector:#selector(registerForRemoteNotifications)])
{
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert) categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
}
//For iOS 7 & less
else if ([UIApplication instancesRespondToSelector:#selector(registerForRemoteNotificationTypes:)])
{
UIRemoteNotificationType myTypes = UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound;
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:myTypes];
}
Hi! I hope someone can help me out there. I have updated my code in order to get push notification for iOS8. Everything works fine on iOS8 device, however it seems the push notification does not work anymore on iOS7 device. Is there something I'm missing? Thanks for helping out!
Use this code
if ([application respondsToSelector:#selector(registerUserNotificationSettings:)]) {
[[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge|UIUserNotificationTypeAlert|UIUserNotificationTypeSound categories:nil]];
} else {
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:UIRemoteNotificationTypeAlert|UIRemoteNotificationTypeBadge|UIRemoteNotificationTypeSound];
}
#ifdef __IPHONE_8_0
- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings {
[application registerForRemoteNotifications];
}
#endif
And other push related Methods.. this will work in both ios..
Hope this will help you.

On/Off PushNotification from app using parse

I developed app which using Parse PushNotification for iOS,
I have kept notification On/Off option in my app,
What i want to do is when user clicks on notification OFF i want to disable push notification for that device or when user clicks on notification ON i want to enable push notification for that device.
I have wrote this code snippet but it doesn't work,
if ([switcher isOn])
{
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound)];
}else{
[[UIApplication sharedApplication] unregisterForRemoteNotifications];
}
Do i need to write some more code ? or am i making any mistake ?
Plaese help and thanks in advance.
We should do some change when register Notification in ios8,
if (SYSTEM_RUNS_IOS8_OR_LATER) {
if ([[UIApplication sharedApplication] respondsToSelector:#selector(registerUserNotificationSettings:)]) {
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge|UIUserNotificationTypeSound categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
} else {
UIRemoteNotificationType myTypes = UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound;
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:myTypes];
}
}else{
UIRemoteNotificationType myTypes = UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound;
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:myTypes];
}

IOS 8 push notification check returns wrong UIRemoteNotificationType

I'm trying to check if PushNotifications for my app are enabled.
In AppDelegate.m I do register app for remote notifications, and in Settings on iPhone (iOS 8) Push Notifications for this app are enabled.
I've googled to methods:
UIRemoteNotificationType types = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];
BOOL check = [[UIApplication sharedApplication] isRegisteredForRemoteNotifications];
As the result, types = UIRemoteNotificationTypeNone and check = NO.
I'm using this code sample to register application for Push Notifications:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions
{
//-- Set Notification
if ([application respondsToSelector:#selector(isRegisteredForRemoteNotifications)])
{
// iOS 8 Notifications
[application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];
[application registerForRemoteNotifications];
}
else
{
// iOS < 8 Notifications
[application registerForRemoteNotificationTypes:
(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound)];
}
//--- your custom code
return YES;
}
What is the problem here?
I think you've already found a solution but just in case: in iOS 8, enabledRemoteNoficationTypes is deprecated you should use:
if ([[UIApplication sharedApplication] respondsToSelector:#selector(currentUserNotificationSettings)]) {
UIUserNotificationSettings *currentSettings = [[UIApplication sharedApplication] currentUserNotificationSettings];
if (currentSettings.types == UIUserNotificationTypeNone) {}
Best
Try checking your state with this code:
UIRemoteNotificationType allOnType = UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeBadge ;
UIRemoteNotificationType offType = UIRemoteNotificationTypeNone ;
UIRemoteNotificationType currentTypes = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];
if (currentTypes == allOnType) {
// all are on
} else if (currentTypes == offType) {
// all are off
} else {
// some are on, some are off
}
Edit
Also, try implementing the Push Notification Callbacks:
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
NSLog(#"Token%#",deviceToken);
}
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)err
{
NSLog(#"err:%#",err);
}
-(void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings
{
//for iOS8
}

Push notification stopped working for IOS 8

I have my application signed using enterprise certificate which supports push notification feature in it. It was working fine and when I updated iphone to iOS 8 push notification stopped working. After I debug and little research I came to know that following code to be added to retrieve push token from iOS 8 onwards.
if ([application respondsToSelector:#selector(registerUserNotificationSettings:)]) //>iOS8
{
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert) categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
}else {// <iOS8
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];
}
Add following callback methods,
- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings
{
[application registerForRemoteNotifications];
}
- (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forRemoteNotification:(NSDictionary *)userInfo completionHandler:(void(^)())completionHandler
{
//handle the actions
}
However we do not face this problem with appstore version of our application and all working good. Is it something broken on enterprise certification only?
You are forgeting a line code: [[UIApplication sharedApplication] registerForRemoteNotifications];
something like this:
if ([application respondsToSelector: selector (registerUserNotificationSettings :)]) {
UIUserNotificationSettings * settings = [UIUserNotificationSettings settingsForTypes: UIUserNotificationTypeBadge | UIUserNotificationTypeAlert | UIUserNotificationTypeSound
categories: nil ];
[[UIApplication sharedApplication] registerUserNotificationSettings: settings];
[[UIApplication sharedApplication] registerForRemoteNotifications];
} else {
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:
UIRemoteNotificationTypeBadge |
UIRemoteNotificationTypeAlert |
UIRemoteNotificationTypeSound];
}

Resources