Alarm's notification only trigger in background - iphone

I am creating an alarm application ,in that i have created notification that display message and play sound. But now its work only in background,i have to minimize my application to trigger my notification,if my app is open then notification does not show.
code for notification are:
- (void)viewDidLoad
{
[super viewDidLoad];
dateTimePiker.date = [NSDate date];
}
- (void)scheduleLocalNotificationWithDate:(NSDate *)fireDate
{
UILocalNotification *notification = [[UILocalNotification alloc] init];
notification.fireDate = fireDate;
notification.alertBody = #"Wake up";
notification.soundName = #"ma.mp3";
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
}
- (IBAction)SetAlarmBtnTapped:(id)sender {
.....
.....
[self scheduleLocalNotificationWithDate:dateTimePiker.date];
}

implement application:didReceiveLocalNotification: on your app delegate
from the docs:
If the application is running in the foreground, there is no alert,
badging, or sound; instead, the
application:didReceiveLocalNotification: method is called if the
delegate implements it.

Related

iOS - UILocalNotification does not fire (when set in background)

I have this code to schedule a UILocalNotification:
UILocalNotification* localNotification = [[UILocalNotification alloc] init];
localNotification.userInfo = #{PLANNED_EVENT_ID : #"some id"};
localNotification.fireDate = someStartDate;
localNotification.alertBody = #"Some text";
localNotification.timeZone = [NSTimeZone defaultTimeZone];
localNotification.soundName = UILocalNotificationDefaultSoundName;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
This method works as expected when I set the above when application is in foreground, but if I use this code when application is in background (pressed home button), then it is not fired at all?
Why on earth will this not work? How can I get the result I need? I need to able to use this code when application is in background.
Thanks!
To be more clear, in my AppDelegate i have the method
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)
When I get the first notification inside this method (which works, it is scheduled in a view controller), I schedule another notification in the same way (1 min from now). So I tried to have the application in foreground, then it worked, but when I had it in background, it didn't.
Tested on Simulator though.
The method
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)
is called when the app opens after a local notification has been shown to the user and the user has opened the app from the notification. If the user doesn't open the app from the notification then this method will not be called. So your next notification is never actually added.

Handle Multiple Local Notification in IOS SDK

In my Background method, i scheduled the two notification as follow.
- (void)applicationDidEnterBackground:(UIApplication *)application
{
localNotification = [[UILocalNotification alloc] init]; //Create the localNotification object which is declared in appDelegate.h
[localNotification setFireDate:[NSDate dateWithTimeIntervalSinceNow:[datePicker countDownDuration]]]; //Set the date when the alert will be launched using the date adding the time the user selected on the timer
[localNotification setAlertAction:#"Launch"]; //The button's text that launches the application and is shown in the alert
[localNotification setAlertBody:[alertBodyField text]]; //Set the message in the notification from the textField's text
[localNotification setHasAction: YES]; //Set that pushing the button will launch the application
[localNotification setApplicationIconBadgeNumber:[[UIApplication sharedApplication] applicationIconBadgeNumber]+1]; //Set the Application Icon Badge Number of the application's icon to the current Application Icon Badge Number plus 1
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification]; //Schedule the notification with the system
// ** Notification 2***
localNotification2 = [[UILocalNotification alloc] init]; //Create the localNotification object which is declared in appDelegate.h
[localNotification2 setFireDate:[NSDate dateWithTimeIntervalSinceNow:[datePicker countDownDuration]]]; //Set the date when the alert will be launched using the date adding the time the user selected on the timer
[localNotification2 setAlertAction:#"Launch"]; //The button's text that launches the application and is shown in the alert
[localNotification2 setAlertBody:[alertBodyField text]]; //Set the message in the notification from the textField's text
[localNotification2 setHasAction: YES]; //Set that pushing the button will launch the application
[localNotification2 setApplicationIconBadgeNumber:[[UIApplication sharedApplication] applicationIconBadgeNumber]+1]; //Set the Application Icon Badge Number of the application's icon to the current Application Icon Badge Number plus 1
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification2]; //Schedule the notification with the system
}
It works fine for coming the notification.
Question
How can I detect which notification is come in didreceivenotification method??
Because I want to do the different task based on the notification came.
You can set the userInfo Dictionary
localNotification1 = [[UILocalNotification alloc] init];
localNotification1.userInfo = #{ "type" : #1 };
...
localNotification2 = [[UILocalNotification alloc] init];
localNotification2.userInfo = #{ "type" : #2 };
...
...
you can set a NSDictionary in userInfo property of a UILocalNotification
So you can do it this way...
localNotification1.userInfo = [NSDictionary dictionaryWithObject:#"1" forKey:#"NO"];
localNotification1.userInfo = [NSDictionary dictionaryWithObject:#"2" forKey:#"NO"];
and compare the key "NO" in didReceiveNotification.

How to create a local notification which does not show an alert but only wakes up the app to perform a calculation? [duplicate]

I would like to know, if it is possible to somehow "wake up" a task that is in the background, to quickly check something on the network.. I think that this could be done with UILocalNotification, however, no matter what I tried, I could not get the didReceiveLocalNotification to do ANYTHING when the app is in the background.. After starting up, I immediately close the app by pressing the Home button (there is a 10 second delay for local notification to fire). This code works PERFECTLY when the app is in the foreground, and just kind of sitting there...
In app delegate header file:
UILocalNotification *localNotif;
For testing, I set up local notification to fire quickly in the appDelegate startup.
localNotif = [[UILocalNotification alloc] init];
localNotif.fireDate = [NSDate dateWithTimeIntervalSinceNow:10]; // the date you want the notification to fire.
localNotif.timeZone = [NSTimeZone defaultTimeZone];
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
NSLog(#"setup the timer for 10 seconds");
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
UIApplicationState state = [application applicationState];
NSLog(#"getting kicked");
if (state == UIApplicationStateInactive) {
// Application was in the background when notification was delivered.
NSLog(#"INACTIVE..");
} else {
NSLog(#"ACTIVE..");
}
}
The user has a couple of choices: #1) Do they want to see a notification for your app. #2) If notifications are enabled for your app, do they want to click on your notification to launch your app. If they do accept notifications and open your notification while your app is in the background, application:didReceiveLocalNotification is called. To be clear, the user has to accept the notification (such as sliding the slider underneath the notification)... otherwise NOTHING is called.
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
NSLog(#"%#", notification);
}
If your app has been terminated application:didFinishLaunchingWithOptions: is called -
- (BOOL)application:(UIApplication *)
application didFinishLaunchingWithOptions:
(NSDictionary *)launchOptions {
UILocalNotification *theNotification =
[launchOptions
objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
NSLog(#"%#", theNotification);
return YES;
}

How send application to background when install on device?

I am making an application in which i want features like as when i run my app on device then it will closed immediately without showing any screen.But Application works in background. When user click on icon of application then it will not show any screen but work in background. After 2 minutes gap it will show a alert message. How do that?
I have used code for this given below:-
-(void)applicationDidFinishLaunching:(UIApplication *)application{
[application cancelAllLocalNotifications];
[self applicationWillTerminate:application];}-(void)applicationWillTerminate:(UIApplication *)application{
/*
Called when the application is about to terminate.
Save data if appropriate.
See also applicationDidEnterBackground:.
*/
UILocalNotification* ln = [[UILocalNotification alloc] init];
ln.fireDate =[NSDate dateWithTimeIntervalSinceNow:30];
ln.alertBody = [NSString stringWithFormat:#"Now app is working in Background."];
ln.soundName = UILocalNotificationDefaultSoundName;
[[UIApplication sharedApplication] scheduleLocalNotification:ln];
ln.hasAction=NO;
[ln release];
exit(0);}
But this is not working as i want. So what is bug in this code? How do that?
Thanks in advance...
You can't put your app away by manually calling [self applicationWillTerminate:application];. It's a delegate method that gets called when your application is about to be terminated, not a method to terminate the app.
You could try to schedule a local notification in didFinishLaunchingWithOptions: and call exit(0); afterwards. Some kind of splah screen (or black screen) will probably be shown for a moment.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
[application cancelAllLocalNotifications];
UILocalNotification* ln = [[UILocalNotification alloc] init];
ln.fireDate =[NSDate dateWithTimeIntervalSinceNow:30];
ln.alertBody = [NSString stringWithFormat:#"Now app is working in Background."];
ln.soundName = UILocalNotificationDefaultSoundName;
[[UIApplication sharedApplication] scheduleLocalNotification:ln];
ln.hasAction=NO;
[ln release];
exit(0); //this line kills the app (and gets your app rejected)
return NO; //this line is just to make compiler happy
}
Please note that this will most definetly not be approved for App Store.

Can UILocalNotification fire a custom method when App is in background mode?

Well the title is self explained. I want to create an App that can manage programmed local notifications when App is in background mode. Notifications works smoothly but I want to fire a custom method when the alert is fired.
Is it possible?
Thank you.
Yes it can be done. You can do somethng like this:
- (void)applicationDidEnterBackground:(UIApplication *)application {
[NSTimer scheduledTimerWithTimeInterval:17.0 target:self selector:#selector(makeNotificationRequest:) userInfo:nil repeats:YES];
}
-(void)makeNotificationRequest:(NSTimer *)timer
{
CLLocation *location = [[AppHelper appDelegate] mLatestLocation];
NSMutableDictionary *paramDic = [[NSMutableDictionary alloc] init];
#ifdef _DEBUG
[paramDic setValue:[NSString stringWithFormat:#"77.586"] forKey:#"Lat"];
[paramDic setValue:[NSString stringWithFormat:#"12.994"] forKey:#"long"];
#else
[paramDic setValue:[NSString stringWithFormat:#"%f",location.coordinate.latitude] forKey:#"Lat"];
[paramDic setValue:[NSString stringWithFormat:#"%f",location.coordinate.longitude] forKey:#"long"];
#endif
WNetwork *mNetwork = [[WNetwork alloc] init];
[mNetwork makeRequsetWithURL:URL_Showbeeps type:JBJsonParser paramDictionary:paramDic delegate:self];
[mNetwork autorelease];
NSLog(#"URL HIT%#",paramDic);
[paramDic autorelease];
}
And to customize your action on alert you can use this:
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
UIApplicationState state = [application applicationState];
if (state == UIApplicationStateActive) {
;
}
}
If the app were in the foreground then you would want the - (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification method of UIApplicationDelegate, however the documentation suggests that this will not be called when the app is in the background.
You can get a list of local notifications by calling scheduledLocalNotifications on your UIApplication instance - you can then poll these for their times and schedule a function to call at that time in your background mode. This won't necessarily 100% match up with when the Local Notification fires though, but I think it's as close as the App Store guidelines will let you get.
You can also present your own Local Notifications when you are in background mode by calling the presentLocalNotificationNow: method:
https://developer.apple.com/library/IOS/#documentation/UIKit/Reference/UIApplication_Class/Reference/Reference.html#//apple_ref/occ/instm/UIApplication/presentLocalNotificationNow:
so you could work around this by just presenting your own notifications and not scheduling them for the OS to deliver.
If you are trying to access Local Notifications from other applications then I don't think this is allowed.
No ..no custom methods are defined while app is running in background..Once an notification is fired we can;t change the alert message also. But nice when we show the alert message then by clicking the yes button to the alert take you to the one method called app is Running in Background which is in AppDelegate.m file