应用程序未运行时的本地通知
Local notification while app not running
是否可以显示来自应用程序的某种本地通知,即使该应用程序未运行(也不在后台)?
例如每日提醒之类的。我知道推送通知是可行的,但它不适合我的应用程序。
您可以轻松安排本地通知,它们将在安排的日期和时间呈现,而不管应用程序的状态如何。
首先您需要获得用户的许可才能显示通知,如下所示:
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)
在 AppDelegate 中,改用此函数
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
let settings = UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: nil)
application.registerUserNotificationSettings(settings)
return true;
}
是否可以显示来自应用程序的某种本地通知,即使该应用程序未运行(也不在后台)?
例如每日提醒之类的。我知道推送通知是可行的,但它不适合我的应用程序。
您可以轻松安排本地通知,它们将在安排的日期和时间呈现,而不管应用程序的状态如何。
首先您需要获得用户的许可才能显示通知,如下所示:
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)
在 AppDelegate 中,改用此函数
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
let settings = UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: nil)
application.registerUserNotificationSettings(settings)
return true;
}