我在 iPhone 上运行了我的应用程序。 (只需连接我的手机并选择我的 iPhone 作为目标。)我在我的应用程序中构建了本地通知,这些通知已安排好并准时完美地显示在 iPhone 上。但是,即使在“设置”->“通知”->“[我的应用程序]”->“在锁定屏幕上显示”下打开,它们也不会出现在锁定屏幕上。
奇怪的是它们甚至出现在通知中心。
这类似于 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.")
}
}
关于ios - UILocalNotifications 不会出现在锁定屏幕上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37381817/