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.

Paresh Thakor
  • 1,795
  • 2
  • 27
  • 47
  • 1
    did you call registerForRemoteNotifications? – Arthur Thompson Jun 23 '16 at 16:49
  • @ArthurThompson yes I did. You can check the code in setupPushNotification function. – Paresh Thakor Jun 23 '16 at 17:41
  • First U check that Device Token is get before Call this Method connectToFcm() ? – Sanandiya Vipul Jun 24 '16 at 03:45
  • @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. :( – Paresh Thakor Jun 24 '16 at 06:11
  • @Mobihunterz U firs check Every time what is device token Before call this Method connectToFcm() ? – Sanandiya Vipul Jun 24 '16 at 13:29
  • Do you have an implementation of didReceiveRemoteNotification? If not then once your app is in the foreground you would not see any notifications. – Arthur Thompson Jun 24 '16 at 18:13
  • @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. – Paresh Thakor Jun 25 '16 at 11:15

1 Answers1

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()
}
Community
  • 1
  • 1
Sanandiya Vipul
  • 764
  • 4
  • 16