swift 点击通知时未调用userNotificationCenter didReceive

g2ieeal7  于 2023-01-19  发布在  Swift
关注(0)|答案(3)|浏览(228)

我设置了以下本地通知:

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

通知中心已设置为:

let notificationCenter = UNUserNotificationCenter.current()

然而,上面的打印语句都不起作用。通知呈现出来,我点击它,应用程序被带到前台,但没有任何东西打印到控制台。
我错过什么了吗?我在模拟器和设备上都测试过了。结果一样。
我使用的是XCode 11.5,正在为12.0进行部署。

8zzbczxx

8zzbczxx1#

我不知道是怎么回事,因为我必须看到完整的代码,所以我只是做了一个最小的例子让你理解。它也工作在模拟器完美的罚款。
如果您有任何问题,请随时告诉我!
在设置代理人或安排任何本地通知**之前,请首先确保您请求了用户权限,否则您的通知将自动失败。

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

wrrgggsh2#

添加到@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
}
zqdjd7g9

zqdjd7g93#

当用户应用程序进入后台并点击通知时。
则调用生命周期的方法“applicationWillEnterForeground”。
这个密码对我有用。

func applicationWillEnterForeground(_ application: UIApplication) {
    let center = UNUserNotificationCenter.current()
    center.delegate = self
}

相关问题