如何从通知remotemessage Android获得特定价值

时间:2019-02-18 10:52:23

标签: android firebase push-notification firebase-cloud-messaging android-push-notification

那时通知到设备时,会有一个数据来自通知,但我希望从该remotemessage.it中获取特定数据。 这是我的远程消息数据 捆绑包[{google.delivered_priority = high,google.sent_time = 1550486789753,google.ttl = 2419200,google.original_priority = high,gcm.notification.e = 1,intent = {“ notification_from”:1,“声音”:1, “ vibrate”:1,“ body”:“您的新访客登机。我可以批准进入此访客”,“ title”:“ SocietyWerx”},gcm.notification.vibrate = 1,gcm.notification.sound = default ,gcm.notification.title = SocietyWerx,来自= 4178695373,gcm.notification.sound2 = 1,google.message_id = 0:1550486789763843%c9737a40c9737a40,gcm.notification.body =您的新访客来了。我可以批准进入此访客吗? ,gcm.notification.notification_from = 1,google.cae = 1,}]

下面是我的消息传递服务代码。我需要从此数组中的此参数获取notification _。

公共类MyFirebaseMessagingService扩展了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);
}

}

1 个答案:

答案 0 :(得分:2)

remoteMessage.getData()

获取用于推送通知的数据有效载荷。

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

您可能会获得像这样的值。