2

I am using following code for local notification. But It is not working. Location is successfully being updated and it get into these methods but notification is not being fired. Any idea?:

NOTE: It is working when the app is in background but not working when the app is closed.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    if ([UIApplication instancesRespondToSelector:@selector(registerUserNotificationSettings:)]){
        [application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound categories:nil]];
    }
    [[UIApplication sharedApplication] cancelAllLocalNotifications];
    // Override point for customization after application launch.
    self.locationManager = [[CLLocationManager alloc] init];
    self.locationManager.delegate = self;

    return YES;
}

-(void) locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region {
    if ([region isKindOfClass:[CLBeaconRegion class]]) {
        UILocalNotification *notification = [[UILocalNotification alloc] init];
        notification.alertBody = @"You are checked in";
        notification.soundName = @"Default";
        [[UIApplication sharedApplication] presentLocalNotificationNow:notification];
    }
}
Pang
  • 9,564
  • 146
  • 81
  • 122
Idrees
  • 133
  • 1
  • 2
  • 6

5 Answers5

1

You should keep in mind that notifications present only when your app in background, not at the foreground. If your are in foreground implement - application:didReceiveLocalNotification: of the AppDelegate and handle notification manually by yourself.

UPD

If your app is not running even in background, your code will not be executed. Look for Background modes (Tracking the User’s Location section) for possible solutions in order to ask system launch your app by events even currently it is not in the memory

Azat
  • 6,745
  • 5
  • 31
  • 48
1

My case was the notification was disabled from the Settings -> Notifications -> YOUR_APP -> Allow Notifications

Mazen Kasser
  • 3,559
  • 2
  • 25
  • 35
0

Wen app is closed applicationDidEnterBackground was called so put this background task code. and class the local notification.

- (void)applicationDidEnterBackground:(UIApplication *)application 
{
       bgTask = [application beginBackgroundTaskWithName:@"MyTask" expirationHandler:^{
    // Clean up any unfinished task business by marking where you
    // stopped or ending the task outright.
    [application endBackgroundTask:bgTask];
    bgTask = UIBackgroundTaskInvalid;
}];

// Start the long-running task and return immediately.
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

    // Do the work associated with the task, preferably in chunks.

    [application endBackgroundTask:bgTask];
    bgTask = UIBackgroundTaskInvalid;
});
    ////  just u call the method want ever u want example

    [self notification];
}

- (void)notification
{ 
    UILocalNotification *notification = [[UILocalNotification alloc] init];
    notification.alertBody = @"You are checked in";
    notification.soundName = @"Default";
    [[UIApplication sharedApplication] presentLocalNotificationNow:notification];
}

I think this is helpful to u.

saravanaa
  • 9
  • 9
0

Local notification will work even your application removed from the background. But in your case, you are listening to the location manager event and triggering the local notification inside the delegate method. Your location manager event will not get triggered once you killed the application. So your local notification will not get triggered at all.

Augustine P A
  • 5,008
  • 3
  • 35
  • 39
0

When your app gets killed, beacon monitoring will still relaunch it — that's the great thing about beacon monitoring!

All you need to do is re-start the monitoring in the application:didFinishLaunchingWithOptions: method. If you do that, the didEnterRegion event which resulted in launch of the app will immediately get delivered to the delegate, which should trigger the notification.

Estimote has a more detailed guide: Launching notifications in iOS when the app is killed. It uses ESTBeaconManager, ESTBeaconManagerDelegate and ESTBeaconRegion classes from the Estimote SDK, but you can simply replace these with regular Core Location classes for the same effect.

heypiotr
  • 2,139
  • 2
  • 15
  • 22