In iOS7.1 , when a user kills an app , does the geofencing still works?
If geofencing is detecting the device is entering or exiting the region, can it trigger a local notification (even when the user has killed the app ) ?
you GeoFencing still works it works like push notification / or passbook same thing it will still generate notification for your app. bellow function will fire even if app is not running in background.
- (void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region {
NSString *event = [NSString stringWithFormat:#"didExitRegion %# at %#", region.identifier, [NSDate date]];
[self updateWithEvent:event];
//implement local notification:
UIApplication *app = [UIApplication sharedApplication];
UILocalNotification *notification = [[UILocalNotification alloc] init];
[[UIApplication sharedApplication] cancelAllLocalNotifications];
if (notification == nil)
return;
notification.alertBody = [NSString stringWithFormat:#"Did You Lock Your House?"];
notification.alertAction = #"Lock House";
notification.soundName = UILocalNotificationDefaultSoundName;
notification.applicationIconBadgeNumber = 1;
[app presentLocalNotificationNow:notification];
[notification release];
}
Related
I used Notification Alert for my app and used the below code
if(condition){
[defaults setBool:YES forKey:#"notificationIsActive"];
[defaults synchronize];
//self.message.text=#"Notifications Started";
NSTimeInterval interval;
interval = 5;
UILocalNotification* localNotification = [[UILocalNotification
alloc] init];
localNotification.fireDate = [NSDate
dateWithTimeIntervalSinceNow:interval]; //Enter the time here in
seconds.
localNotification.alertBody= #"Alert time";
localNotification.timeZone = [NSTimeZone defaultTimeZone];
localNotification.repeatInterval=
NSCalendarUnitDay;//NSCalendarUnitMinute;
//Repeating instructions here.
localNotification.soundName= UILocalNotificationDefaultSoundName;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
}
else
{
NSArray *arrayOfLocalNotifications = [[UIApplication sharedApplication] scheduledLocalNotifications] ;
for (UILocalNotification *localNotification in arrayOfLocalNotifications) {
if ([localNotification.alertBody isEqualToString:#"Alert time"]) {
NSLog(#"the notification this is canceld is %#", localNotification.alertBody);
[[UIApplication sharedApplication] cancelLocalNotification:localNotification] ; // delete the notification from the system
}
}
}
Now problem is I got extra notifications showing this is message Users will see. I checked everything that there is no extra notification alert I added. Now what can I do? Please help..
EDIT:
One point is missing. I used that notification for testing in my app but later I removed that notification.
I'm developing an iOS App which makes a local notification as an Alert on screen inside the app. I want it to send the notification to the main Notifications area and not alert anything on screen instead. How can I do this?
At the moment the notification area just says "No Notifications".
Here's my code:
AppDelegate
- (void)application:(UIApplication *)application didReceiveLocalNotification:(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];
}
// Set icon badge number to zero
application.applicationIconBadgeNumber = 0;
}
ViewController
self.itemText.text = #"Notification";
NSDate *pickerDate = [[NSDate alloc] initWithTimeIntervalSinceNow:5];
NSLog(#"Picked date is %#", pickerDate);
NSDate *todaysDate;
todaysDate = [NSDate date];
NSLog(#"Todays date is %#", todaysDate);
// Schedule the notification
UILocalNotification* localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = pickerDate;
// TO DO : Assign proper text to self.itemText.text based on real data
localNotification.alertBody = self.itemText.text;
localNotification.alertAction = #"Another";
localNotification.timeZone = [NSTimeZone defaultTimeZone];
localNotification.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber] + 1;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
UILocalNotification will be added to the main Notifications area only if application is in background or terminated. In the foreground it will call your method
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
The only way you can get what you want, is to use UserNotifications.framework (since iOS 10).
Introduction to User Notifications Framework in iOS 10 is just the first tutorial
Here is the Apple Documentation on Local Notifications
And using it, don't forget to implement method of UNUserNotificationCenterDelegate to get Native notification even if your application is in foreground.
- (void)userNotificationCenter:(UNUserNotificationCenter *)center
willPresentNotification:(UNNotification *)notification
withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler;
Hope it helps!
I have implemented the
didEnterRegion
method and it works in background for example if I show an alertview...even with the app in background (when I foreground the app the alertViews show)
I use alertview only to test the didEnterRegion method in background and it works...but now im trying to fire an UILocalNotification in didEnterRegion but the UILocalNotification is not firing. Theres my code:
-(void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region {
int iRegiao = [region.identifier intValue];
Location *ocorrencia = (Location *)[self.locations.objects objectAtIndex:iRegiao];
// NSLog(#"Voce está proximo da %#", ocorrencia.name);
// UIApplication *app = [UIApplication sharedApplication];
UILocalNotification *notification = [[UILocalNotification alloc] init];
[[UIApplication sharedApplication] cancelAllLocalNotifications];
notification.fireDate = [NSDate date];// Now here you can manage the fire time.
notification.timeZone = [NSTimeZone defaultTimeZone];
notification.alertBody = ocorrencia.name;
notification.alertAction = ocorrencia.name;
notification.soundName = UILocalNotificationDefaultSoundName;
notification.applicationIconBadgeNumber = 1;
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
}
Are you targeting iOS 8? If so, you need to ask for permission for local notifications.
Try this:
if ([[UIApplication sharedApplication] respondsToSelector:#selector(registerUserNotificationSettings:)]) {
[[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeSound|UIUserNotificationTypeBadge categories:nil]];
}
The respondsToSelector: check is needed if you are also supporting iOS 7 or below.
Did you try to use - (void)presentLocalNotificationNow:(UILocalNotification *)notification on UIApplication? (it ignores fireDate)
I'm developing application which acquire the functionalities of Local notification. When few conditions matched, i need to fire Local notifications. I'm done with firing local notification and actions then. But i need to show all fired notifications in 'Notifications Center' even application is foreground or background.
I'm use following code :
In didFinishLaunchingWithOptions method :
if ([UIApplication instancesRespondToSelector:#selector(registerUserNotificationSettings:)]) {
[[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge|UIUserNotificationTypeSound categories:nil]];
}
When few conditions matched :
for (int i = 0; i < [arrayNotif count]; i++) {
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
[localNotif setAlertBody:[NSString stringWithFormat:#"%#", [[arrayNotif objectAtIndex:i] valueForKey:#"title"]]];
[localNotif setFireDate:[NSDate dateWithTimeIntervalSinceNow:1]];
[localNotif setTimeZone:[NSTimeZone defaultTimeZone]];
NSDictionary *dictionary = [NSDictionary dictionaryWithObject:[arrayNotif objectAtIndex:i] forKey:[NSString stringWithFormat:#"%#", [[arrayNotif objectAtIndex:i] valueForKey:#"id"]]];
[localNotif setUserInfo:dictionary];
localNotif.soundName = UILocalNotificationDefaultSoundName;
[localNotif setApplicationIconBadgeNumber:[[UIApplication sharedApplication] applicationIconBadgeNumber] + 1];
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
dictionary = nil;
}
I get all notifications by didReceiveLocalNotification: method only application is in foreground. Now, i want to show all notifications in Notification Center.
I want to cancel a UILocalnotification , when i cancel notification still the notification is being fired . I wanted to know do i have to call any delegate method in appdelegate to cancel notification .the code which i am using is executing correctly .but the notification is getting fired .
Should i use NSNotification center which has removeObserver method to cancel uilocalnotification.
Does UILocalnotification fires notification from the app or from the device.
UPDATE - This is how i am scheduling my notification
-(UILocalNotification *)scheduleNotification :(int)remedyID
{
NSString *descriptionBody;
NSInteger frequency;
UILocalNotification *notif = [[UILocalNotification alloc] init];
NSLog(#"%d",remedyID);
descriptionBody =[[self remedyDetailsForRemedyID:remedyID] objectForKey:#"RemedyTxtDic"];
frequency = [[[self remedyDetailsForRemedyID:remedyID] objectForKey:#"RemedyFrequency"]intValue];
NSArray *notificationFireDates = [self fireDatesForFrequency:frequency];
for (NSDate *fireDate in notificationFireDates)
{
notif.timeZone = [NSTimeZone defaultTimeZone];
notif.repeatInterval = NSDayCalendarUnit;
notif.alertBody = [NSString stringWithString:descriptionBody];
notif.alertAction = #"Show me";
notif.soundName = UILocalNotificationDefaultSoundName;
notif.applicationIconBadgeNumber = 1;
notif.fireDate = fireDate;
NSDictionary *userDict = [NSDictionary dictionaryWithObjectsAndKeys:notif.alertBody, #"kRemindMeNotificationDataKey", [NSNumber numberWithInt:remedyID],kRemindMeNotificationRemedyIDKey,
nil];
notif.userInfo = userDict;
[[UIApplication sharedApplication] scheduleLocalNotification:notif];
}
return notif;
}
Cancelling notification
- (void)cancelNotification:(int)remedyId
{
NSArray *notifications = [[UIApplication sharedApplication] scheduledLocalNotifications];
NSLog(#"Cancelling... Before %d",[[[UIApplication sharedApplication]scheduledLocalNotifications]count]);
for (UILocalNotification *notification in notifications)
{
int notifRemedyId = [[notification.userInfo objectForKey:#"kRemindMeNotificationRemedyIDKey"]intValue];
NSLog(#"remedyID : %d",remedyId);
NSLog(#"notifyId : %d",notifRemedyId);
if (remedyId == notifRemedyId) {
[[UIApplication sharedApplication] cancelLocalNotification:notification];
}
}
NSLog(#"Cancelling... After %d",[[[UIApplication sharedApplication]scheduledLocalNotifications]count]);
}
NSNotification center which has removeObserver method to cancel uilocalnotification.
NSNotificationCenter has nothing to do with UILocalNotification. I'm sorry that they both use "notification" somewhere in their names, but that is just coincidence really.
scheduledLocalNotifications will give you the list of all scheduled notifications and use
- (void)cancelLocalNotification:(UILocalNotification *)notification
or you can cancel them all using:
[[UIApplication sharedApplication] cancelAllLocalNotifications];
Edit :
NSString *notifRemedyId = #"notifRemedyId";
UILocalNotification *localnotif=[[UILocalNotification alloc]init];
NSDictionary *userDict = [NSDictionary dictionaryWithObjectsAndKeys:#"kRemindMeNotificationRemedyIDKey", kRemindMeNotificationRemedyIDKey,YOUR_REMEDYID,#"notifRemedyId",nil];
localnotif.userInfo = userDict;
[[UIApplication sharedApplication] scheduleLocalNotification:localnotif];
Cancel as :
for (UILocalNotification *aNotif in [[UIApplication sharedApplication] scheduledLocalNotifications]) {
if ([[aNotif.userInfo objectForKey: kRemindMeNotificationRemedyIDKey] isEqualToString:#"kRemindMeNotificationRemedyIDKey"]) {
if (remedyId == [[aNotif.userInfo objectForKey: kRemindMeNotificationRemedyIDKey] intValue]) {
[[UIApplication sharedApplication] cancelLocalNotification:aNotif];
break;
}
}
}
Hope it helps you.