简体   繁体   English

UNUserNotification不会在iOS 12下触发我的本地通知

[英]UNUserNotification doesnt fire out my local notification under iOS 12

In code I try to do it like this: 在代码中,我尝试这样做:

import UserNotifications

if #available(iOS 10.0, *) {
        let center = UNUserNotificationCenter.current()
        center.requestAuthorization(options: [.alert, .sound]) { _, _ in }
        let content = UNMutableNotificationContent()
        content.title = "Hello Staff"
        content.body = "Customer just ordered a milk with croissant"
        content.sound = UNNotificationSound.default()
        let date = Calendar.current.dateComponents([.year, .month, .day, .hour, .minute, .second], from: Date(timeIntervalSinceNow: 10))
        let trigger = UNCalendarNotificationTrigger(dateMatching: date, repeats: false)
        let request = UNNotificationRequest(identifier: "abcde", content: content, trigger: trigger)
        center.add(request)
}

All happens within AppDelegate . 所有这些都在AppDelegate But I cant see any local notification. 但是我看不到任何本地通知。 Why? 为什么?

Your code is working fine. 您的代码运行正常。 I guess the problem is that your app is active when you receive the notifications. 我想问题是当您收到通知时您的应用程序处于活动状态。 iOS shows system notifications only if the app is not active. 仅当该应用程序未激活时, iOS才会显示系统通知。 If app is active when notifications fires, the system triggers the UNUserNotificationCenterDelegate method so you could handle the notification by yourself. 如果在触发通知时应用程序处于活动状态,则系统会触发UNUserNotificationCenterDelegate方法,以便您可以UNUserNotificationCenterDelegate处理通知。

So since you set a notification time with a delay for 10 seconds, you need to run your app, then close it and wait for 10 seconds. 因此,由于您将通知时间设置为延迟10秒,因此您需要运行您的应用,然后将其关闭并等待10秒。 Notification should appear if you give your app such a permission. 如果您授予您的应用这样的许可,则应该显示通知。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM