0

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?

Community
  • 1
  • 1
Ari
  • 1,296
  • 1
  • 18
  • 36
  • @Selvin please read my query properly, I didn't ask when it is called and when it is not, I asked for a workaround. Please share valuable feedback how I can achieve my requirements. Thanks anyways. – Ari Apr 04 '17 at 16:48
  • Obviously send proper message type... As is stated in many similar questions. – Selvin Apr 04 '17 at 17:04
  • @Selvin please have a look into this part I mentioned: "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." I need to use the notification for syncing some data, different APIs needs to be called based on some conditions. Please suggest how to achieve that without control on the notification payload? – Ari Apr 04 '17 at 17:18
  • There is no way. Only right payload. – Selvin Apr 04 '17 at 17:21
  • So you don't have an answer. Will wait for others if anyone has a different suggestion. This was possible using GCM long back, I did it many times, don't need to show notification all the time, some background sync is fine. And please be helpful to everyone when they ask some doubts in SO, everyone is not stupid as much as you wish them to be. – Ari Apr 04 '17 at 17:30
  • Yes, it was, with right payload. Gcm had no notification built in so it has only one type of message. – Selvin Apr 04 '17 at 17:31
  • "***never properly answered** and can't come to a proper solution.*" -- this is a bit harsh. There are a number of posts that answer this question properly. With that said. It's hard to provide any answer on your question. "*How do Whatsapp and Gmail do those stuff?*" -- would be too broad. You say the code isn't working, but there's no code snippet posted -- would be off-topic (Why isn't this code working?). With all that said, do post the code snippets you currently have (app server and client side), and the SO posts you've checked and we'll see what we can do from their. – AL. Apr 05 '17 at 02:19
  • @AL. Edited the question. added code snippet. Please have a look if I'm missing out anything? And if any update regarding devices lower than Lollypop. – Ari Apr 05 '17 at 13:16
  • Hi. Could you also put in a sample payload that you're sending? If it's from an App Server, the codes when sending them would be good too. If you're sending from the Firebase Notifications Console, perhaps a screenshot. I'm not aware of any issues with FCM for pre-Lollipop versions. – AL. Apr 06 '17 at 03:27
  • @AL. No, I'm not sending from Firebase Console. I don't think the server payload needs to be different whether the client app is foreground or background. And while the App is foreground it works fine, I'm even syncing whenever Notification payload hits. But same doesn't happen when the app is background/closed. And there are problems with FCM for pre-Lollipop versions. – Ari Apr 06 '17 at 11:54

0 Answers0