iOS 10 UNNotification不显示警报

时间:2018-07-11 13:36:34

标签: xcode ios10 ios11 unnotificationrequest

代码非常简单:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge], completionHandler: { (granted, error) in
        if !granted {
            print("Not allowed")
        }
    })

    let content = UNMutableNotificationContent()
    content.title = "Alert"
    content.sound = UNNotificationSound.default()

    let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 20, repeats: false)

    let request = UNNotificationRequest(identifier: "test", content: content, trigger: trigger)

    UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)
    return true
}

它在iOS 11上运行良好,如下所示: enter image description here

但是在iOS 10上,警报不会显示。

在iOS 10和iOS 11上,声音确实出现了。

我的Xcode版本是9.2(9C40b)

感谢您的帮助。

1 个答案:

答案 0 :(得分:2)

尝试添加这样的通知正文

content.body = "Any text/Blank Space"

希望这对您有帮助