Firebase Unity SDK MessageReceived

时间:2017-01-15 11:11:50

标签: android unity3d firebase firebase-cloud-messaging

我正在使用Firebase Unity SDK 1.1.0在Android上发送消息。我在游戏对象的Start方法中添加了TokenReceived和MessageReceived的监听器,如快速入门教程中所述:

void Start() {
    Firebase.Messaging.FirebaseMessaging.TokenReceived += OnTokenReceived;
    Firebase.Messaging.FirebaseMessaging.MessageReceived += OnMessageReceived;
}

只要应用程序位于前台,这就没有问题。当应用程序发送到后台时,消息将发送到通知托盘,当我点击通知时,我的应用程序将返回到前台。所以一切都按预期工作到现在为止。但之后不再调用MessageReceived侦听器。我尝试在OnApplicationPause中删除两个侦听器,并在应用程序恢复时再次添加它们:

void OnApplicationPause(bool paused) {
    if (paused) {
        Firebase.Messaging.FirebaseMessaging.TokenReceived -= OnTokenReceived;
        Firebase.Messaging.FirebaseMessaging.MessageReceived -= OnMessageReceived;
    } else {
        Firebase.Messaging.FirebaseMessaging.TokenReceived += OnTokenReceived;
        Firebase.Messaging.FirebaseMessaging.MessageReceived += OnMessageReceived;
    }
}

但这没有帮助。我也尝试了没有任何数据的纯通知消息,但它是相同的行为。我做错了什么或Firebase Unity SDK还处于测试阶段?

1 个答案:

答案 0 :(得分:0)

我可以把它放在评论中,但是图片不能包含在内,所以我把它放在答案中。

代码是正确的,我可以在应用处于活动状态,后台甚至未启动时收到消息。这是快照。 enter image description here

以下是完整代码:

public void Start()
{
    Debug.Log("Unity Firebase App started");
    Firebase.Messaging.FirebaseMessaging.TokenReceived += OnTokenReceived;
    Firebase.Messaging.FirebaseMessaging.MessageReceived += OnMessageReceived;
}

public void OnTokenReceived(object sender, Firebase.Messaging.TokenReceivedEventArgs token)
{
    Debug.Log("Received Registration Token: " + token.Token);
}

public void OnMessageReceived(object sender, Firebase.Messaging.MessageReceivedEventArgs e)
{
    Debug.Log("Received a new message from: " + e.Message.From);
}