gpt4 book ai didi

javascript - 将 Remotemessage 中的 JSONString Bundle 解析为 JSON 对象

转载 作者:太空宇宙 更新时间:2023-11-03 13:43:02 27 4
gpt4 key购买 nike

我正在尝试解析 FCM 中的通知数据。我会尽可能详细地解释我的问题。我有两个应用程序,一个是 android,另一个是 javascript webapp。因此,当从 webapp 向 androd app 发送 pushnotification 时,我将以 jsonstring 格式发送通知数据。现在我无法在 java 端(android)将它转换为 JSONObject。下面是我的代码

    var notification = {
'TITLE': currentUser.displayName,
'MSG': message,
'CHAT_KEY': chatKey,
'MSG_KEY': 'messageKey',
'USER_DISPLAY_NAME': currentUser.displayName,
'USER_EMAIL': currentUserEmail,
'USER_FCM_DEVICE_ID': toKey,
'USER_FCM_DEVICE_ID_SENDER': fromKey,
};

fetch('https://fcm.googleapis.com/fcm/send', {
'method': 'POST',
'headers': {
'Authorization': 'key=' + fromKey,
'Content-Type': 'application/json'
},
'body': JSON.stringify({
'notification': notification,
'to': toKey
})
}).then(function(response) {
console.log(response);
}).catch(function(error) {
console.error(error);
})
};

在安卓端

@Override public void onMessageReceived(RemoteMessage remoteMessage) {

if (remoteMessage.getNotification() != null) {


sendDefaultNotification(remoteMessage.getNotification().getTitle(),
remoteMessage.getNotification().getBody());
} else {
String currentUserEmail = "";
FirebaseAuth auth = FirebaseAuth.getInstance();
if (auth.getCurrentUser() != null && auth.getCurrentUser().getEmail() != null) {
currentUserEmail = auth.getCurrentUser().getEmail();
}

String userName = remoteMessage.getData().get(Constants.KEY_USER_DISPLAY_NAME);
String userEmail = remoteMessage.getData().get(Constants.KEY_USER_EMAIL);
String chatKey = remoteMessage.getData().get(Constants.KEY_CHAT_KEY);
String deviceId = remoteMessage.getData().get(Constants.KEY_USER_FCM_DEVICE_ID);
String deviceIdSender = remoteMessage.getData().get(Constants.KEY_USER_FCM_DEVICE_ID_SENDER);
String title = remoteMessage.getData().get(Constants.KEY_MSG_TITLE);
String msg = remoteMessage.getData().get(Constants.KEY_MSG);
String msgKey = remoteMessage.getData().get(Constants.KEY_MSG_KEY);

/*if (chatKey.equals(ConstantsFirebase.FIREBASE_LOCATION_CHAT_GLOBAL)) {
title = String.format("%s- %s", title, ConstantsFirebase.CHAT_GLOBAL_HELPER);
} else {*/
if (!currentUserEmail.equals(Utils.decodeEmail(userEmail))) {
setMessageReceived(FirebaseDatabase.getInstance().getReference()
.child(ConstantsFirebase.FIREBASE_LOCATION_CHAT).child(chatKey).child(msgKey)
.child(ConstantsFirebase.FIREBASE_PROPERTY_MESSAGE_STATUS));
}
/* }*/

boolean notificationIsActive = PreferenceManager.getDefaultSharedPreferences(this)
.getBoolean(Constants.KEY_PREF_NOTIFICATION, false);
if (auth.getCurrentUser() != null && notificationIsActive) {
if (!currentUserEmail.equals(Utils.decodeEmail(userEmail))) {

Utils.setAdditionalData(new PushNotificationObject
.AdditionalData(title, msg, chatKey, msgKey, userName,
userEmail, deviceId, deviceIdSender));
sendNotification(title, msg);
}
}
}
}

在这里,我将 Remotemessage 直接视为 JSONObject,但它以 bundle jsonstring 的形式出现。我该如何解析它?

输出:

Bundle[{gcm.notification.USER_DISPLAY_NAME=ishku sukshi, google.sent_time=1512190657773, gcm.notification.TITLE=ishku sukshi, gcm.notification.USER_FCM_DEVICE_ID=fXLDo7zU7c0:APA91bFx0sIGwIZ9jIm7xi7QvSrWKrL29uWJnNT0jujlyVHTScUteuRZ37nB-FgEeBXokZdQfmyGKhhRLjCILraS8sTif4p6DRJ_jZkNlh-J_yhKTAU3WnBYzGBtlaTorcAJhDtd1AIy, gcm.notification.CHAT_KEY=-L-FVx8eZBuz-QIsnXvx, from=1028795933953, gcm.notification.USER_EMAIL=ishkumihu@gmail,com, google.message_id=0:1512190657780774%bfd1fc79bfd1fc79, gcm.notification.MSG_KEY=messageKey, gcm.notification.MSG=, gcm.notification.USER_FCM_DEVICE_ID_SENDER=AAAA74kEJQE:APA91bHN5lJf0S8KNXzhU4XL1rz1rqyZ6ziY4UghZudtW6iH84ytQksWMSvSKsaBqQEsw7P2txk-yTGp5DOYElb7pdg8VFgj8wecJUcsPKJ6JCASCO_ihXh6xpo3a2aDuw8HnHPvL0Mr, collapse_key=com.sukshi.sukshichat}]

实际上 gcm.notification appending each key 也不应该来,我不知道为什么会这样。

enter image description here

最佳答案

实际上您是从 RemoteMessage#getData() 方法获取 Map 对象。所以如果你需要一个 json 对象,你可以像下面那样自己创建它

JSONObject json = new JSONObject();
//data is RemoteMessage#getData();
Set<String> keys = data.keySet();
for (String key : keys) {
try {
json.put(key, JSONObject.wrap(data.get(key)));
} catch(JSONException e) {
//Handle exception here
}
}

关于javascript - 将 Remotemessage 中的 JSONString Bundle 解析为 JSON 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47605001/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com