应用程序在后台时未调用FCM通知方法

时间:2017-04-04 16:15:19

标签: android firebase firebase-cloud-messaging

这个问题已被问过几次,但它从来没有得到妥善回答,也无法找到合适的解决方案。

我在我的应用中实现了FCM,当应用处于前台时,FCM也正在运行。但是一旦它处于背景或关闭状态,一切都变得混乱。我知道数据有效负载如果在后台没有来,但如果应用程序不在前台,则根本不会调用onMessageReceived。

它将通知发送到系统托盘并且android显示它,但我需要设计通知,更重要的是我需要一些功能,当通知命中时,根据某些条件导航到特定屏幕。

Whatsapp和Gmail如何做这些事情?

请在标记此副本或关闭之前帮助我...任何有用的想法。

编辑:之前已检查过的链接Link1 Link2 Link3 Link4等等。 代码片段应用程序级别:

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);
            }
        }
    }

如果我错过了通知部分的任何内容,请告诉我。数据部分工作正常。通知触发时如何在后台同步某些数据。

对于低于4.4的设备的通知的任何更新?它根本不会收到通知。有什么问题吗?

0 个答案:

没有答案