【问题标题】:userNotificationCenter didReceive is not being called when tapping a notification点击通知时未调用 userNotificationCenter didReceive
【发布时间】:2020-06-04 23:29:56
【问题描述】:

我设置了以下本地通知:

    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 进行部署。

【问题讨论】:

    标签: ios swift unusernotificationcenter


    【解决方案1】:

    我不确定发生了什么,因为我必须看到完整的代码,所以我只是做了一个小例子让你理解。它在模拟器中也能正常工作。

    如果您有任何问题,请随时告诉我!

    首先确保您在设置您的委托或安排任何本地通知之前请求用户许可,否则您的通知将静默失败。

    UNUserNotificationCenter.current().requestAuthorization(options: [.badge, .alert, .sound]) { (isAuthorized, error) in
        // ...
    }
    

    完成请求授权后(状态为granted),只需设置您的委托:

    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()
        }
    }
    

    【讨论】:

    • 我忘记设置代理了。非常感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多