0

When notification comes to device at that time there will be a data comes from notification but I want specific data from that remotemessage. It getting null
Here is my remotemessage data

Bundle[{google.delivered_priority=high, google.sent_time=1550486789753, google.ttl=2419200, google.original_priority=high, gcm.notification.e=1, intent={"notification_from":1,"sound":1,"vibrate":1,"body":"Your New Guest Come On Gate. Can I approved Entry this guest","title":"SocietyWerx"}, gcm.notification.vibrate=1, gcm.notification.sound=default, gcm.notification.title=SocietyWerx, from=4178695373, gcm.notification.sound2=1, google.message_id=0:1550486789763843%c9737a40c9737a40, gcm.notification.body=Your New Guest Come On Gate. Can I approved Entry this guest, gcm.notification.notification_from=1, google.c.a.e=1, }]

Below is my code for messaging service. I need to get notification_from this parameter from this array.

public class MyFirebaseMessagingService extends FirebaseMessagingService {

    private static final String TAG = MyFirebaseMessagingService.class.getSimpleName();
    String notification_from = "";
    private NotificationUtils notificationUtils;

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        Log.e(TAG, "From: " + remoteMessage.getFrom());

        if (remoteMessage == null)
            return;

        // Check if message contains a notification payload.
        if (remoteMessage.getNotification() != null) {
            JSONObject jsonObject = new JSONObject();
            try {
                JSONObject object = jsonObject.getJSONObject("data");
                notification_from = object.getString("notification_from");

            } catch (JSONException e) {
                e.printStackTrace();
            }
            Log.e(TAG, "Notification Body: " + remoteMessage.getNotification().getBody());
            handleNotification(remoteMessage.getNotification().getBody(), notification_from);
        }

        // Check if message contains a data payload.
        if (remoteMessage.getData().size() > 0) {
            Log.e(TAG, "Data Payload: " + remoteMessage.getData().toString());

            try {
                JSONObject json = new JSONObject(remoteMessage.getData().toString());
                handleDataMessage(json);
            } catch (Exception e) {
                Log.e(TAG, "Exception: " + e.getMessage());
            }
        }
    }

    private void handleNotification(String message, String notification_from) {


        if (!NotificationUtils.isAppIsInBackground(getApplicationContext())) {
            // app is in foreground, broadcast the push message
            Intent pushNotification = new Intent(Config.PUSH_NOTIFICATION);
            pushNotification.putExtra("message", message);
            pushNotification.putExtra("notification_from", notification_from);
            LocalBroadcastManager.getInstance(this).sendBroadcast(pushNotification);

            // play notification sound
            NotificationUtils notificationUtils = new NotificationUtils(getApplicationContext());
            notificationUtils.playNotificationSound();
        } else {

            Intent pushNotification = new Intent(Config.PUSH_NOTIFICATION);
            pushNotification.putExtra("message", message);
            pushNotification.putExtra("notification_from", notification_from);
            LocalBroadcastManager.getInstance(this).sendBroadcast(pushNotification);

            // play notification sound
            NotificationUtils notificationUtils = new NotificationUtils(getApplicationContext());
            notificationUtils.playNotificationSound();
            // If the app is in background, firebase itself handles the notification


        }
    }


    private void handleDataMessage(JSONObject json) {
        Log.e(TAG, "push json: " + json.toString());

        try {
            JSONObject data = json.getJSONObject("data");

            String title = data.getString("title");
            String message = data.getString("message");
            notification_from = data.getString("notification_from");
            boolean isBackground = data.getBoolean("is_background");
            String imageUrl = data.getString("image");
            String timestamp = data.getString("timestamp");
            JSONObject payload = data.getJSONObject("payload");

            Log.e(TAG, "title: " + title);
            Log.e(TAG, "message: " + message);
            Log.e(TAG, "isBackground: " + isBackground);
            Log.e(TAG, "payload: " + payload.toString());
            Log.e(TAG, "imageUrl: " + imageUrl);
            Log.e(TAG, "timestamp: " + timestamp);


            if (!NotificationUtils.isAppIsInBackground(getApplicationContext())) {
                // app is in foreground, broadcast the push message
                Intent pushNotification = new Intent(Config.PUSH_NOTIFICATION);

                pushNotification.putExtra("message", message);
                pushNotification.putExtra("notification_from", notification_from);
                LocalBroadcastManager.getInstance(this).sendBroadcast(pushNotification);

                // play notification sound
                NotificationUtils notificationUtils = new NotificationUtils(getApplicationContext());
                notificationUtils.playNotificationSound();
            } else {
                // app is in background, show the notification in notification tray
                Intent resultIntent = new Intent(getApplicationContext(), ApprovalPendingList.class);
                resultIntent.putExtra("message", message);
                resultIntent.putExtra("notification_from", notification_from);
                // check for image attachment
                if (TextUtils.isEmpty(imageUrl)) {
                    showNotificationMessage(getApplicationContext(), title, message, timestamp, resultIntent);
                } else {
                    // image is present, show notification with image
                    showNotificationMessageWithBigImage(getApplicationContext(), title, message, timestamp, resultIntent, imageUrl);
                }
            }
        } catch (JSONException e) {
            Log.e(TAG, "Json Exception: " + e.getMessage());
        } catch (Exception e) {
            Log.e(TAG, "Exception: " + e.getMessage());
        }
    }

    /**
     * Showing notification with text only
     */
    private void showNotificationMessage(Context context, String title, String message, String timeStamp, Intent intent) {
        notificationUtils = new NotificationUtils(context);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        notificationUtils.showNotificationMessage(title, message, timeStamp, intent);
    }

    /**
     * Showing notification with text and image
     */
    private void showNotificationMessageWithBigImage(Context context, String title, String message, String timeStamp, Intent intent, String imageUrl) {
        notificationUtils = new NotificationUtils(context);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        notificationUtils.showNotificationMessage(title, message, timeStamp, intent, imageUrl);
    }
}
greybeard
  • 2,249
  • 8
  • 30
  • 66
Mind Werx
  • 77
  • 12
  • I think you are sending notifications message and getting data when app is in foreground and not getting data when app is in background. According to Firebase there are two types of noticitation scenarios (https://firebase.google.com/docs/cloud-messaging/concept-options). If you want to send data, it must be send from trusted enviroment. Can you provide which option are you sending? – ilker Feb 18 '19 at 11:01

1 Answers1

2
remoteMessage.getData()

gets the data payload for push notification.

String messageID = remoteMessage.getData().get("messageID");
String message = remoteMessage.getData().get("message");

you may get the values like this.

Alp Altunel
  • 3,324
  • 1
  • 26
  • 27