Rahul Sharma
Rahul Sharma

Reputation: 6179

application:didReceiveLocalNotification: not called when app in background

When my app goes in background then applicationDidEnterBackground is automatically called and in this method we fire local notification. but didReceiveLocalNotification: method is not called

- (void)applicationDidEnterBackground:(UIApplication *)application {

UIApplication *app = [UIApplication sharedApplication];
bgTask = [app beginBackgroundTaskWithExpirationHandler:^{
    bgTask = UIBackgroundTaskInvalid;
}];

UILocalNotification* localNotification = [[UILocalNotification alloc] init];
localNotification.timeZone = [NSTimeZone defaultTimeZone];
localNotification.fireDate = [NSDate date];
localNotification.alertBody = textString;
localNotification.alertAction = @"View";
localNotification.soundName = UILocalNotificationDefaultSoundName;
localNotification.applicationIconBadgeNumber =  1;

[[UIApplication sharedApplication] presentLocalNotificationNow:localNotification];
}

Upvotes: 2

Views: 6025

Answers (3)

Doro
Doro

Reputation: 2413

Before scheluding notification you must register it for specified type:

    - (void)applicationDidEnterBackground:(UIApplication *)application {

    UIApplication *app = [UIApplication sharedApplication];
    bgTask = [app beginBackgroundTaskWithExpirationHandler:^{
        bgTask = UIBackgroundTaskInvalid;
    }];

    UILocalNotification* localNotification = [[UILocalNotification alloc] init];
    localNotification.timeZone = [NSTimeZone defaultTimeZone];
    localNotification.fireDate = [NSDate date];
    localNotification.alertBody = textString;
    localNotification.alertAction = @"View";
    localNotification.soundName = UILocalNotificationDefaultSoundName;
    localNotification.applicationIconBadgeNumber =  1;
    // register notification settings
    if ([application respondsToSelector:@selector(registerUserNotificationSettings:)]) {
#ifdef __IPHONE_8_0
  UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:(UIRemoteNotificationTypeBadge
      |UIRemoteNotificationTypeSound
      |UIRemoteNotificationTypeAlert) categories:nil];
  [application registerUserNotificationSettings:settings];
#endif
} else {
  UIRemoteNotificationType myTypes = UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound;
  [application registerForRemoteNotificationTypes:myTypes];
}

    [[UIApplication sharedApplication] presentLocalNotificationNow:localNotification];
    }

Upvotes: 0

Tariq
Tariq

Reputation: 9979

Your expectations with local notifications are incorrect:

When your app will go in background then you will see an immediate notification on your phone but you have to tap that notification to trigger didReceiveLocalNotification delegate.

If you receive local notification in foreground then didReceiveLocalNotification will be triggered automatically.

Above scenario is tested and verified.

Update: You must read this documentation: http://www.thekspace.com/home/component/content/article/62-uilocalnotification-demystified.html

Upvotes: 5

Om Prakash
Om Prakash

Reputation: 9471

Try this code:

NSDate *pickerDate = [date_picker date];
// Schedule the notification
UILocalNotification* localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = pickerDate;
localNotification.alertBody = @"Alert Body Message";
localNotification.alertAction = @"Alert Action Message";
localNotification.timeZone = [NSTimeZone defaultTimeZone];
[[UIApplication sharedApplication]     scheduleLocalNotification:localNotification];

and use this code in didFinishLaunchingWithOptions in App delegate class.

    UILocalNotification *locationNotification = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
if (locationNotification) {
    // Set icon badge number to zero
    application.applicationIconBadgeNumber = 0;
}

Upvotes: 2

Related Questions