Swift gcm push无法接收通知

时间:2015-10-01 17:46:39

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

我有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永远不会触发,我确信服务器会发送消息。

可能有什么问题?

1 个答案:

答案 0 :(得分:0)

感谢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())
    }
}

现在一切正常..