I am implementing a system in which I receive push notifications with Firebase.
If the application is open or in the background, I am able to capture the notification sent by firebase, I take the data and if I am interested I show a notification or not, I do what I need.
For this, I have the following classes:
MyFirebaseMessagingService:
public class MyFirebaseMessagingService extends FirebaseMessagingService {
private static final String LOGTAG = "android-fcm";
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
if(remoteMessage.getNotification() != null)
{
String titulo = remoteMessage.getNotification().getTitle();
String texto = remoteMessage.getNotification().getBody();
Log.d(LOGTAG, "NOTIFICACIÓN RECIBIDA");
Log.d(LOGTAG, "Título: " + titulo);
Log.d(LOGTAG, "texto: " + texto);
showNotification(titulo, texto);
}
if(remoteMessage.getData() != null)
{
Log.d(LOGTAG, "DATOS RECIBIDOS");
Log.d(LOGTAG, "Usuario: " + remoteMessage.getData().get("usuario"));
Log.d(LOGTAG, "Estado: " + remoteMessage.getData().get("estado"));
showNotification(remoteMessage.getData().get("usuario") + " titulo", remoteMessage.getData().get("estado") + " mensaje");
}
}
private void showNotification(String titulo, String texto)
{
try {
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this).setSmallIcon(R.drawable.ic_launcher_background).setContentTitle(titulo).setContentText(texto);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notificationBuilder.build());
}catch(Exception e)
{
String t = e.getMessage();
t = t + "";
}
}
}
MainActivity:
public class MainActivity extends AppCompatActivity {
private static final String LOGTAG = "android-fcm";
Button btnToken;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FirebaseMessaging.getInstance().subscribeToTopic("android");
if(getIntent().getExtras() != null)
{
Log.d(LOGTAG, "DATOS RECIBIDOS (INTENT)");
Log.d(LOGTAG, "Usuario: " + getIntent().getExtras().getString("usuario"));
Log.d(LOGTAG, "Estado: " + getIntent().getExtras().getString("estado"));
}
btnToken = (Button)findViewById(R.id.btnToken);
btnToken.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String refreshedToken = FirebaseInstanceId.getInstance().getToken();
Toast.makeText(MainActivity.this, "Token actualizado: " + refreshedToken, Toast.LENGTH_SHORT).show();
Log.e("Token", "Token actualizado: " + refreshedToken);
}
});
}
}
As I said, with this code, if I have the app open or in the background, upon receiving a notification, I capture it, go through the onMessageReceived method and locally generate the notification.
My problem is as follows. If I have the app closed (I remove it from the background), when I receive a firebase notification, it does not go through the onMessageReceived method and it automatically generates a notification which I can not capture.
Is there any possibility of capturing the message sent by firebase with the app closed for, if we are interested in generating the notification and if we are not interested, no?
I do not know if you have to capture it from another method or you have to do something different.