swift - Cocos2d FCM 推送通知不起作用

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

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

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

从仅开始一次,我就能够收到通知,但由于我的无知,该通知也丢失了,但此后我无法使用相同的代码和开发配置文件接收通知事件。

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

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

获取以下设备 token :

cMqaF0FVwbY:APA91bFMzsUmP2NKSipGMC7NTehPjBDWE72S6Fdi13iVV51ziPZvVkVw3g5NXEGooII5IVwby3ekBS4MquWyRQyF7rXDnWTDvY6eDPtL_kQQDk3Wen6V0DPv2Yf-Ym6YPi8k66aW6I-O

我也能够获取设备 token ,但无法接收通知。

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

最佳答案

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()
}

关于swift - Cocos2d FCM 推送通知不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37965615/

相关文章:

Swift:DatePicker-Timer?

swift - 如何在 SwiftUI 上使 LongPressGesture 失败

arrays - 如果属性值存在于另一个对象数组中,则快速过滤对象数组

objective-c - cocos2d动画总是崩溃

c++ - 将 Objective C NSRange 转换为 C++ 代码?

ios - 为什么即使导入后 PhoneAuthProvider 对于 iOS 和 Swift 仍未解决

ios - 使用 CCTMXTiledMap 迭代 cocos2d 中所有可用的图 block

firebase - 为什么我无法更改 Firebase 引用的优先级(权限被拒绝)

android - FirebaseAuth.getInstance() 崩溃并显示 "IllegalArgumentException: Given String is empty or null"

ios - 在谷歌登录后使用 Swift 4 推送 View Controller