Swift 应用程序未运行时的本地通知
即使应用程序未运行(也不在后台),是否可以显示来自应用程序的某种本地通知Swift 应用程序未运行时的本地通知,swift,notifications,uilocalnotification,Swift,Notifications,Uilocalnotification,即使应用程序未运行(也不在后台),是否可以显示来自应用程序的某种本地通知 例如,每日提醒或类似的事情。我知道推送通知是可能的,但它不适合我的应用程序。您可以轻松安排本地通知,无论应用程序的状态如何,它们都将在计划的日期和时间显示 首先,您需要获得用户提供通知的权限,如下所示: func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]
例如,每日提醒或类似的事情。我知道推送通知是可能的,但它不适合我的应用程序。您可以轻松安排本地通知,无论应用程序的状态如何,它们都将在计划的日期和时间显示 首先,您需要获得用户提供通知的权限,如下所示:
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)
然后按如下方式创建通知:
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;
}
推送通知需要付款才能工作。我认为最好的选择是后台运行threadWell,但如果用户停止应用程序,后台线程也会被杀死,对吗?@Enriquequequero你说的付款是什么意思?这是一个很棒的答案,解决了我的问题。工作起来很有魅力。已被弃用。请查看使用的本地通知。