【问题标题】:Swift gcm push can't receive notificationSwift gcm push 收不到通知
【发布时间】: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 消息。

标签: ios swift push-notification google-cloud-messaging apple-push-notifications


【解决方案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())
    }
}

现在一切正常..

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-05-21
    • 1970-01-01
    • 2016-12-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-16
    相关资源
    最近更新 更多