UILocalNotifications 不会出现在锁定的屏幕上

UILocalNotifications don't appear on locked screen

我 运行 我的应用程序在我的 iphone 上。 (只需连接我的 phone 并选择我的 iPhone 作为目标。)我在我的应用程序中构建了本地通知,这些通知已安排好并准时出现在 iPhone 上。但是,即使在设置 -> 通知 -> [我的应用] -> 在锁定屏幕上显示已打开,它们也不会出现在锁定屏幕上。

什么是 st运行ge 是它们甚至出现在通知中心。

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

有人遇到过这种情况吗?我认为这可能是一个 iOS 错误。我使用的是迄今为止最新的 iOS、Xcode、OS X。

您必须获得许可才能在锁定屏幕上显示通知!一旦查看 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.")
            }
        }