gpt4 book ai didi

ios - Swift 中的推送通知和 token 设备

转载 作者:行者123 更新时间:2023-11-30 11:10:40 24 4
gpt4 key购买 nike

我正在开发一个应用程序,它需要知道设备 token ,以便在用户授权时向用户发送通知。系统第一次请求授权通知。如果用户说“允许”,系统会为我调用方法 didRegisterForRemoteNotificationsWithDeviceToken,并在此方法的主体中,我在 UserDefaults 中写入设备 token 。这是流程:1)系统请求许可2)在 didFinishLaunchingWithOptions (AppDelegate 内部)中,我在管理通知框架的类中调用此方法:

   func registerForPushNotifications() {
UNUserNotificationCenter.current().delegate = self
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) {
(granted, error) in
print("NFM permission granted: \(granted)")
// 1. Check if permission granted
guard granted else { return }
// 2. Attempt registration for remote notifications on the main thread
DispatchQueue.main.async {
UIApplication.shared.registerForRemoteNotifications()
}
}
}

3)系统为我调用方法 didRegisterForRemoteNotificationsWithDeviceToken (在 AppDelegate 中),一切正常。在方法的主体中,我编写了 token UserDefaults 设置。第二种情况发生在用户第一次拒绝权限时。在第二种情况下,在我的应用程序的另一个 ViewController 中,我检查用户是否已使用设备设置授予推送通知权限(因此在应用程序之外)。在我的应用程序的特定部分中,我使用此方法来验证用户是否已授予授权(该方法始终在我的管理通知框架的类中)。

    func checkPremissionStatus() -> Bool{
var isAuth = false
UNUserNotificationCenter.current().delegate = self
UNUserNotificationCenter.current().getNotificationSettings { (settings) in
print("NFM checkPremissionStatus -> notification status")
switch settings.authorizationStatus {
case .authorized:
print("NFM checkPremissionStatus -> authorized status")
self.autorizzato = true
isAuth = true
//TODO check pending notification...
case .denied:
print("NFM checkPremissionStatus -> denied status")
self.autorizzato = false
isAuth = false
case .notDetermined:
print("NFM notDetermined never see this print")
}
}
return isAuth
}

在这种特定情况下,方法 didRegisterForRemoteNotificationsWithDeviceToken 不会被调用,因此我无法存储 token 。仅在用户第一次打开应用程序时才会显示询问用户授权的警报。下次用户打开应用程序时,系统将不再显示警报。我该如何解决这个问题?

谢谢。

最佳答案

checkPremissionStatus 中,我添加了对方法 registerForPushNotifications 的调用,这样系统就会调用方法 didRegisterForRemoteNotificationsWithDeviceToken,以便我可以使用该设备我的应用程序的业务逻辑中的 token 。

这是在管理通知框架的类中修改的代码。

func checkPremissionStatus() -> Bool{
var isAuth = false
UNUserNotificationCenter.current().delegate = self
UNUserNotificationCenter.current().getNotificationSettings { (settings) in
print("NFM checkPremissionStatus -> notification status")
switch settings.authorizationStatus {
case .authorized:
print("NFM checkPremissionStatus -> authorized status")
self.autorizzato = true
isAuth = true
print("NFM checkPremissionStatus -> call registerForPushNotification")
self.registerForPushNotifications()
//TODO check pending notification...
case .denied:
print("NFM checkPremissionStatus -> denied status")
self.autorizzato = false
isAuth = false
case .notDetermined:
print("NFM notDetermined never see this print")
}
}
return isAuth
}

因此,当我检查用户权限并发现用户已启用(从设备设置)推送通知 case .authotrized 系统调用正确的方法,我可以存储设备用户首选项中的 token 。

关于ios - Swift 中的推送通知和 token 设备,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52205466/

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