【问题标题】:Push notifications and token device in SwiftSwift 中的推送通知和令牌设备
【发布时间】:2018-09-06 13:42:57
【问题描述】:

我正在开发一个需要知道设备令牌以在用户授权时向用户发送通知的应用程序。 系统第一次请求授权通知。如果用户说“允许”,系统会为我调用方法 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 未被调用,因此我无法存储令牌。询问用户授权的警报仅在用户第一次使用打开应用程序时显示。 下次用户打开应用程序时,系统不再显示警报。 我该如何解决这个问题?

谢谢。

【问题讨论】:

  • 您可以在每次应用启动时注册推送通知。您不需要用户权限即可注册推送通知,但没有权限的通知将不会显示。您可以在未经许可的情况下接收静默推送。

标签: ios swift apple-push-notifications devicetoken


【解决方案1】:

我是这样解决这个问题的: 在 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
}

因此,当我检查用户权限时,我发现用户已启用(从设备设置)推送通知案例。授权系统调用正确的方法,我可以将设备令牌存储在用户首选项中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-11-23
    • 1970-01-01
    • 1970-01-01
    • 2011-12-30
    • 2012-02-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多