Swift gcm push 收不到通知

Posted

技术标签:

【中文标题】Swift gcm push 收不到通知【英文标题】:Swift gcm push can't receive notification 【发布时间】:2015-10-01 17:46:38 【问题描述】:

我对 GCM 和 ios 推送通知有疑问。 应用程序连接到 GCM 并且一切正常,但是当我无法收到通知时。

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool 
    let gcmSenderID = sharedUser.getGcmSenderId()
    if gcmSenderID?.characters.count > 0 
        var configureError:NSError?
        GGLContext.sharedInstance().configureWithError(&configureError)
        if configureError != nil 
            print("Error configuring the Google context: \(configureError)")
                
        let settings: UIUserNotificationSettings = UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: nil)
        application.registerUserNotificationSettings(settings)
        application.registerForRemoteNotifications()
    

func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) 
    GGLInstanceID.sharedInstance().startWithConfig(GGLInstanceIDConfig.defaultConfig())
    registrationOptions = [kGGLInstanceIDRegisterAPNSOption:deviceToken,
        kGGLInstanceIDAPNSServerTypeSandboxOption:true]
    GGLInstanceID.sharedInstance().tokenWithAuthorizedEntity(gcmSenderID,
        scope: kGGLInstanceIDScopeGCM, options: registrationOptions, handler: registrationHandler)

func registrationHandler(registrationToken: String!, error: NSError!) 
    if (registrationToken != nil) 
        let params = [
            "reg_id": registrationToken,
            "dev_id": UIDevice.currentDevice().identifierForVendor!.UUIDString
        ]
        Alamofire.request(.POST, Config().gcmRegUrl, parameters: params, encoding: .JSON)
        print("Registred")
     else 
        print("Registration to GCM failed with error: \(error.localizedDescription)")
    

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

这个didReceiveRemoteNotification 永远不会触发,我确信服务器会发送消息。

可能有什么问题?

【问题讨论】:

能否展示一下下游消息的结构? 消息是对象:collapseKey: "message", data: "msg": "text" 您是否设置了 to 或 registration_ids 字段? @ArthurThompson 在服务器端是的,我向这个客户注册 ID 发送消息。 你在使用 GCMService.sharedInstance().connectWithHandler 吗?鉴于您使用的消息对象不会通过 APNS,您将需要它来接收非 APNS 消息。 【参考方案1】:

感谢Arthur Thompson,需要这个:

func applicationDidBecomeActive(application: UIApplication) 
    GCMService.sharedInstance().connectWithHandler(
        (NSError error) -> Void in
        if error != nil 
            print("Could not connect to GCM: \(error.localizedDescription)")
         else 
            print("Connected to GCM")
        
    )

我忘了:

GCMService.sharedInstance().startWithConfig(GCMConfig.defaultConfig())

添加didFinishLaunchingWithOptions

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool 
    let gcmSenderID = sharedUser.getGcmSenderId()
    if gcmSenderID?.characters.count > 0 
        var configureError:NSError?
        GGLContext.sharedInstance().configureWithError(&configureError)
        if configureError != nil 
            print("Error configuring the Google context: \(configureError)")
                
        let settings: UIUserNotificationSettings = UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: nil)
        application.registerUserNotificationSettings(settings)
        application.registerForRemoteNotifications()

        GCMService.sharedInstance().startWithConfig(GCMConfig.defaultConfig())
    

现在一切正常..

【讨论】:

以上是关于Swift gcm push 收不到通知的主要内容,如果未能解决你的问题,请参考以下文章

哪个app平台制作的app能把消息推送到手机的通知栏

Swift - GCM:不显示 iOS 远程推送通知

当应用程序关闭或应用程序在后台时获得两次 GCM PUSH 通知

android gcm push 不适用于 json 标签通知

GCM 推送通知的依赖项

使用 GCM 推送通知发布数据