在用户授予接收推送通知的权限时未调用 didRegisterForRemoteNotificationsWithDeviceToken

Posted

技术标签:

【中文标题】在用户授予接收推送通知的权限时未调用 didRegisterForRemoteNotificationsWithDeviceToken【英文标题】:didRegisterForRemoteNotificationsWithDeviceToken not called upon user granting permission to receive push notifications 【发布时间】:2020-03-26 07:41:37 【问题描述】:

我在 AppDelegate 中设置了 didRegisterForRemoteNotificationsWithDeviceToken,如下所示:

func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) 
        let token = deviceToken.map  String(format:"%02.2hhx", $0) .joined()
        print("didRegisterForRemoteNotificationsWithDeviceToken got called - Token is: \(token)")
        // delegate might get called even before an authtoken has been set for the user. Return in such cases:
        guard UserDefaults.standard.string(forKey: "authtoken") != nil else return

        // otherwise continue:
        if (token != UserDefaults.standard.string(forKey: "apnsToken")) 
            self.apiService.setAPNSToken(apnsToken: token, completion: result in
                switch result 
                case .success(let resultString):
                    DispatchQueue.main.async 
                        UserDefaults.standard.set(token, forKey: "apnsToken")
                        print(resultString, " Token is: \(token)")
                    
                case .failure(let error):
                    print("An error occured \(error.localizedDescription)")
                
            )
         else 
            print("User is registered for Push Notifications. Token did not change and is: \(token)")
        

    

我要求用户允许在我的一个视图控制器中发送推送通知,如下所示:

UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .badge, .sound])  granted, error in
            DispatchQueue.main.async 
                if (granted) 
                    UserDefaults.standard.set(true, forKey: "pushNotificationsEnabled")
                
                print("permission granted?: \(granted)")
            
        

我想都是很标准的。我的困惑/问题是这样的: didRegisterForRemoteNotificationsWithDeviceToken 不会在用户交互时被调用 - 即我希望用户点击“允许推送通知”时立即调用它,以便将 apns 令牌存储在我的后端。但事实并非如此。 当我关闭应用程序并重新启动它时,didRegisterForRemoteNotificationsWithDeviceToken 被调用并且令牌被存储在后端。

在用户点击“允许推送通知”后,我需要做什么才能访问令牌并将其存储在后端?

【问题讨论】:

【参考方案1】:

当您的用户确认他们需要通知时,您应该致电UIApplication.shared.registerForRemoteNotifications()。你好像没有这样做。

你可以这样做

UNUserNotificationCenter.current()
  .requestAuthorization(options: [.alert, .sound, .badge]) 
    [weak self] granted, error in

    print("Permission granted: \(granted)")
    guard granted else  return 
    self?.getNotificationSettings()

然后getNotificationSettings 在注册远程通知之前检查它是否已被授权。

func getNotificationSettings() 
  UNUserNotificationCenter.current().getNotificationSettings  settings in
    print("Notification settings: \(settings)")

    guard settings.authorizationStatus == .authorized else  return 

    DispatchQueue.main.async 
       UIApplication.shared.registerForRemoteNotifications()
    
  

有关如何处理推送通知的更多信息,请查看Ray Wenderlich tutorial。

【讨论】:

以上是关于在用户授予接收推送通知的权限时未调用 didRegisterForRemoteNotificationsWithDeviceToken的主要内容,如果未能解决你的问题,请参考以下文章

iOS 推送通知自定义警报

应用程序在前台时未收到推送通知

应用程序处于前台时未收到 Firebase 推送通知

通过 Spring security ldap 对用户进行身份验证时未授予任何权限错误

企业应用程序处于生产状态时未收到推送通知

网络推送通知未显示