【问题标题】:Cocos2d FCM Push Notification not workingCocos2d FCM 推送通知不起作用
【发布时间】:2016-06-22 10:40:34
【问题描述】:

我正在创建一个想要集成推送通知的游戏。为了便于使用,我连接到 FCM,信任 Google 的服务。

按照我的想法,我正确集成了 FCM,但仍然不知道缺少什么,但是当我从 Firebase 控制台发送任何通知时,我的设备上没有收到通知。

从仅开始 1 次开始,我就能够收到通知,这也因我的无知而丢失,但之后我无法收到具有相同代码和开发配置文件的通知事件。

如果有人指出我的错误并帮助我清除障碍,那就太好了。

在 Cocos2d 游戏中集成 FCM 时,我已经在 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.")
    }

获取关注设备令牌:

cMqaF0FVwbY:APA91bFMzsUmP2NKSipGMC7NTehPjBDWE72S6Fdi13iVV51ziPZvVkVw3g5NXEGooII5IVwby3ekBS4MquWyRQyF7rXDnWTDvY6eDPtL_kQQDk3Wen6V0DPv2Yf-Ym6YPi8k66aW6I-O

我也可以获取设备令牌,但无法接收通知。

如果您需要任何进一步的说明,请告诉我。

【问题讨论】:

  • 你打电话给 registerForRemoteNotifications 了吗?
  • @ArthurThompson 是的,我做到了。您可以在 setupPushNotification 函数中查看代码。
  • 在调用此方法 connectToFcm() 之前先检查是否获取了设备令牌?
  • @sanandyavipul 意思是在调用 connectToFcm() 之前是否需要检查我是否获得了有效的令牌?是吗,你的意思是?来自 FCM 指南:“当您的应用程序变为活动状态以及新的注册令牌可用时连接。一旦您的应用程序连接,FCM 将忽略后续连接尝试。”并且不是每次都调用“tokenRefreshNotification”方法。 :(
  • @Mobihunterz U 首先检查每次什么是设备令牌在调用此方法之前 connectToFcm() ?

标签: swift cocos2d-iphone firebase apple-push-notifications firebase-cloud-messaging


【解决方案1】:

获取 Token Use Notification.object 并检查它。 并在此处查看我的代码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()
}

【讨论】:

  • 甚至我的 tokenRefreshNotification 也没有被每次调用。
猜你喜欢
  • 2020-10-14
  • 2018-03-05
  • 2018-04-24
  • 1970-01-01
  • 2019-08-07
  • 2018-02-14
  • 1970-01-01
  • 2020-04-21
  • 2021-05-04
相关资源
最近更新 更多