ios Swift中的推送通知和令牌设备

8yparm6h  于 2023-06-07  发布在  iOS
关注(0)|答案(1)|浏览(203)

我正在开发一个应用程序,需要知道设备令牌,以便在给予时向用户发送通知。系统第一次要求授权通知。如果用户说“允许”,系统会为我调用方法didRegisterForRemoteNotificationsWithDeviceToken,并在此方法的主体中写入UserDefaults设备令牌。这就是流程: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中),一切正常。在方法的主体中,我编写了令牌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方法没有被调用,所以我不能存储令牌。仅在用户第一次使用打开应用程序时显示向用户询问授权的警报。下次用户打开应用程序时,系统不再显示警报。我该如何解决这个问题?
谢谢

ep6jt1vc

ep6jt1vc1#

checkPremissionStatus中,我添加了一个对我的方法registerForPushNotifications的调用,这样系统就调用了方法didRegisterForRemoteNotificationsWithDeviceToken,这样我就可以在应用的业务逻辑中使用设备令牌。
这是在管理通知框架的类中修改的代码。

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时,系统调用正确的方法,我可以将设备令牌存储在用户首选项中。

相关问题