UILocalNotifications未显示在锁定的屏幕上

时间:2016-05-23 02:31:20

标签: ios iphone swift uilocalnotification

我在iphone上运行了我的应用程序。 (只需连接我的手机,然后选择我的iPhone作为目标。)我在我的应用程序中构建了本地通知,这些通知按计划在iPhone上按时完成。但是,即使在“设置”下,它们也不会出现在锁定的屏幕上。通知 - > [我的应用] - >锁定屏幕上的显示已打开。

奇怪的是,它们甚至出现在通知中心。

这与UILocalNotification not showing in the lock screen类似,但我也没有找到答案。

有人遇到过这个吗?我在想它可能是一个iOS错误。我到目前为止使用最新的iOS,Xcode,OS X.

1 个答案:

答案 0 :(得分:3)

您必须获得在锁定屏幕上显示通知的权限!一看Appdelegate.swift中的代码

 func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    // Override point for customization after application launch.
    let notificationCategory = UIMutableUserNotificationCategory()
    let categories = Set<UIUserNotificationCategory>(arrayLiteral: notificationCategory)
    let settings = UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: categories)
    application.registerUserNotificationSettings(settings)
    return true
}

Swift 3

首先添加 import UserNotifications ,然后在 didFinishLaunchingWithOptions

中添加代码
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound , .badge]) {(accepted, error) in
            if !accepted {
                print("Notification access denied.")
            }
        }
相关问题