![](/img/trans.png)
[英]userNotificationCenter(_:didReceive:withCompletionHandler:) not be called when a notification action is selected
[英]userNotificationCenter didReceive is not being called when tapping a notification
我設置了以下本地通知:
let content = UNMutableNotificationContent()
content.title = "Some Title"
content.body = "Some text"
content.sound = UNNotificationSound.default
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 20, repeats: false)
let request = UNNotificationRequest(identifier: "OneDay", content: content, trigger: trigger)
notificationCenter.add(request)
我已經將以下內容添加到 AppDelegate
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
print("Notification Tapped")
if response.notification.request.identifier == "OneDay" {
print("OneDay Notification Tapped")
}
completionHandler()
}
notificationCenter 已設置為:
let notificationCenter = UNUserNotificationCenter.current()
但是,以上打印語句均無效。 顯示通知,我點擊它,應用程序被帶到前台,但沒有任何內容打印到控制台。
我在這里錯過了什么嗎? 我已經在模擬器和設備中進行了測試。 同樣的結果。
我正在使用 XCode 11.5,為 12.0 進行部署。
我不確定發生了什么,因為我必須看到完整的代碼,所以我只是做了一個最小的例子讓你理解。 它在模擬器中也可以正常工作。
如果您有任何問題,請隨時告訴我!
首先確保您在設置委托或安排任何本地通知之前請求用戶許可,否則您的通知將靜默失敗。
UNUserNotificationCenter.current().requestAuthorization(options: [.badge, .alert, .sound]) { (isAuthorized, error) in
// ...
}
完成請求授權(並且狀態已授予)后,只需設置您的委托:
UNUserNotificationCenter.current().delegate = self
創建您的通知:
let content = UNMutableNotificationContent()
content.title = "Title"
content.subtitle = "Subtitle"
content.body = "Body"
content.sound = UNNotificationSound.default
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 5, repeats: false)
let request = UNNotificationRequest(identifier: "OneDay", content: content, trigger: trigger)
UNUserNotificationCenter.current().add(request) { error in
guard error == nil else {
return
}
// ...
}
您的委托方法將按預期調用:
extension AppDelegate: UNUserNotificationCenterDelegate {
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
// ...
completionHandler([.alert, .badge, .sound])
}
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
if response.notification.request.identifier == "OneDay" {
// ...
}
completionHandler()
}
}
添加到@user13639548 答案:
確保每次重新啟動應用程序時都注冊當前的 UNUserNotificationCenter.current().delegate。
if UserStorage.shared.pushNotificationsToken == nil {
SettingsHelper.getUserPushNotificationStatus(completion: self.processPushNotificationStatus(status:))
} else {
UIApplication.shared.appDelegate?.setAppDelegateAsPushNotificationDelegate()
}
}
這是結果調用:
func setAppDelegateAsPushNotificationDelegate() {
UNUserNotificationCenter.current().delegate = self
}
當用戶應用程序進入后台並點擊通知時。
然后調用生命周期的'applicationWillEnterForeground'方法。
這段代碼對我有用。
func applicationWillEnterForeground(_ application: UIApplication) {
let center = UNUserNotificationCenter.current()
center.delegate = self
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.