Did some research on making FCM notification work some devices when the application is killed by the user(swiped away by the user from the app tray) or killed by the system OS but did not find any solution would appreciate if you can provide with solution or tell me what i am dong wrong. The notification is a data payload
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
// TODO: Handle FCM messages here.
// If the application is in the foreground handle both data and notification messages here.
// Also if you intend on generating your own notifications as a result of a received FCM
// message, here is where that should be initiated.
notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
String payload = "",notification_message = "", channelId ="", channelName="AAA", channelDescription="";
Log.d(TAG, "From: " + remoteMessage.getFrom());
String title = null, body = null;
if(remoteMessage.getNotification() != null) {
Log.d("REMOTE_DATA",remoteMessage.getNotification().getBody());
title = remoteMessage.getNotification().getTitle();
body = remoteMessage.getNotification().getBody();
}
if(remoteMessage.getData() != null) {
if(remoteMessage.getData().get("title") != null)
title = remoteMessage.getData().get("title");
if(remoteMessage.getData().get("message") != null)
body = remoteMessage.getData().get("message");
if(remoteMessage.getData().get("channelName") != null)
channelName = remoteMessage.getData().get("channelName");
}
}
//Setting up Notification channels for android O and above
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
Log.e(TAG, "onMessageReceived: => " + android.os.Build.VERSION.SDK_INT);
if(notificationChannels.containsKey(channelName)) {
JSONObject channelData = (JSONObject) notificationChannels.get(channelName);
try {
channelId = channelData.getString("channelId");
channelName = channelData.getString("channelName");
channelDescription = channelData.getString("channelDescription");
} catch (JSONException e) {
e.printStackTrace();
}
NotificationChannel notificationChannel = notificationManager.getNotificationChannel(channelId);
if (notificationChannel == null)
setupChannel(channelId, channelName, channelDescription);
}
}
Log.d(TAG, "Notification Message Body: " + title);
Log.d(TAG, "Notification Message Body: " + body);
if(title != null && body != null) {
apptivity_variable.createNotification(channelId,title, body);
db.insertNotification(title, body, remoteMessage.getData().toString());
}
}