Is it only me or did Apple break the UILocalNotification mechanism in iOS5 (GM + public release)? It seemed to work fine in the betas but since GM only the sound plays back, no alert is shown (yes I have checked the Notification Settings and the app is all on ON).
I've also made a small separate project (with a brand new app identifier just to be on the safe side), and tested the UILocalNotification class with some very simple code, on the order of:
UILocalNotification *singleLocalPush = [[UILocalNotification alloc] init];
singleLocalPush.fireDate = [NSDate dateWithTimeIntervalSinceNow:15];
singleLocalPush.hasAction = YES;
singleLocalPush.alertBody = #"Alert Body";
singleLocalPush.alertAction = #"Alert Action";
[[UIApplication sharedApplication] scheduleLocalNotification:singleLocalPush];
anyone experience similar problems? This is pretty annoying if your app IS about local notifications :)
Best,
Kacper
You should make sure your app is enabled for notifications-
Navigate to Setting->Notifications and make sure your app is enabled.
My app's local notifications work fine on iOS 5, and it was set to be deployed to 3.1.2.
Related
I have searched for this, and I can't find any documentation about doing these banner / notification / alerts... but I would really like to implement it.
In case my description in words is not clear, here is a picture of what I would like to do:
1:
I tried using this code:
UILocalNotification *note = [[UILocalNotification alloc] init];
[note setAlertBody:[NSString stringWithFormat:#"%# scanned", result]];
[note setAlertAction:#"New Scanned Image"];
[[UIApplication sharedApplication] presentLocalNotificationNow:note];
And it worked fine, such that it displayed the notification in the notifications center, but there was no banner alert.
So what are the classes that I use for this?
Thanks!
You can't define what type of alert to be used for your app's notifications. It can be set only by user through Notification Center settings.
Note! Alerts appear only when you app is closed or it is in background. If your app is active (it is in foreground), it will get only a notification (see - (void)applicationDidReceiveMemoryWarning: for details).
I have a GPS app that registers to run in the background. I also show a UILocalNotification when I have finished a process. This correctly shows, and if the app is open, then it also appears in Notification Center (swipe down from top). But, if I call the UILocalNotification when my app is in the background, or the screen is locked, I DO get the notification, but it does NOT show up in Notification Center.
I am correctly registering for notifications in my app delegate (iOS 5 bug workaround):
// Register for notifications
[[UIApplication sharedApplication]registerForRemoteNotificationTypes:
UIRemoteNotificationTypeBadge |
UIRemoteNotificationTypeAlert |
UIRemoteNotificationTypeSound];
Calling the notification:
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
localNotif.alertBody = msg;
localNotif.alertAction = NSLocalizedString(#"View", nil);
localNotif.soundName = #"alert.caf";
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:1];
[[UIApplication sharedApplication] presentLocalNotificationNow:localNotif];
[localNotif release];
Is this a bug? Why would it show up in Notification Center only when my app is open, even though the notification is shown to the user, and not other times?
Are you also using push notifications? I believe there is no need to call registerForRemoteNotificationTypes: if you are only using local.
Check Here
From the look of the code you posted, it does not look like the code would run in the background. It looks like you are trying to present a Local Notification when an even occurs in the background. You can not do this. Local Notifications are meant to be scheduled while the app is running to go off at a later time when the app is not running, and then they will trigger even when the app is closed.
You need to use Push Notifications. Check out Apple's documentation here.
EDIT: Is your app enabled for the notification center in settings?
have you added the UIBackgroundModes key to your Info.plist file ?
You need to have the UIBackgroundModes Key set to location.
After configuring everything in notification center, which allows the app to display the notification, my app's local notification doesn't fire.
Do you encounter the same problem?
more information:
The same app compiled from the same source code a few days ago, which compiled with XCode 4.1 and iOS 4.3 SDK, everything works well.
In addition, the app compiled with old version XCode and iOS SDK, can work on iOS5, after upgrade.
However, the app which compiled with the same code, but XCode 4.2 and iOS5 SDK doesn't work.
Do you have any ideas?
Or is there any special work for iOS5?
The sample code is like:
UIApplication *app = [UIApplication sharedApplication];
NSArray *oldNotifications = [app scheduledLocalNotifications];
// Clear out the old notification before scheduling a new one.
if (0 < [oldNotifications count]) {
[app cancelAllLocalNotifications];
}
// Create a new notification
UILocalNotification *alarm = [[UILocalNotification alloc] init];
if (alarm) {
alarm.fireDate = theDate;
alarm.timeZone = [NSTimeZone defaultTimeZone];
alarm.repeatInterval = NSDayCalendarUnit; //repeat every day
alarm.alertBody = [NSString stringWithFormat:#"alert"];
[app scheduleLocalNotification:alarm];
[alarm release];
}
Thanks,
Michael
In iOS 5, notifications are managed by Notification Center. You have to register your application with the Notification Center (programmatically), or (non-programmatically) go to Settings > Notifications and select appropriate settings i.e. enable Notification Center, select Alert Style, and others.
You can use following piece of code to register your application with Notification Center (programmatically), by putting it in applicationDidFinishLaunching::
// Although Register For Remote Notifications is not required for Local Notifications,
// but in iOS 5's Notifications, we have to register otherwise the system doesn't register/recognize
// the notifications posted from the application. Note that this behavior is not documented
// as of Oct 2011, and it's possible that it's a bug and will be handled in the future releases.
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:
UIRemoteNotificationTypeAlert |
UIRemoteNotificationTypeSound];
HTH.
I'm using ASIHTTPRequest to download multiple files while the iPhone app is running in the background. I want to present a UILocalNotification when the queue finishes.
The following delegate method isn't called until the app is resumed:
- (void)queueFinished:(ASINetworkQueue *)aQueue
{
NSLog(#"Queue finished");
if ([[UIApplication sharedApplication] applicationState] == UIApplicationStateBackground) {
UILocalNotification* localNotification = [[UILocalNotification alloc] init];
localNotification.alertBody = NSLocalizedString(#"All downloads completed");
[[UIApplication sharedApplication] presentLocalNotificationNow:localNotification];
[localNotification release];
}
}
So, how can I make this notification appear?
The reason your delegate isn't getting called is likely because your app is suspended in the background. If you are doing some sort of lengthy network process that continues after the user closes the app, you can use -[UIApplication beginBackgroundTaskWithExpirationHandler:] when you start the network tasks so that your application continues running in the background until you're done with the network tasks. However, it can still expire so you're not guaranteed to get enough time to finish.
From previous SO question notification when program is in background iOS 4
You do realize that when your app is in a suspended state, you won't receive any notifications -- and this is right in the documentation. There are only 3 classes of applications that can receive notifications: Audio applications (like iPod and analogues), location based applications, and voip apps. Your plist has to be set up correctly if your app is one of those applications.
Use this:
UILocalNotification *localNot = [[UILocalNotification alloc] init];
localNot.alertBody = #"Your Text";
localNot.alertAction = #"Name on the button";
localNot.fireDate = [NSDate date];
localNot.soundName = UILocalNotificationDefaultSoundName;
[[UIApplication sharedApplication] scheduleLocalNotification:localNot];
What you're doing:
Create the LocalNotification
Add the body of your LN
Add the name of the button which appears on the "alert"
Set the fireDate to the actual date and time (maybe you need to increase the actual date with 1 or 2 seconds - for this use: dateByAddingTimeInterval:)
Set the soundName (you could also use a custom sound...)
Schedule / Create the LN
Do you have your queue maxConcurrentOperationCount set to 1?
The method setShouldContinueWhenAppEntersBackground:YES is set on a per request basis, and since you have a bunch of ASIHTTPRequest's inside a queue, only one of them may be executing at a time. This means that the other items in your queue haven't even started when you suspend the app, so the OS doesn't know yet to keep that network request alive.
I'm not sure of a solution, but I think this is the reason for what you're seeing.
I just downloaded xcode and trying to make local notification example. The question is if local notification works in simulator?
thank you
Yes, local notifications work with the simulator. However, make sure you are implementing application:didreceiveLocalNotification in your app delegate if you want to see the notification while your app is in the foreground:
- (void)application:(UIApplication *)application
didReceiveLocalNotification:(UILocalNotification *)notification
{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"MyAlertView"
message:notification.alertBody
delegate:self cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alertView show];
if (alertView) {
[alertView release];
}
}
Otherwise, make sure you schedule your notification for some time in the future, then close the application, in order to see the Apple sample work:
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
if (localNotif == nil) return;
NSDate *fireTime = [[NSDate date] addTimeInterval:10]; // adds 10 secs
localNotif.fireDate = fireTime;
localNotif.alertBody = #"Alert!";
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
[localNotif release];
It's easy to think you're not implementing the test code correctly, and you just aren't handling the event while the app is running.
Another gotcha you might find, for anyone who stumbles on this older question: iOS 8 introduced new notification permissions; and your app has to explicitly ask for them.
In your AppDeligate.m:
- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//register local notifications
if ([UIApplication instancesRespondToSelector:#selector(registerUserNotificationSettings:)]){
[application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound categories:nil]];
}
//the rest of your normal code
return YES;
}
If you don't, your notification will never fire, and you'll get a wonderful message like this in your logs:
"Attempting to schedule a local notification <UIConcreteLocalNotification: 0x7ae51b10>{... alert details ...} with an alert but haven't received permission from the user to display alerts"
local notifications work on simulator, push notifications do not
To test local notifications in iPhone simulator, follow these steps:
As simulator time is exactly the one in your machine, change the time of your machine to the 1-minute previous of your desired time (when you are expecting your local notification to fire)
Restart simulator(this is awkward, but sometimes it seems iPhone simulator fails to get updated time instantly)
Run the simulator once again(maybe by running your app from xcode, in which case you must press the home button to send your app to background). Once the time is reached you should see the notification
These steps helped me always to get successful local notifications.
App Settings Notification Section not showing
I ran into this issue as well. What seems to happen is the following,
Your app runs / is installed on simulator (might have to reset the simulator state of your app, see below).
Your app has not asked for permission to show notifications.
Therefore your app settings (Settings > App Name) won't show the notification section,
You have to request notifications in the app (using UNUserNotificationCenter.current().requestAuthorization etc).
Then your app will have a section for notifications (even if you deny the allow notification prompt)!
Resetting App State
After this I launched the app again and the notification setting still showed (and showed as Off since I denied permission).
To reset this and reproduce the original problem I had to do the following,
Enable simctl XCode command line utility (See How do I fix the xcrun unable to find simctl error? )
Run in terminal xcrun simctl uninstall booted com.app.bundlename (see How to run a fresh install of the application every time unit tests are run?)