ios - UILocalNotifications 不会出现在锁定屏幕上

标签 ios iphone swift uilocalnotification

我在 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/

相关文章:

iphone - Cocos2D旋转和 anchor

iphone - 在UIWebview中加载gif图像-iPhone

iphone - 完善用户界面

ios - 如何在 swift NSMutableArray 中基于其他元素获取一个元素的所有值

ios - Swift NSXMLParserDelegate 不会被调用

ios - 使用 block 在表中加载图像-dispatch_async返回值

ios - 在 UIView 上快速绘制 UIImage 图像

iphone - 在 iOS 中,如何获取以特定字体和点大小呈现的字符串的宽度?

ios - Swift:如何重用 View Controller

ios - 手势结束后,实例变量将重置为零