简体   繁体   中英

Swift gcm push can't receive notification

I have problem with GCM and ios push notificaion. App connect to GCM and all that works but when i can't receive notification.

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")
}

This didReceiveRemoteNotification never fires, and i am sure that server send message.

What can be problem?

Thanks to Arthur Thompson , needed this:

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")
        }
    })
}

And i forgot:

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

to add on 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())
    }
}

Now everything works..

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM