I am able to register my device with backend using GCM Registration ID.But I am not able to receive GCM message in my iPad/iPhone, even though the server(.NET) got success message. And I am able to receive the notification when sending through a third party tool called APN Tester. I am not able to find what must be the problem for this. And I checked all my provisioning profiles, certificates, reinstallation of apps. Can someone help me on this?
In my AppDelegate.Swift,
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
dispatch_async(dispatch_get_main_queue(),{
let internet = Server.isConnectedToInternet()
if !internet{
}else{
GMSServices.provideAPIKey(self.googleMapsApiKey)
var configureError:NSError?
GGLContext.sharedInstance().configureWithError(&configureError)
assert(configureError == nil, "Error configuring Google services: \(configureError)")
self.gcmSenderID = GGLContext.sharedInstance().configuration.gcmSenderID
let application = UIApplication.sharedApplication()
let settings: UIUserNotificationSettings =
UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: nil)
application.registerUserNotificationSettings(settings)
application.registerForRemoteNotifications()
GCMService.sharedInstance().startWithConfig(GCMConfig.defaultConfig())
}
})
return true
}
func application( application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData ) {
print("didRegisterForRemoteNotificationsWithDeviceToken \(deviceToken)")
// [END receive_apns_token]
// [START get_gcm_reg_token]
// Create a config and set a delegate that implements the GGLInstaceIDDelegate protocol.
let instanceIDConfig = GGLInstanceIDConfig.defaultConfig()
instanceIDConfig.delegate = self
// Start the GGLInstanceID shared instance with that config and request a registration
// token to enable reception of notifications
GGLInstanceID.sharedInstance().startWithConfig(instanceIDConfig)
//Change kGGLInstanceIDAPNSServerTypeSandboxOption=true for testing and kGGLInstanceIDAPNSServerTypeSandboxOption=false for production
#if DEBUG
registrationOptions = [kGGLInstanceIDRegisterAPNSOption:deviceToken, kGGLInstanceIDAPNSServerTypeSandboxOption:true]
#else
registrationOptions = [kGGLInstanceIDRegisterAPNSOption:deviceToken, kGGLInstanceIDAPNSServerTypeSandboxOption:false]
#endif
GGLInstanceID.sharedInstance().tokenWithAuthorizedEntity(gcmSenderID, scope: kGGLInstanceIDScopeGCM, options: registrationOptions, handler: registrationHandler)
// [END get_gcm_reg_token]
}
func application( application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: NSError ) {
print("Registration for remote notification failed with error: \(error.localizedDescription)")
}
func application( application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], fetchCompletionHandler handler: (UIBackgroundFetchResult) -> Void) {
print("Notification received: \(userInfo)")
MessageHandler().handleMessage(userInfo)
handler(UIBackgroundFetchResult.NoData);
}
func registrationHandler(registrationToken: String!, error: NSError!) {
if (registrationToken != nil) {
self.registrationToken = registrationToken
if parent != nil{
parent?.gcmKey = registrationToken
parent?.saveManagedParent()
sendGCMKeyGBox()
}
print("Registration Token: \(registrationToken)")
} else {
print("Registration to GCM failed with error: \(error.localizedDescription)")
}
}
func sendGCMKeyGBox() {
let senderID = (UIApplication.sharedApplication().delegate as! AppDelegate).gcmSenderID!
print(senderID)
let params: [String:AnyObject] = [
"appId" : (parent!.appId),
"registrationId" : (parent!.gcmKey),
"senderId" : senderID,
"isAndroid" : "0"
]
Server.sendPost(URL.GCMRegistration, params: params, completionHandler: sendGCMSenderIDCompletionHandler)
}
Above are the GCM functions I am using in AppDelegate Class.