gpt4 book ai didi

swift - 应用程序未运行时的本地通知

转载 作者:搜寻专家 更新时间:2023-10-30 22:04:48 25 4
gpt4 key购买 nike

是否可以显示来自应用程序的某种本地通知,即使该应用程序未运行(也不在后台)?

例如每日提醒之类的。我知道推送通知是可行的,但它不适合我的应用。

最佳答案

您可以轻松安排本地通知,无论应用处于何种状态,它们都会在安排的日期和时间显示。

首先,您需要获得用户的许可才能显示通知,如下所示:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
application.registerUserNotificationSettings(UIUserNotificationSettings(forTypes: UIUserNotificationType.Sound|UIUserNotificationType.Alert | UIUserNotificationType.Badge, categories: nil))
return true
}

然后像这样创建通知:

var localNotification:UILocalNotification = UILocalNotification()
localNotification.alertAction = "This is"
localNotification.alertBody = "A notification"
localNotification.fireDate = NSDate(timeIntervalSinceNow: 15)
UIApplication.sharedApplication().scheduleLocalNotification(localNotification)

看看 Local and Remote Notification Programming Guide .

关于swift - 应用程序未运行时的本地通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27732784/

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