应用未运行时的本地通知

Chr*_*örz 6 notifications uilocalnotification swift

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

例如每日提醒或类似的东西.我知道推送通知是可能的,但它不适合我的应用程序.

Emi*_*mil 16

您可以轻松安排本地通知,无论应用程序的状态如何,它们都将在预定的日期和时间显示.

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

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

然后你创建这样的通知:

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

查看本地和远程通知编程指南.