gpt4 book ai didi

ios - presentLocalNotificationNow 和 scheduleLocalNotification 的区别

转载 作者:搜寻专家 更新时间:2023-10-30 20:18:35 27 4
gpt4 key购买 nike

presentLocalNotificationNowscheduleLocalNotification 有什么区别。

因为以下两个函数都是在 1 秒后显示通知

-(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];
}

最佳答案

如果应用程序在后台运行,本地通知将不会发出警报或声音,因为它直接由应用程序接收。在这种情况下,您需要使用 presentLocalNotificationNow 显示通知。

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
UIApplicationState applicationState = application.applicationState;
if (applicationState == UIApplicationStateBackground) {
[application presentLocalNotificationNow:notification];
}
}

来自 Apple 文档:

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.

关于ios - presentLocalNotificationNow 和 scheduleLocalNotification 的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19751536/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com