在代码中我尝试这样做:
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)
}
所有发生在 AppDelegate
中。但是我看不到任何本地通知。为什么?
最佳答案
您的代码运行良好。我想问题是当您收到通知时您的应用程序处于事件状态。 iOS
仅在应用未激活时显示系统通知。如果通知触发时应用处于事件状态,系统会触发 UNUserNotificationCenterDelegate
方法,以便您可以自己处理通知。
因此,由于您设置了延迟 10
秒的通知时间,因此您需要运行您的应用,然后关闭它并等待 10
秒。如果您向您的应用授予此类权限,则应该会出现通知。
关于ios - UNUserNotification 不会触发我在 iOS 12 下的本地通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52438828/