This question has been asked a couple of times, but it's never properly answered and can't come to a proper solution.
I implemented FCM in my app, FCM is working too when the app is in a foreground. But once it is in background or closed, everything went haywire. I knew data payload doesn't come if it's in background, but onMessageReceived doesn't get called at all if the app is not in foreground.
It sends the notification to the system tray and android shows it, but I need to design that Notification and more importantly I need some functionality to work when the notification hits, navigation to a particular screen depending upon some conditions has to made.
How do Whatsapp and Gmail do those stuff?
Please help me out... any useful ideas before marking this duplicate or close it.
EDIT: Links checked previously Link1 Link2 Link3 Link4 and a few more. Code Snippet App level:
if(remoteMessage != null){
LogUtils.debug(TAG, "From: " + remoteMessage.getFrom());
if (remoteMessage.getData().size() > 0) {
LogUtils.debug(TAG, "Message data payload: " + remoteMessage.getData());
Map<String, String> hashRemoteMessage = remoteMessage.getData();
if(hashRemoteMessage != null && hashRemoteMessage.size() > 0){
MessageDO objMessage = new MessageDO();
objMessage.MessageId = hashRemoteMessage.get(MessageDO.NOTI_MESSAGEID);
objMessage.messageSender = hashRemoteMessage.get(MessageDO.NOTI_SENDER);
objMessage.messageBody = hashRemoteMessage.get(MessageDO.NOTI_BODY);
objMessage.messageDate = hashRemoteMessage.get(MessageDO.NOTI_MESSAGEDATE);
objMessage.isRead = AppConstants.UPLOAD_STATUS_UNUPLOAD;
LogUtils.debug(TAG, "messageDate: " + objMessage.messageDate);
if(hashRemoteMessage.containsKey(MessageDO.NOTI_RECEIVER)) {
objMessage.messageReceiver = hashRemoteMessage.get(MessageDO.NOTI_RECEIVER);
objMessage.chatMemberId = objMessage.messageSender;
}
else if(hashRemoteMessage.containsKey(MessageDO.NOTI_GROUPID)) {
objMessage.messageGroupId = hashRemoteMessage.get(MessageDO.NOTI_GROUPID);
objMessage.messageReceiver = objMessage.messageGroupId;
objMessage.chatMemberId = objMessage.messageGroupId;
}
if(!TextUtils.isEmpty(objMessage.messageDate))
objMessage.messageDate = CalendarUtils.getTimeInTimeZone(objMessage.messageDate, CalendarUtils.DATE_TIME_FORMAT_T, "UTC", CalendarUtils.getCurrentTimeZone());
new ChatMessageDA().insertSingleChat(this, objMessage);
Intent intent = new Intent(AppConstants.ACTION_REFRESH);
intent.putExtra(MessageDO.SENDER, hashRemoteMessage.get(MessageDO.SENDER));
intent.putExtra(MessageDO.BODY, hashRemoteMessage.get(MessageDO.BODY));
sendBroadcast(intent);
}
}
if (remoteMessage.getNotification() != null) {
LogUtils.debug(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
Context context = getApplicationContext();
String fcmBody = remoteMessage.getNotification().getBody();
String fcmTitle = remoteMessage.getNotification().getTitle();
AppConstants.writeFile(fcmTitle + " Notification received: " + CalendarUtils.getDateinPattern(CalendarUtils.DATE_TIME_FORMAT_T));
AppConstants.writeFile("Notification title: " + fcmTitle + " body: " + fcmBody);
HashMap<String, String> hashFCM = null;
if(!TextUtils.isEmpty(fcmTitle)) {
switch (fcmTitle) {
case FCM_TYPE_LEAD:
hashFCM = new FCMNotificationParser(context).parseNotificationData(fcmBody, TYPE_LEAD);
break;
case FCM_TYPE_MESSAGE:
hashFCM = new FCMNotificationParser(context).parseNotificationData(fcmBody, TYPE_MESSAGE);
break;
}
if(hashFCM != null){
ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningTaskInfo> services = activityManager.getRunningTasks(Integer.MAX_VALUE);
boolean isActivityFound = false;
if (services.get(0).topActivity.getPackageName().toString().equalsIgnoreCase(context.getPackageName().toString())) {
isActivityFound = true;
}
AppConstants.writeFile(fcmTitle + " isActivityFound " + CalendarUtils.getDateinPattern(CalendarUtils.DATE_TIME_FORMAT_T));
if (isActivityFound) {
return;
} else {
sendNotification(hashFCM);
}
}
}
if (NetworkUtility.isConnectionAvailable(context)) {
Intent SyncIntent = new Intent(context, SyncDataService.class);
context.startService(SyncIntent);
}
}
}
Please let me know if I missed out anything for the notification part. Data part is working fine. How to sync some data in background when notification fires.
And any update for notification for devices less than ver 4.4? It never receives notification at all. Any fix on that?