What am I doing wrong? It prints that my Token registration is NIL, although it also successfully completes the registration in GCM.
appDelegate.swift
import UIKit
import SlideMenuControllerSwift
import AFNetworking
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
var connectedToGCM = false
var subscribedToTopic = false
var gcmSenderID: String?
var registrationToken: String?
var registrationOptions = [String: AnyObject]()
let registrationKey = "onRegistrationCompleted"
let messageKey = "onMessageReceived"
let subscriptionTopic = "/topics/global"
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
var configureError:NSError?
GGLContext.sharedInstance().configureWithError(&configureError)
if configureError != nil {
println("Error configuring the Google context: \(configureError)")
}
gcmSenderID = GGLContext.sharedInstance().configuration.gcmSenderID
var types: UIUserNotificationType = UIUserNotificationType.Badge | UIUserNotificationType.Alert | UIUserNotificationType.Sound
var settings: UIUserNotificationSettings =
UIUserNotificationSettings( forTypes: types, categories: nil )
application.registerUserNotificationSettings(settings)
application.registerForRemoteNotifications()
GCMService.sharedInstance().startWithConfig(GCMConfig.defaultConfig())
AFNetworkReachabilityManager.sharedManager().startMonitoring()
Utilities.customizeNavigationBar()
Utilities.customizeNavigationBarButtons()
let storyboard = UIStoryboard(name: "MenuStoryboard", bundle: nil)
let containerController: RestaurantTableViewController = storyboard.instantiateViewControllerWithIdentifier("hi") as! RestaurantTableViewController
let rightMenu: RightMenuViewController = storyboard.instantiateViewControllerWithIdentifier("rightMenu") as! RightMenuViewController
let leftMenu: LeftMenuViewController = storyboard.instantiateViewControllerWithIdentifier("leftMenu") as! LeftMenuViewController
let nvc: UINavigationController = UINavigationController(rootViewController: containerController)
let slideMenuController = SlideMenuController(mainViewController:nvc, leftMenuViewController: leftMenu, rightMenuViewController: rightMenu)
self.window?.rootViewController = slideMenuController
self.window?.makeKeyAndVisible()
return true
}
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 application( application: UIApplication, didFailToRegisterForRemoteNotificationsWithError
error: NSError ) {
println("Registration for remote notification failed with error: \(error.localizedDescription)")
let userInfo = ["error": error.localizedDescription]
NSNotificationCenter.defaultCenter().postNotificationName(
registrationKey, object: nil, userInfo: userInfo)
}
func registrationHandler(registrationToken: String!, error: NSError!) {
if (registrationToken != nil) {
self.registrationToken = registrationToken
println("Registration Token: \(registrationToken)")
NSUserDefaults.standardUserDefaults().setObject(registrationToken, forKey: "registrationToken")
self.subscribeToTopic()
let userInfo = ["registrationToken": registrationToken]
NSNotificationCenter.defaultCenter().postNotificationName(
self.registrationKey, object: nil, userInfo: userInfo)
} else {
println("Registration to GCM failed with error: \(error.localizedDescription)")
let userInfo = ["error": error.localizedDescription]
NSNotificationCenter.defaultCenter().postNotificationName(
self.registrationKey, object: nil, userInfo: userInfo)
} }
func onTokenRefresh() {
println("The GCM registration token needs to be changed.")
GGLInstanceID.sharedInstance().tokenWithAuthorizedEntity(gcmSenderID,
scope: kGGLInstanceIDScopeGCM, options: registrationOptions, handler: registrationHandler)
}
func subscribeToTopic() {
if(registrationToken != nil && connectedToGCM) {
GCMPubSub.sharedInstance().subscribeWithToken(self.registrationToken, topic: subscriptionTopic,
options: nil, handler: {(NSError error) -> Void in
if (error != nil) {
if error.code == 3001 {
print("Already subscribed to \(self.subscriptionTopic)")
} else {
print("Subscription failed: \(error.localizedDescription)");
}
} else {
self.subscribedToTopic = true;
NSLog("Subscribed to \(self.subscriptionTopic)");
}
})
}
}
func applicationDidBecomeActive( application: UIApplication) {
GCMService.sharedInstance().connectWithHandler({
(NSError error) -> Void in
if error != nil {
print("Could not connect to GCM: \(error.localizedDescription)")
} else {
self.connectedToGCM = true
print("Connected to GCM")
self.subscribeToTopic()
}
})
}
func application( application: UIApplication,
didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {
print("Notification received: \(userInfo)")
GCMService.sharedInstance().appDidReceiveMessage(userInfo);
NSNotificationCenter.defaultCenter().postNotificationName(messageKey, object: nil,
userInfo: userInfo)
}
func application( application: UIApplication,
didReceiveRemoteNotification userInfo: [NSObject : AnyObject],
fetchCompletionHandler handler: (UIBackgroundFetchResult) -> Void) {
print("Notification received: \(userInfo)")
GCMService.sharedInstance().appDidReceiveMessage(userInfo);
NSNotificationCenter.defaultCenter().postNotificationName(messageKey, object: nil,
userInfo: userInfo)
handler(UIBackgroundFetchResult.NoData);
}
func applicationDidEnterBackground(application: UIApplication) {
GCMService.sharedInstance().disconnect()
self.connectedToGCM = false
println("Stop executing app and whent into background!")
}
func applicationWillEnterForeground(application: UIApplication) {
}
func applicationWillTerminate(application: UIApplication) {
}
func willSendDataMessageWithID(messageID: String!, error: NSError!) {
if (error != nil) {
} else {
}
}
func didSendDataMessageWithID(messageID: String!) {
}
func didDeleteMessagesOnServer() {
}
}
Error log
Tracking ID must not be nil or empty.
Client ID must not be nil or empty.
Attempted to configure [Identity, Analytics, AdMob, SignIn, AppInvite, CloudMessaging].
Successfully configured [CloudMessaging].
Failed to configure [Analytics, SignIn].
Subspecs not present, so not configured [Identity, AdMob, AppInvite].
Error configuring the Google context: Optional(Error Domain=com.google.greenhouse Code=-200 "Unable to configure GGL."
UserInfo=0x7fdab9d3a390 {NSLocalizedRecoverySuggestion=Check formatting and location of GoogleService-Info.plist., NSLocalizedDescription=Unable to configure GGL., NSLocalizedFailureReason=Unable to parse supplied GoogleService-Info.plist. See log for details.})
I'm trying to connect to GCM and I already set the SENDER-ID and SENDER ID in order, what is wrong now?
Setting up the GCM Client application on iOS , it says:
, InstanceID registrationToken . , nil . , . GGLInstanceID.h. , , .
nil error object, nil.