Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/212.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/firebase/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何从通知remotemessage Android获取特定值_Android_Firebase_Push Notification_Firebase Cloud Messaging_Android Push Notification - Fatal编程技术网

如何从通知remotemessage Android获取特定值

如何从通知remotemessage Android获取特定值,android,firebase,push-notification,firebase-cloud-messaging,android-push-notification,Android,Firebase,Push Notification,Firebase Cloud Messaging,Android Push Notification,当通知到达设备时,会有一个来自通知的数据,但我需要来自该远程消息的特定数据。它正在变为null 这是我的remotemessage数据 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,“振

当通知到达设备时,会有一个来自通知的数据,但我需要来自该远程消息的特定数据。它正在变为null
这是我的remotemessage数据

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,“振动”:1,“body”:“您的新客人到门口来。我能批准这位客人进入吗”,“title”:“SocietyWerx”},gcm.notification.swirb=1,gcm.notification.sound=default,gcm.notification.title=SocietyWerx,from=4178695373,gcm.notification.sound2=1,google.message\u id=0:1550486789763843%c9737a40c9737a40,gcm.notification.body=您的新客人来登机门。我可以批准这个客人入境吗,gcm.notification.notification\u from=1,google.c.a.e=1,}]

下面是我的消息服务代码。我需要从此数组中获取此参数的
通知

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);
    }
}
获取推送通知的数据有效负载

String messageID = remoteMessage.getData().get("messageID");
String message = remoteMessage.getData().get("message");
您可能会得到这样的值

获取推送通知的数据有效负载

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

您可能会得到如下值。

我认为您在应用程序位于前台时发送通知消息并获取数据,而在应用程序位于后台时不获取数据。根据Firebase,有两种类型的通知场景()。如果要发送数据,必须从受信任的环境发送。你们能提供你们正在发送的选项吗?我认为你们在前台发送通知消息并获取数据,而在后台并没有获取数据。根据Firebase,有两种类型的通知场景()。如果要发送数据,必须从受信任的环境发送。你能提供你要发送的选项吗?