gpt4 book ai didi

ios - userInfo 通过在应用程序关闭时单击通知

转载 作者:搜寻专家 更新时间:2023-10-31 23:07:30 26 4
gpt4 key购买 nike

我正在做一个安排本地通知并保存用户信息的应用程序。这部分没问题。

但是当应用程序关闭时,如果出现 Notification 并且用户点击,则不会调用该方法,我无法处理 userInfo。

我看到有一种新方法可以使用 UNUserNotificationCenter 接收通知。但是也不起作用。

我已经试过了,但是没有成功:

func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {

let userInfo = response.notification.request.content.userInfo
if let yourData = userInfo["yourKey"] as? String {
// Handle your data here, pass it to a view controller etc.
}
}

这是我在 AppDelegate 中的实现:

@available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {

let lNotification = UILocalNotification()
lNotification.userInfo = response.notification.request.content.userInfo
// Handle your data here, pass it to a view controller etc.

}

有人帮我吗?我在这里看到了所有相关的问题,但没有找到任何东西。

最佳答案

您是否注册了通知?如果没有,将其添加到 AppDelegate didFinishLaunchingWithOptions:

// Register Notifications
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge], completionHandler: { granted, error in

if granted {
print("User notifications are allowed")
} else {
print("User notifications are NOT allowed")
}
})
UNUserNotificationCenter.current().delegate = self

关于ios - userInfo 通过在应用程序关闭时单击通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49518950/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com