Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between presentLocalNotificationNow and scheduleLocalNotification

What is the difference between presentLocalNotificationNow and scheduleLocalNotification.

For the both following function is showing notification after 1 second

-(void)showLocalNotification:(NSNotification *)notification {

    NSString *msg = @"test message";

    [[UIApplication sharedApplication] cancelAllLocalNotifications];

    UILocalNotification *_localNotification = [[UILocalNotification alloc]init];

    _localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:1];

    _localNotification.timeZone = [NSTimeZone defaultTimeZone];

    _localNotification.alertBody = msg;

    _localNotification.soundName = UILocalNotificationDefaultSoundName;

    _localNotification.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber]+1;

    [[UIApplication sharedApplication] scheduleLocalNotification:_localNotification];

    // or 

    //[[UIApplication sharedApplication] presentLocalNotificationNow:_localNotification];
}
like image 493
MaxEcho Avatar asked Jun 10 '26 02:06

MaxEcho


1 Answers

If the application is running in the background, the local notification will not get an alert or sound, as it is directly received by the 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];
    }
}

From Apple Documentation:

Once you have created an instance of UILocalNotification, you schedule it using one of two methods of the UIApplication class: scheduleLocalNotification: or presentLocalNotificationNow:. The former method use the fire date to schedule delivery; the latter method presents the notification immediately, regardless of the value of fireDate. You can cancel specific or all local notifications by calling cancelLocalNotification: or cancelAllLocalNotifications, respectively.

like image 54
Nikos M. Avatar answered Jun 11 '26 18:06

Nikos M.



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!