this issue has been driving me crazy. What I am trying to do is trigger a notification when the user enter in a region. However, what I want is, if the user is using the app show an alert message and if the app is in background shows a local notification.
Here is my code:
- (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region
{
UIApplication *app = [UIApplication sharedApplication];
if (app.applicationState == UIApplicationStateActive)
{
NSURL *musicFile = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"Alarm" ofType:#"caf"]];
AVAudioPlayer *audioFile = [[AVAudioPlayer alloc] init];
audioFile = [[AVAudioPlayer alloc] initWithContentsOfURL:musicFile error:NULL];
[audioFile play];
UIAlertView *arrivingDestinationAlert = [[UIAlertView alloc] initWithTitle: #"Arriving" message:[NSString stringWithFormat:#"Alert"] delegate:nil cancelButtonTitle:#"Ok" otherButtonTitles:nil];
[arrivingDestinationAlert show];
}else if(app.applicationState == UIApplicationStateBackground || app.applicationState == UIApplicationStateInactive)
{
UILocalNotification *notification = [[UILocalNotification alloc] init];
notification.alertBody = [NSString stringWithFormat:#"%d to reach your destination", self.distToRemind];
notification.alertAction = #"Destination Approaching";
notification.hasAction = YES;
notification.soundName = UILocalNotificationDefaultSoundName;
[app presentLocalNotificationNow:notification];
}
[manager stopMonitoringForRegion:region];
}
What I was doing before was working. Which was lunch an local notification without asking whether the state of the app was background or active. I was just lunching the notification as soon the didEnterRegion was trigger. However now I can get it work.
Am I missing anything?
I think you are checking for the active state in the wrong spot. I do something very similar, but I check for active state in the AppDelegate when the notification fires. You should intercept the local notification in -didReceiveLocalNotification. If the state is active, push your UIAlertView there. If you fire the alert from the -didEnterRegion or -didExitRegion from the background, it will never be seen.
Related
Here is my local notification code, I get notification daily using [localNotification setRepeatInterval:NSCalendarUnitDay]; now I want to stop or cancel the notifications after the date and time expires using end date and time. and where should I implement on those, Anyone please explain.Advance in thanks.
NSString *startdate = #"10-12-2016 07:00 am";
NSString *enddate = #"14-12-2016 07:00 am";
UILocalNotification* localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = startdate;
localNotification.alertBody = [NSString stringWithFormat:#"%# ",self.nameTextField.text];
localNotification.soundName = #"bell_tree.mp3";
localNotification.alertAction = #"Show me the item";
localNotification.timeZone = [NSTimeZone defaultTimeZone];
localNotification.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber] + 1;
[localNotification setRepeatInterval:NSCalendarUnitDay];
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
and this is my app delegate didReceiveLocalNotification codings,
- (void)application:(UIApplication *)application didReceiveLocalNotification:(nonnull UILocalNotification *)notification {
UIApplicationState state = [application applicationState];
if (state == UIApplicationStateActive) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Reminder"
message:notification.alertBody
delegate:self cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
}
// Request to reload table view data
[[NSNotificationCenter defaultCenter] postNotificationName:#"reloadData" object:self];
// Set icon badge number to zero
application.applicationIconBadgeNumber = 0;
}
You can cancel that notification by using following code :
Cancel one local notification with this line of code: [[UIApplication sharedApplication] cancelLocalNotification:theNotification]
You can do this in didReceiveLocalNotification after showing notification to user.. or pass that "notification" object to that class/method whether user is canceling it.
or you can implement "cancel" button click of alert, and on that click you can cancel that Reminder(localNotification).
I want to display notification popup when app is in foreground state, with alertbody as per code snippets.
It is completely working when app is in background state.
UILocalNotification *notification = [[UILocalNotification alloc] init];
if (notification == nil)
return;
NSDate *dt = [NSDate dateWithTimeInterval:10 sinceDate:[NSDate date]];
notification.fireDate = dt;
notification.timeZone = [NSTimeZone defaultTimeZone];
notification.alertBody = #"After 10Secs...";
notification.alertAction = #"View";
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
application:didreceiveLocalNotification method in your app delegate if you want to see the nofication while your app is in the foreground:
- (void)application:(UIApplication *)application
didReceiveLocalNotification:(UILocalNotification *)notification
{
//simply show a alert,but the standard one will not show up by itself
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"MyAlertView"
message:notification.alertBody
delegate:self cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alertView show];
if (alertView) {
[alertView release];
}
}
I am working with UILocalNotification to notify users about their schedule. Everything is working fine but I'd like to know whether there is a way to display notification alert on notification center when the app is in foreground.
The alert will not be fire on running application main view.
Please help me.
Put this code in didReceiveRemoteNotification Method.
UIApplicationState state = [application applicationState];
if (state == UIApplicationStateActive)
{
NSString *cancelTitle = #"OK";
NSString *message = [[userInfo valueForKey:#"aps"] valueForKey:#"alert"];
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Test"
message:message
delegate:self
cancelButtonTitle:cancelTitle
otherButtonTitles:nil, nil];
[alertView show];
} else
{
//Do stuff that you would do if the application was not active
}
my requirement is to fire notification message like popup in iPad, message i am getting from web service. i am calling web service after certain time interval and only if user to be notified then only i have to show notification.
my code is not working, i want like pop up. my code is below:
- (void)scheduleNotificationWithInterval:(NSString *)Notificationmsg
{
[[UIApplication sharedApplication] cancelAllLocalNotifications];
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
if (localNotification == nil)
return;
localNotification.fireDate = [NSDate date];
localNotification.timeZone = [NSTimeZone defaultTimeZone];
localNotification.alertBody = #"MyApp!";
localNotification.repeatInterval = nil;
localNotification.alertBody = [NSString stringWithFormat:NSLocalizedString(Notificationmsg, nil)];
localNotification.alertAction = NSLocalizedString(#"View Details", nil);
localNotification.applicationIconBadgeNumber = 1;
/*
NSDictionary *infoDict = [NSDictionary dictionaryWithObjectsAndKeys:#"Object 1", #"Key 1", #"Object 2", #"Key 2", nil];
localNotification.userInfo = infoDict;*/
[[UIApplication sharedApplication] presentLocalNotificationNow:localNotification];
}
Please help me.
Try this one:
-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification{
// I recieved a notification
}
put this method in Appdelegate class.
When Your fire localnotification and app is open Active state it will call this method and if app is in background mode it will show notification i.e what you style you have set for your app.
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
UIApplicationState state = [application applicationState];
if (state == UIApplicationStateActive)
{
NSLog(#"lcoal:%#",[notification userInfo]);
UIAlertView *al=[[UIAlertView alloc]initWithTitle:#"Challenge gehaald!" message:#"Gefeliciteerd, je hebt deze bonus challenge succesvol afgerond." delegate:nil cancelButtonTitle:#"ok" otherButtonTitles:nil];
[al show];
}
}
I have an App registers for location updates, running tests, sometime when I enter a region while the app is in the background I receive a alarm notification with sound. sometime I only see the notification in notification center, and i did not receive any sound and alert...
What can you do to always get the sound and the alert notification ?
this is what i have in my view
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
localNotif.fireDate = nil;
localNotif.hasAction = YES;
localNotif.alertBody = fbName;
localNotif.alertAction = #"View";
localNotif.soundName = UILocalNotificationDefaultSoundName;
[[UIApplication sharedApplication]presentLocalNotificationNow:localNotif];
and this is the app delegate
- (void)application:(UIApplication *)application didReceiveLocalNotification (UILocalNotification *)notification
{
if (notification)
{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Alert"
message:notification.alertBody
delegate:self cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alertView show];
}
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
facebook = [[Facebook alloc] initWithAppId:kAppId andDelegate:self];
UILocalNotification *notification = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
if (notification)
{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Alert"
message:notification.alertBody
delegate:self cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alertView show];
}
return YES;
}
If the application is running in the background, the local notification will not get an alert or sound, as it is directly received by your application. In that case, you need to present the notification using presentLocalNotificationNow.
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
UIApplicationState applicationState = application.applicationState;
if (applicationState == UIApplicationStateBackground) {
[application presentLocalNotificationNow:notification];
}
}