[英]UNUserNotificationCenter didReceive response not called when app is terminated
[英]UNUserNotificationCenter don't work when app is killed
我只是為iOs 10和其他通知更改了:
if #available(iOS 10.0, *) {
let center = UNUserNotificationCenter.current()
center.requestAuthorization(options: [.alert, .sound]) { (granted, error) in
let content = UNMutableNotificationContent()
content.body = notifMessage!
content.sound = UNNotificationSound.default()
// Deliver the notification in five seconds.
let trigger = UNTimeIntervalNotificationTrigger.init(timeInterval: 5, repeats: false)
let request = UNNotificationRequest.init(identifier: "Upload", content: content, trigger: trigger)
// Schedule the notification.
let center = UNUserNotificationCenter.current()
center.add(request)
}
} else {
let notification = UILocalNotification()
notification.alertBody = notifMessage
notification.fireDate = NSDate() as Date
notification.soundName = UILocalNotificationDefaultSoundName
UIApplication.shared.scheduleLocalNotification(notification)
}
通過使用USB連接設備在設備上運行應用程序時,它可以運行,但僅當應用程序在后台運行時,它在以下情況下不起作用:
我殺了這個應用程序
顯示應用程序時
如果您殺死該應用程序(通過雙擊主屏幕按鈕然后向上滑動),則不僅會終止該應用程序,還會禁止該應用程序進行進一步的后台操作(直到用戶再次啟動它)。 您只需要按主頁按鈕,即可通過正常的內存恢復過程將應用拋棄。 或者,出於測試目的,您可以以編程方式使應用程序崩潰。 但是您不能使用跳板(雙擊主屏幕按鈕的技巧),因為它會影響應用程序允許的背景模式。
關於顯示應用程序時的通知,如果用戶點擊通知,還是用戶手動啟動應用程序而忽略了通知,則所有通知都以不同的方式傳達給應用程序。 請參閱UIApplicationDelegate
文檔的“ 響應通知和事件”部分。 或者,有關后台操作的常規信息,請參見《 iOS應用程序編程指南:后台執行 》。
您的代碼中有幾個錯誤:1.通知標題丟失。 您已將內容和聲音添加到內容中,但是標題丟失。 標題是必須的,如果不添加標題,則不會顯示通知。
content.title = "Some Title"
init
進行初始化。 這些功能可以重寫為: let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 5, repeats: false)
let request = UNNotificationRequest.init(identifier: "Upload", content: content, trigger: trigger)
let request = UNNotificationRequest(identifier: some_value, content: content, trigger: trigger)
希望這可以幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.