Local Notifications Wont Appear - iOS - ios

The app should send a local notification to the user when the app is in the background...
i have followed every tutorial there is, i am doing exactly as the tutorials say and i dont have any errors after writing the code or warnings but the Local notifications still wont appear after trying everything.
i am using xcode 6, running ios8 on the test device i have even tried running on the simulator and still nothing. please help. i have tried atleast 5 different ways to get local notifications working but still nothing however i have no errors... what should i add or what arent i doing?
To test that the fault that this isnt working isnt mine, i have:
-Tried restarting xcode
-Tried rebooting my device and simulator
-Tried using the code on a new project
-Tried rebooting my mac
But still the notifications wont appear... i have a feeling it might be ios8 issue or xcode6 issue
here is the code in AppDelegate.m
- (void)applicationDidEnterBackground:(UIApplication *)application {
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
[localNotification setFireDate: [NSDate dateWithTimeIntervalSinceNow:15]];
[localNotification setAlertBody:#"this is a test notification"];
[localNotification setTimeZone:[NSTimeZone defaultTimeZone]];
[[UIApplication sharedApplication]scheduleLocalNotification:localNotification];
}

Add this to your didFInishLaunchingWithOptions method in your app delegate:
if ([application respondsToSelector:#selector(registerUserNotificationSettings:)]) {
[application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound categories:nil]];
}
In iOS 8, I am not 100% sure but it may be required to put that piece of code before local notifications are allowed to work.

Since the Notifications are changed in iOS 8.0, add this in your AppDelegate didFinishLaunchingWithOptions:
if ([application respondsToSelector:#selector(isRegisteredForRemoteNotifications)])
{
// iOS 8 Notifications
[application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];
[application registerForRemoteNotifications];
}
else
{
//iOS Version < 8.0 Notifications
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:
(
UIRemoteNotificationTypeBadge |
UIRemoteNotificationTypeSound |
UIRemoteNotificationTypeAlert)];
}

Related

Device token not coming for iOS 9 device while integrating iOS native push notification with WorkLight

I am trying to send Push notification on iOS device via Worklight using sample code and steps provided at https://mobilefirstplatform.ibmcloud.com/tutorials/en/foundation/6.3/notifications/push-notifications-native-ios-applications/
When I run the app on iOS 7 device, i get a device token and therefore when i tap on subscribe button, i get a successful response. This works even if, when we don't write any code in didFinishLaunchingWithOptions for registeringForRemoteNotifications.
But When i run the same code on iOS 8 and iOS 9 device I am getting following message on console :
iOSNativePush[1119:372642] registerForRemoteNotificationTypes: is not supported in iOS 8.0 and later.
To make my application run for iOS>=8 device, I wrote following code :
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
if([[UIApplication sharedApplication] respondsToSelector:#selector(isRegisteredForRemoteNotifications)])
{
[[UIApplication sharedApplication] registerUserNotificationSettings: [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound) categories:nil]];
[[UIApplication sharedApplication] registerForRemoteNotifications];
}
else
{
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound)];
}
return YES;
}
- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings
{
[application registerForRemoteNotifications];
}
Still i am getting "iOSNativePush[1119:372642] registerForRemoteNotificationTypes: is not supported in iOS 8.0 and later" message in console but for iOS 8 device I am getting device token and device is getting subscribed but same thing is not working for iOS 9 device.
I refered following link also but no luck
https://www.ibm.com/developerworks/community/blogs/worklight/entry/understanding-and-setting-up-push-notifications-in-development-evnironment?lang=en
Kindly help me to obtain device token for iOS 9 device.
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
{
[[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];
[[UIApplication sharedApplication] registerForRemoteNotifications];
}
Use above code
This issue has been fixed in later iFixes of MobileFirst 6.3 and higher.
To resolve:
1) Update your MobileFirst studio to the latest iFix .
2) Add a new iOS native environment.
3) Replace the sample's native folder with the newly generated one ( with iFix)
4) Clean , build and deploy.

Push notification does not appear if app is in background on iOS 9

I'm having an issue with push notifications and it affects only my devices running iOS 9:
I have my app installed in a set of devices. I run it and register for push notifications with the following code:
if ([application respondsToSelector:#selector(registerUserNotificationSettings:)]) {
UIUserNotificationType userNotificationTypes = (UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound);
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:userNotificationTypes categories:nil];
[application registerUserNotificationSettings:settings];
[application registerForRemoteNotifications];
}
The app is put on background on all devices.
I send push notifications to all devices - and get the response that it succeeded.
All iOS 8 devices display the push notification in the notification center.
NO iOS 9 device display the push notification in the notification center.
Open the app in any of the iOS 9 devices in some random moment. The notification is displayed (triggered by application:didReceiveRemoteNotification:).
Has anything changed on iOS 9? How can I make the push notifications display also on iOS 9, even if the app is not active?
I don't want to use notifications to fetch any content in background, I just need to display a message.
In the end, I had to do two things that made it work for me:
Enable "Remote Notifications" for Background Modes under the Application capabilities (in the target settings)
Send the notifications with high priority (when they were sent with low priority, they were not being handled with the application on background)
I think that the user has to tap the notification to bring the app to the foreground and then application(_:didReceiveRemoteNotification:) will be called.
See here: https://www.raywenderlich.com/123862/push-notifications-tutorial
It seems you are OK. This is my code, i get notification in iOS 9.0 also.
-(void)registarPushNotification:(UIApplication*)application
{
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
{
UIUserNotificationType types = UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert;
UIUserNotificationSettings *mySettings = [UIUserNotificationSettings settingsForTypes:types categories:nil];
[application registerUserNotificationSettings:mySettings];
[application registerForRemoteNotifications];
}
else
{
[application registerForRemoteNotificationTypes:(UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert)];
}
}

Device Token not generating in ios 8.1

I have implemented push notification generation using the following methods.
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)];
}
the device token generated correctly normally. But when switch off the Sounds and Badge App Icon and set the alert style as none, (Settings -> App Name -> Notifications) the device token not generating. This issue only occur in iOS 8 devices. In iOS 7 devices it correctly working. Could any one give solution for this.
This is more a bug of iOS7. Notification has 3 way to be prompted to the user : Sounds / Badge / Alert.
If you disable sounds and badge and if you set Alert style to None ( i.e. you disable alert ) there is no way to present the notification.
I think this is battery optimisation : if there is no way to notify the user, why registering him to remote notification ?

iOS8 - Asking for remote push notifications

Two questions about changes in iOS8 for the user to accept push notifications (as well as notifications such as badges and so).
1) I am using the current approach which is working fine both on iOS7 and iOS8
if ([[[UIDevice currentDevice] systemVersion] floatValue]>= 8.0) {
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:
(UIUserNotificationTypeBadge|UIUserNotificationTypeSound|UIUserNotificationTypeAlert)
categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
[[UIApplication sharedApplication] registerForRemoteNotifications];
} else {
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:
(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];
}
This is completely working as push and alert are working and I got the token in the didRegister delegate method, but I am never asked with a pop-up as it used to be the case. Even if I completely delete the app from my phone?
Why that? Does the OS keep in memory privacy settings for apps even if they have been deleted ?
2) I saw some people suggesting to ask for remote notifications in the following delegate
- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings
{
NSLog(#"registered for notifications settings %#",notificationSettings);
//register to receive notifications
[application registerForRemoteNotifications];
}
Why that ?
From document, - (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings this method callback will be made upon calling -[UIApplication registerUserNotificationSettings:]. The settings the user has granted to the application will be passed in as the second argument. This means, once we get user permission (or if we already got before), then this method will call, where we can register the app for remote notification by calling, [application registerForRemoteNotifications].

application:didReceiveLocalNotification never called on ios 8

Is there any known problem issue with:
application:didReceiveLocalNotification delegate
on iOS 8?
My application creates local notifications using UILocalNotification. When application is in background I get notifications, and when I click on the notification banner, it moves to my app. But this method:
-(void) application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
is never called on iOS 8(Xcode 5.1.1), but works well on iOS 7 or earlier.
P.S. I've also tested project from this site:
http://www.appcoda.com/ios-programming-local-notification-tutorial/
and it doesn't work on iOS 8.
Actually, the solution on iOS 8 is to request authorization for notifications settings to the user, otherwise the delegate method -didReceiveLocalNotification: will never be called. You can do so by adding this code to the -didFinishLaunchingWithOptions: method:
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
{
[[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings
settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge)
categories:nil]];
}
This will show the user an alert view asking for permission to display notifications. If she accepts, the delegate method will be called whenever a local notification is fired.
Use this for iOS8
- (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forLocalNotification:(UILocalNotification *)notification completionHandler:(void(^)())completionHandler{
}
I meet the same problem...
You must change to use the following code:
// register notification for push mechanism
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)];
}
to instead of the original:
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound];
I have noticed the same on iOS8Beta5. Same code works fine on iOS8Beta4.
Edit:
If as the answer suggests, we need to handle it differently - then why did they drop support between two beta builds. It would make sense if iOS8Beta1 build behaved this way. This is why I feel its a bug.

Resources