在代码中,我尝试这样做:
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内部。但我没看到任何当地通知。为什么?
发布于 2018-09-21 08:35:40
你的代码运行良好。我想问题是,当您收到通知时,应用程序是活动的。只有当应用程序不活动时,iOS才会显示系统通知。如果app在通知触发时处于活动状态,系统将触发UNUserNotificationCenterDelegate方法,以便您可以自己处理通知。
因此,由于您设置了一个延迟为10秒的通知时间,所以您需要运行应用程序,然后关闭它并等待10秒。如果您给予应用程序这样的许可,应该会出现通知。
https://stackoverflow.com/questions/52438828
复制相似问题