首页 > 解决方案 > 如何使用 AWS 推送通知服务修复未注册或过期令牌错误

问题描述

我正在尝试将 AWS 推送通知服务与 IOS 平台集成,但它在从控制台发送通知时给了我未注册或过期的令牌错误。

我已经通过显示活动状态的亚马逊 API 注册了设备。遵循创建 SSL 证书、设置 AWS 开发工具包的所有步骤,并尝试生成不同的生产证书和沙盒证书。

var pinpoint: AWSPinpoint?

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        
        registerForPushNotifications()
        
        /** start code copy **/
        // Create AWSMobileClient to connect with AWS
        AWSMobileClient.sharedInstance().initialize { (userState, error) in
            if let error = error {
                print("Error initializing AWSMobileClient: \(error.localizedDescription)")
            } else if let userState = userState {
                print("AWSMobileClient initialized. Current UserState: \(userState.rawValue)")
            }
        }
        
        // Initialize Pinpoint
        let pinpointConfiguration = AWSPinpointConfiguration.defaultPinpointConfiguration(launchOptions: launchOptions)
        pinpoint = AWSPinpoint(configuration: pinpointConfiguration)
        /** end code copy **/
        
        
        
        
        return true
    }

//FOR REGISTERING PUSH NOTIFICATIONS
  func registerForPushNotifications() {
        UNUserNotificationCenter.current().delegate = self
        UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) {
            (granted, error) in
            print("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()
            }
        }
    }

//GENERATED TOKEN
    pinpoint!.notificationManager.interceptDidRegisterForRemoteNotifications(
            withDeviceToken: deviceToken)
        
        let deviceToken = deviceToken.map { String(format: "%02.2hhx", $0) }.joined()
        print("Token: \(deviceToken)")

完成所有步骤后,我得到未注册或过期的令牌错误。

请求 ID 640:{"errorMessage":"未注册或过期令牌","channelType":"APNS","pushProviderStatusCode":"400","pushProviderError":"BadDeviceToken","pushProviderResponse":"{"reason": "BadDeviceToken"}"}

标签: iosamazon-web-servicespush-notification

解决方案


推荐阅读