FirebaseMessagingService未触发回调

时间:2018-05-30 16:14:04

标签: android firebase firebase-cloud-messaging react-native-android notification-listener

我用FirebaseMessagingService编写了一个代码来读取通知。该应用程序是Android的一部分,部分反应原生。 问题:FirebaseMessagingService onCreate()被调用,但即使应用程序位于前台,onMessageReceived回调也不会被触发。 此外,通知都包含数据。

这是我写的firebase消息服务代码。

package com.lockquick;

import android.content.Context;
import android.content.Intent;
import android.media.RingtoneManager;
import android.support.v4.content.LocalBroadcastManager;
import android.widget.Toast;

import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;



import com.google.firebase.messaging.FirebaseMessagingService;

public class NotificationService extends FirebaseMessagingService {
    private LocalBroadcastManager broadcaster;

    @Override
    public void onCreate() {

        Toast.makeText(getApplicationContext(), "NotificationService  created", Toast.LENGTH_SHORT).show();

         //This part is working fine

        broadcaster = LocalBroadcastManager.getInstance(this);
    }

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        Intent intent = new Intent("MyData");


        String s= remoteMessage.getData().get("Type");

        // This part is not working.
        Toast.makeText(getApplicationContext(), "onMessageReceived  called" + s, Toast.LENGTH_SHORT).show();

        if(s.equals("S")){
            intent.putExtra("s", remoteMessage.getData().get("S"));
            broadcaster.sendBroadcast(intent);
        }

    }
}

我正在从反应原生模块初始化firebase messenger服务。

            // Notificatons test
            LocalBroadcastManager.getInstance(getReactApplicationContext()).registerReceiver((mMessageReceiver),
                    new IntentFilter("MyData")
            );


            getReactApplicationContext().startService(new Intent(this.getReactApplicationContext(), NotificationService.class));

0 个答案:

没有答案