I am creating a game in which I want to integrate Push notification. For ease of use I connected to FCM, trusting Google's service.
I integrated FCM properly, as I think so, but still don't know what's missing but when I send any notification from Firebase Console, notification is not being received on my device.
From starting only 1 time, I was able to receive the notification, that also lost with my ignorance but after that I am not able to receive notification event with same code and development profile.
It would be great if someone points out my mistake and help me to clear the roadblock.
As I am integrating FCM in Cocos2d game, I have written my code in MainScene.swift
override func onEnter() {
super.onEnter()
//if !self.globalHolders.isFBaseConfigured {
self.setupPushNotification()
// self.globalHolders.isFBaseConfigured = true
//}
}
override func onExit() {
super.onExit()
NSNotificationCenter.defaultCenter().removeObserver(self, name: kFIRInstanceIDTokenRefreshNotification, object: nil)
NSNotificationCenter.defaultCenter().removeObserver(self, name: UIApplicationDidBecomeActiveNotification, object: nil)
NSNotificationCenter.defaultCenter().removeObserver(self, name: UIApplicationDidEnterBackgroundNotification, object: nil)
}
func setupPushNotification () {
let application = UIApplication.sharedApplication()
// Register for remote notifications
if #available(iOS 8.0, *) {
let settings: UIUserNotificationSettings =
UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: nil)
application.registerUserNotificationSettings(settings)
application.registerForRemoteNotifications()
} else {
// Fallback
let types: UIRemoteNotificationType = [.Alert, .Badge, .Sound]
application.registerForRemoteNotificationTypes(types)
}
FIRApp.configure()
// Add observer for InstanceID token refresh callback.
NSNotificationCenter.defaultCenter().addObserver(self, selector: "tokenRefreshNotification:",
name: kFIRInstanceIDTokenRefreshNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: "didBecomeActive:", name: UIApplicationDidBecomeActiveNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: "didEnterBackground:", name: UIApplicationDidEnterBackgroundNotification, object: nil)
}
func tokenRefreshNotification(notification: NSNotification) {
let refreshedToken:String? = FIRInstanceID.instanceID().token()
print("InstanceID token: \(refreshedToken)")
// Connect to FCM since connection may have failed when attempted before having a token.
connectToFcm()
}
func connectToFcm() {
FIRMessaging.messaging().connectWithCompletion { (error) in
if (error != nil) {
print("Unable to connect with FCM. \(error)")
} else {
print("Connected to FCM.")
}
}
}
func didBecomeActive(application:UIApplication) {
NSLog("Did Become Active")
connectToFcm()
}
func didEnterBackground(application: UIApplication) {
NSLog("Did enter background")
FIRMessaging.messaging().disconnect()
NSLog("Disconnected from FCM.")
}
Getting Following Device token:
cMqaF0FVwbY:APA91bFMzsUmP2NKSipGMC7NTehPjBDWE72S6Fdi13iVV51ziPZvVkVw3g5NXEGooII5IVwby3ekBS4MquWyRQyF7rXDnWTDvY6eDPtL_kQQDk3Wen6V0DPv2Yf-Ym6YPi8k66aW6I-O
I am able to get device token also, but unable to receive notification.
Let me know if you need any further clarification.