我有一个Firebase监听器,当它像这样触发时启动通知:
chat.ref.child("lastMessage").observe(.value, with: { snapshot in
let lastMessage = snapshot.value as! String
chat.lastMessage = lastMessage
self.tableView.reloadData()
chat.lastMessageText = "New Message!"
let content = UNMutableNotificationContent()
content.title = "You have a new Message!"
content.subtitle = chat.title
content.body = chat.lastMessage
content.badge = 1
let trigger = UNTimeIntervalNotificationTrigger (timeInterval: 1, repeats: false)
let request = UNNotificationRequest(identifier: "timerDone", content: content, trigger: trigger)
UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)
}
还有:
override func viewDidLoad() {
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge], completionHandler: {didAllow, error in })
}
我遇到的问题是,虽然HomeScreen通知在模拟器中工作,但它们没有在设备上显示。所有权限都在设备上授予。在这里阅读一些问题我发现了一些答案,建议我在app delegate中添加以下内容:
application.beginBackgroundTask(withName: "showNotification", expirationHandler: nil)
但这实际上也禁用了模拟器上的通知。通知确实在设备上工作但突然停止了。有什么想法吗?