GCM IOS 未注册问题

Posted

技术标签:

【中文标题】GCM IOS 未注册问题【英文标题】:GCM IOS NotRegistered issue 【发布时间】:2015-10-23 06:59:31 【问题描述】:

我已经使用 GCM 很长时间了。有一天,它突然坏了。问题是我发送的第一次推送我得到了成功状态,但应用程序没有收到任何推送。我发送的第二次推送失败,出现 NotRegistered 错误。我重新安装应用程序:成功(未收到通知),失败(未注册)-> 循环。我不知道发生了什么变化。谷歌支持非常无用,无论是 GCM 问题、APNs 问题还是客户端问题,都需要花费大量时间来回答简单的问题。如果以前有人遇到过此类问题,请告诉我要查找的内容。就是这个样子:

我怀疑它是在更新到 ios 9 之后发生的。不过我不确定。如果新 iOS 中的某些东西可能会阻止 GCM,如果有人指出,我将不胜感激。

更新:

GCM push fails with NotRegistered

那家伙也有类似的问题。问题出在一些清单文件上。我需要为 iOS 9 添加 Info.plist 中的一些条目以允许 GCM 吗?

更新:

每次应用启动时都会调用 onTokenRefresh。不过,我得到了相同的令牌。所以我怀疑 GCM 服务器上的令牌有问题。

APPDELEGATE 中的 GCM 代理代码:

var connectedToGCM = false


private var deviceToken: NSData?

var gcmSenderID: String!
let authorizedEntity = "my GCM Sender_ID"


public func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool 
    // Override point for customization after application launch.
    var configureError:NSError?
    GGLContext.sharedInstance().configureWithError(&configureError)
    assert(configureError == nil, "Error configuring Google services: \(configureError)")
    gcmSenderID = GGLContext.sharedInstance().configuration.gcmSenderID
    // [END_EXCLUDE]
    // Register for remote notifications
    if #available(iOS 8.0, *) 
        let settings: UIUserNotificationSettings =
        UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: nil)
        application.registerUserNotificationSettings(settings)
        application.registerForRemoteNotifications()
     else 
        // Fallback
        let types: UIRemoteNotificationType = [.Alert, .Badge, .Sound]
        application.registerForRemoteNotificationTypes(types)
    

    // [END register_for_remote_notifications]
    // [START start_gcm_service]
    let gcmConfig = GCMConfig.defaultConfig()
    GCMService.sharedInstance().startWithConfig(gcmConfig)

    return true


public func onTokenRefresh() 
    print("Token needs to be refreshed!")
    let options = [
        kGGLInstanceIDRegisterAPNSOption : deviceToken!,
        kGGLInstanceIDAPNSServerTypeSandboxOption : true
    ]
           GGLInstanceID.sharedInstance().tokenWithAuthorizedEntity(authorizedEntity, scope: kGGLInstanceIDScopeGCM, options: options)  (token, error) -> Void in
        if error != nil 
            print("Error: \(error.localizedDescription)")
         else 
            print("Token: \(token)")
        
    



public func application(application: UIApplication, openURL url: NSURL, sourceApplication: String?, annotation: AnyObject) -> Bool 
    return FBSDKApplicationDelegate.sharedInstance().application(application, openURL: url, sourceApplication: sourceApplication, annotation: annotation)


public func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) 
    let instanceIDConfig = GGLInstanceIDConfig.defaultConfig()
    instanceIDConfig.delegate = self
    // Start the GGLInstanceID shared instance with that config and request a registration
    // token to enable reception of notifications
    GGLInstanceID.sharedInstance().startWithConfig(instanceIDConfig)
    self.deviceToken = deviceToken


public func applicationDidEnterBackground(application: UIApplication) 
    GCMService.sharedInstance().disconnect()
    connectedToGCM = false


public func applicationDidBecomeActive( application: UIApplication) 
    print("App became active")
    UIApplication.sharedApplication().applicationIconBadgeNumber = 0
    // Connect to the GCM server to receive non-APNS notifications
    GCMService.sharedInstance().connectWithHandler(
        (NSError error) -> Void in
        if error != nil 
            print("Could not connect to GCM: \(error.localizedDescription)")
         else 
            self.connectedToGCM = true
            print("Connected to GCM")
            // ...
        
    )


public func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) 
    print("Notification received: \(userInfo)")
    GCMService.sharedInstance().appDidReceiveMessage(userInfo)


public func application(application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: NSError) 
    print("Error registering")


public func application( application: UIApplication,
    didReceiveRemoteNotification userInfo: [NSObject : AnyObject],
    fetchCompletionHandler handler: (UIBackgroundFetchResult) -> Void) 

        print("Notification received(background): \(userInfo)")

        NotificationManager.sharedInsteance().parseNotification(userInfo)

        // This works only if the app started the GCM service
        GCMService.sharedInstance().appDidReceiveMessage(userInfo);

        // Handle the received message
        // Invoke the completion handler passing the appropriate UIBackgroundFetchResult value
        // [START_EXCLUDE]
        handler(UIBackgroundFetchResult.NewData);
        // [END_EXCLUDE]

更新

好的,所以我相信我搞砸了 .plist 位置(由于某种原因它不在根目录中)。我移到了根目录,现在在启动 GCM 时收到此警告/错误:

UPD。 好吧,它实际上只发生过一次就停止了。所以我认为问题不在这里。

在我将 .plist 移动到根目录后,onTokenRefresh() 调用停止了,但我仍然没有注册。

【问题讨论】:

能否在 GCM 的应用程序委托中包含您实现的回调? 这通常发生在 APNs 令牌无效的情况下。您确定用于向 GCM 注册的 APNS 令牌对特定服务器有效吗?例如,您没有使用沙盒 APNS 令牌将消息发送到使用 PROD 证书签名的应用程序。还要确保在向 GCM 注册时正确地为 kGGLInstanceIDAPNSServerTypeSandboxOption 赋值。 我正在使用正确的沙盒选项。如何检查令牌是否无效? 【参考方案1】:

所以我解决了这个问题。看来我没有使用正确的 iOS 开发配置文件。我使用的是通用的,而我需要为我的包名称使用特定的。发生这种情况的原因是因为我一周前重新安装了我的操作系统,所以另一个证书被清除了,直到我下载并手动将其添加到 Xcode 后才起作用。我还需要从设备中删除团队配置文件。完全不是 GCM 或 APNs 故障。

【讨论】:

发生在我身上,修复配置文件对我也有用。我认为它在升级 iOS 和 xcode 后开始发生,需要重新下载我的配置文件。 真的很有帮助!非常感谢您提出并回答这个问题!

以上是关于GCM IOS 未注册问题的主要内容,如果未能解决你的问题,请参考以下文章

C# GCM 未注册

卸载应用时 GCM 未取消注册设备

GCM 通知结果=[错误=未注册]

GCM'错误:未注册'

从未调用 gcm.unregister() 时出现意外的 gcm 未注册消息

Android GCM“错误”:“未注册”