I have an app that consumes firebase messaging service to recieve push notifications.
I already searched for the differences between using "notification" or "data" while sending the notification information to the FCM sending server.
My notification doesn't appear when the app is destroyed, only while it is on foreground or background.
I need to make it so that my server ocassionally send a push notification to the device every once and a while (about twice a week or so) but the app should be able to display the notification while it is closed.
Any idea how I can do this? This is my notification json encoded string that i am sending to the FCM sending server:
{
"to":"d_kQKDkq5V4:APA91bG4hIyqbr2a_24nUIe6jVbySS90FMnVKwwuTfG1dV3OooeAc_555XkB2e_h_oWzEOMde8uIt2ESvv_Thl1J1sEXDHWPsLCE7EdMXkZ_AS6xlVq8uJIjVojz1WPxqiUjWZm65Ypf",
"data":{
"title":"App in background again",
"body":"asdsad"
}
}
So the notification appears while the app is on background or foreground, but it doesn't appear while it is destroyed. How do i make it so it can still recieve and display the notification while the app is destroyed?
This is my onMessageRecieved() method:
public void onMessageReceived(RemoteMessage remoteMessage) {
/*String title = remoteMessage.getNotification().getTitle();
String message = remoteMessage.getNotification().getBody();
*/
Log.e("REG_TOKEN",remoteMessage.getData().get("title"));
Map<String,String> data = remoteMessage.getData();
String title = data.get("title");
String message = data.get("body");
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this);
notificationBuilder.setContentTitle(title)
.setContentText(message)
.setSmallIcon(R.mipmap.ic_launcher_round)
.setAutoCancel(true)
.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notificationBuilder.build());
}