所以我一直在尝试向新的 UNUserNotificationCenter 添加通知,但我似乎没有得到它。
我的视图控制器有一个动作:
@IBAction func sendPressed(_ sender: AnyObject) {
let content = UNMutableNotificationContent()
content.title = "Hello"
content.body = "What up?"
content.sound = UNNotificationSound.default()
// Deliver the notification in five seconds.
let trigger = UNTimeIntervalNotificationTrigger.init(timeInterval: 5, repeats: false)
let request = UNNotificationRequest.init(identifier: "FiveSecond", content: content, trigger: trigger)
// Schedule the notification.
let center = UNUserNotificationCenter.current()
center.add(request) { (error) in
print(error)
}
print("should have been added")
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
let center = UNUserNotificationCenter.current()
center.requestAuthorization([.alert, .sound]) { (granted, error) in
}
}
而且我Notification Content Extension
在项目中也有一个,但它似乎根本没有被触发,我缺少什么想法吗?我正在尝试用户文档中的示例,但它并没有告诉我更多信息,或者我错过了它。
这里:https ://developer.apple.com/reference/usernotifications/unmutablenotificationcontent
另外: https ://developer.apple.com/reference/usernotificationsui https://developer.apple.com/reference/usernotifications
编辑:
所以把应用程序放在后台就可以了。