I wrote a code with a FirebaseMessagingService to read notifications. The app is part android and part react-native. PROBLEM: The FirebaseMessagingService onCreate() is getting called but the onMessageReceived callback is not firing even when the app is in the foreground. Moreover, the notifications all have data in them.
This is the firebase messaging service code that I wrote.
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);
}
}
}
I am intializing the firebase messenger service from a react native module.
// Notificatons test
LocalBroadcastManager.getInstance(getReactApplicationContext()).registerReceiver((mMessageReceiver),
new IntentFilter("MyData")
);
getReactApplicationContext().startService(new Intent(this.getReactApplicationContext(), NotificationService.class));