Cocos2d FCM推送通知不工作 [英] Cocos2d FCM Push Notification not working

查看:672
本文介绍了Cocos2d FCM推送通知不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个游戏,其中我想集成推送通知。为方便使用,我连接到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()

//注册远程通知
如果#available(iOS 8.0,*){
让设置: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()

//为InstanceID令牌刷新回调添加观察器。
NSNotificationCenter.defaultCenter()。addObserver(self,selector:tokenRefreshNotification:,
name:kFIRInstanceIDTokenRefreshNotification,object:nil)

NSNotificationCenter.defaultCenter()。addObserver ,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))

//连接到FCM,因为连接可能失败,令牌。
connectToFcm()
}

func connectToFcm(){
FIRMessaging.messaging()。connectWithCompletion {(error)in
if(error! nil){
print(无法连接FCM。\(错误))
} else {
print(Connected to FCM。)
}
}
}

func didBecomeActive(application:UIApplication){
NSLog(Did Being Active)
connectToFcm()
}

func didEnterBackground(application:UIApplication){
NSLog(Did enter background)
FIRMessaging.messaging()。disconnect()
NSLog(Disconnected from FCM。 )
}

获取以下设备令牌:




cMqaF0FVwbY:APA91bFMzsUmP2NKSipGMC7NTehPjBDWE72S6Fdi13iVV51ziPZvVkVw3g5NXEGooII5IVwby3ekBS4MquWyRQyF7rXDnWTDvY6eDPtL_kQQDk3Wen6V0DPv2Yf-Ym6YPi8k66aW6I-O




我能够获取设备令牌


如果您需要进一步澄清,请与我们联系。

解决方案


获取令牌使用Notification.object并检查它。
并查看我的代码此处 Firebase推播通知




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

//连接到FCM,因为连接可能在尝试之前具有令牌时失败。
connectToFcm()
}


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.

解决方案

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

这篇关于Cocos2d FCM推送通知不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆