【问题标题】:UILocalNotifications don't appear on locked screenUILocalNotifications 不会出现在锁定的屏幕上
【发布时间】:2016-05-23 02:31:20
【问题描述】:

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

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

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

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

【问题讨论】:

    标签: ios iphone swift uilocalnotification


    【解决方案1】:

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

    斯威夫特 3

    先添加import UserNotifications,然后在didFinishLaunchingWithOptions

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

    【讨论】:

    • Set(arrayLiteral: ) 初始化器不打算直接使用,它打算与数组文字一起使用。你可以写[notificationCategory]。无论如何,由于 Swift 3,它现在几乎无关紧要:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-06-17
    • 1970-01-01
    • 1970-01-01
    • 2014-07-20
    • 2013-12-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多