Cocos2d FCM Push Notification not working
Asked Answered
C

1

0

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.

Childish answered 22/6, 2016 at 10:40 Comment(7)
did you call registerForRemoteNotifications?Reinwald
@ArthurThompson yes I did. You can check the code in setupPushNotification function.Childish
First U check that Device Token is get before Call this Method connectToFcm() ?Rudich
@sanandiyavipul Means do I need to check whether I get valid token or not before calling connectToFcm()? Is it, what you mean? From FCM guideline: "Connect when your application becomes active and whenever a new registration token is available. Once your app is connected, FCM ignores subsequent attempts to connect." And the method "tokenRefreshNotification" is not being called every time. :(Childish
@Mobihunterz U firs check Every time what is device token Before call this Method connectToFcm() ?Rudich
Do you have an implementation of didReceiveRemoteNotification? If not then once your app is in the foreground you would not see any notifications.Reinwald
@sanandiyavipul I can't understand you are asking question or guiding me over something. But you can check my code posted in this question. And in Quickstart code for FCM the code is written in same manner what I have written here.Childish
R
0

Get Token Use Notification.object and check it. and See My Code Here Firebase Push Notification

func tokenRefreshNotification(notification: NSNotification) {
    let refreshedToken:String? = notification.object
    print("InstanceID token: \(refreshedToken)")

    // Connect to FCM since connection may have failed when attempted before having a token.
    connectToFcm()
}
Rudich answered 25/6, 2016 at 11:20 Comment(1)
Even my tokenRefreshNotification is not being called each time.Childish

© 2022 - 2024 — McMap. All rights reserved.