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?)
Related
I am making an application where for it to work properly, the notifications must be set to alert.
I have done my research, and I don't think this is possible, but if it is how?
If it isn't possible, how do I set it so that if the user taps that it is ok if the app sends local notifications, it would set it to alert.
On the xcode simulator how could I make the notification an alert.
How could I Detect if notifications were on and set to alert (if thats possible)
First register for the UILocalNotification and when the notification comes, its delegate method is called,
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification NS_AVAILABLE_IOS(4_0)
In this method you can pass the info or do whatever you want to do.
You can use UILocalNotification methods to schedule and cancel your notifications such as
- (void)presentLocalNotificationNow:(UILocalNotification *)notification NS_AVAILABLE_IOS(4_0);
- (void)scheduleLocalNotification:(UILocalNotification *)notification NS_AVAILABLE_IOS(4_0); // copies notification
- (void)cancelLocalNotification:(UILocalNotification *)notification NS_AVAILABLE_IOS(4_0);
- (void)cancelAllLocalNotifications NS_AVAILABLE_IOS(4_0);
look at this answer
https://stackoverflow.com/a/9137501/1305001
And
https://stackoverflow.com/a/8259743/1305001
It is now user's choice to choose alert style not a programmer.
You can change your alert style from Device->Settings->Notification Center->Phone->Alert Style to :---
None
Banners
Alerts
What notifications are you talking about, a timed notification of your own?
1. If so, all you would need to do is show a [UIAlertView][1] when your notification is supposed to appear.
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Hey"
message:#"This is a message"
delegate:self
cancelButtonTitle:nil
otherButtonTitles:#"Okay", nil];
[alertView show];
[alertView release];
2. If the notification may show when the app is not launched, you can use local notifications as you mentioned with UILocalNotification . When the local notification is fired an alert will appear whether the user is in your app or not.
3. For a simulator, same as #1 if I understand what you are saying.
4. You could store the local notifications or some indicator of them in an array and refer to the array to check for pending local notifications.
Similar to this question: How do I access remote push notification data on applicationDidBecomeActive?
But the different is how can you access the notification data when you are inapplicationDidBecomeActive and if you have clicked on the app icon instead of the push notification.
The flow is: If you click on the push notification then didReceiveRemoteNotification will be triggered, but if you click on the original app icon, only applicationDidBecomeActive will be triggered and didReceiveRemoteNotification will not be called.
I am looking for the later case so how can I access the push notification data.
(Both case assuming the app is in background and not killed yet.)
You can't get remote push payload by launching app from homescreen.
If the push data is important for app use, load it from your server after app launched.
#fannheyward answer is absolutely correct. You cannot get payload when application is launched by tapping app icon.
I have an idea, what if you get to know that some notification is pending when app is launched by tapping app icon. With this knowledge your app can fetch payload from your server.
You can set "Badge" in every such notification and on applicationDidBecomeActive you can check [application applicationIconBadgeNumber] > 0 to know that some notification is active. After fetching payload from your server you can set it to 0 like below
[UIApplication sharedApplication] setApplicationIconBadgeNumber:1];
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
Please note: This means your app will have badge displayed over it when notification is received. I am not sure of the behaviour when badge is disabled by user from settings.
If your application target is over iOS7, you can do only if application is alive in backgroud.
In the capabilities settings at Xcode, you should enable Background Modes>Remote notifications, and write below code.
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
// save userInfo in NSUserDefaults
completionHandler( UIBackgroundFetchResultNoData );
}
If you want to test it, it will be helpful to use https://github.com/acoomans/SimulatorRemoteNotifications
From the server side, make sure to set content-available property with a value of 1
For this to work I also had to check the background fetch box.
You should get the notification in the launchWithOptions method in your appDelegate something like this:
NSDictionary *remoteNotif = [launchOptions objectForKey: UIApplicationLaunchOptionsRemoteNotificationKey];
if(remoteNotif != nil){
//Handle your notification
}
With iOS 5, push notifications can appear as banner and disappear after a few seconds.
I understand that didReceiveRemoteNotification will be called when user taps on the banner.
My question is, if the banner has disappeared and my user sees that there is a badge number on the app, they will tap on the app icon to start the app. Now if the app is running in the background, how do I check that the app is brought to foreground and there has been a notification, and do the necessary?
The purpose of my notification is basically to inform user there has been an update to the app content and encourage them to run the app to get the latest contents. My app only checks for latest contents at launch time and doesn't check for updates periodically.
This question is a bit old, but I'll pop what I've found in here anyway.
There are two methods you need to implement in your app delegate to check if your app was either launched from the remote notification (From when the app is not running on your device), or received the remote notification while running (in the background or foreground).
First is a method that is already in your App Delegate:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
To check if this was launched from a remote notification, have some code similar to this:
// Check to see if launched from notification
if (launchOptions != nil)
{
NSDictionary* dictionary = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
if (dictionary != nil)
{
NSLog(#"Launched from push notification: %#", dictionary);
// DO SOMETHING HERE
}
}
The other method you will need to implement is specifically for the case that your application for when your application is running:
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
NSLog(#"Received notification: %#", userInfo);
}
How you handle the notification from there is up to you, but that's how your app knows about it!
In this second method, you can check the UIApplicationState of the passed application to find out if you were in the foreground or background.
If a UILocalNotification fires with a sound set, and the user taps "Cancel" on the notification alert, the sound is stopped. But if the user taps "View", iOS then delivers the notification to the app and the sound keeps on playing. Is there any way to cancel this sound from the app? Canceling the notification in the app once the notification is delivered doesn't work (I didn't expect it to, the notification has already been delivered after all), and since I don't have the sound's system sound ID (and I didn't allocate it in the first place), I can't call AudioServicesDisposeSystemSoundID (can I?).
Is it possible to stop a UILocalNotification sound from playing if the user taps the Action button of the notification's alert?
It does not stop on the device too (5.1)
I have been trying to fix it but I can't figure it out.
I actually got it to stop on the simulator using this
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
UILocalNotification *localNotif = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
if (localNotif) {
[[UIApplication sharedApplication] cancelLocalNotification:localNotif];
}
return YES;
}
but It still doesn't stop on the device
Apparently the problem only exists on the simulator (iOS 4.2 sdk), not on the actual device.
It can be handled in application delegate method as follows, if the user taps the Action button of the notification's alert, then the following method will be called, there we can cancel the notification
- (void)application:(UIApplication *)app
didReceiveLocalNotification:(UILocalNotification *)notif {
[[UIApplication sharedApplication]cancelLocalNotification:notif];
}
I had the same problem when initially testing with an iPhone 4.
The problem appears to have gone away as of iOS 8 and now Local Notification Sounds stop when canceled after transitioning to the App.
iPhone4 [ iOS - 7.1.2 ] - Local Notification sound keeps playing in App no matter what
iPhone6 [ iOS - 8.1.1 ] - Local Notification sound stop when canceled programmatically
From what I can infer, it appears a fix exists somewhere in iOS 8.x
(Alas I didn't manage to find any documentation or release notes on the matter.)
The Problem becomes a non-issue for apps focusing on iOS 8
(provided you cancel the Local Notification coming in)
My app has an alarm function using UILocalNotification, and it works great. However, if the user uninstalls the app, then later REINSTALLS it, he would receive all the "in between" notifications at once.
I have tried to call:
[[UIApplication sharedApplication] cancelAllLocalNotifications];
if it's the first time the app is launched, but it doesn't help, because the notification is received even before application:didFinishLaunchingWithOptions: is called.
This was worse in 4.0 when the alarm was repeated even if the user has deleted the app, but at least that bug was fixed by Apple in later release. However now I'm stuck with this. Anyone has an idea?
According to Apple, this is not a bug (I filed a bug report). The system retains the UILocalNotifications for uninstalled apps for 24 hours just in case the user deleted the app by accident, and restores the said UILocalNotifications if the app is re-installed within that time frame.
The solution would be to remove all UILocalNotifications on first startup, like so:
- (BOOL) application: (UIApplication*) application
didFinishLaunchingWithOptions: (NSDictionary*) launchOptions
{
if (self.isFirstRun)
{
[[UIApplication sharedApplication] cancelAllLocalNotifications];
self.firstRun = NO;
}
/* Other code here */
...
}
of course, implement your own firstRun setter and getter to fetch/save into persistent storage, like NSUserDefaults.
This is actually a bug in iPhone. If you removed the application and install it later also, it will have same app id, so when the application is reinstalled all the past local notifications were fired even if you didn't open the app.