0

我有問題,GCM和iOS推notificaion。 應用程序連接到GCM和所有的工作,但當我無法收到通知。斯威夫特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永遠不會啓動,我相信服務器發送消息。

可以採取什麼問題?

+0

您可以顯示下游消息的結構? –

+0

消息對象:'{collapseKey:「消息」的數據:{「味精」:「文本」}}' –

+0

你有沒有或registration_ids字段集? –

回答

0

感謝Arthur Thompson,需要這樣的:

​​

我也忘了:

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

現在一切正常..