
我正在尝试在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); })};在AndroID方面
@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,但它以捆绑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也不应该来,我不知道为什么会这样.
解决方法:
尝试使用Json的字符串表示形式制作JsONObject.将通知更改为服务器端代码上的数据.喜欢 –
fetch('https://fcm.GoogleAPIs.com/fcm/send', { 'method': 'POST', 'headers': { 'Authorization': 'key=' + fromKey, 'Content-Type': 'application/Json' }, 'body': JsON.stringify({ 'data': notification, 'to': toKey }) }).then(function(response) { console.log(response); }).catch(function(error) { console.error(error); })};并在AndroID端获取您的fcm消息-
String body = remoteMessage.getData();JsONObject Json = new JsONObject(body );Log.d("Json", Json.toString());然后,您可以通过从JavaScript分配的键来获取价值.像-Json.get(“ USER_disPLAY_name”);
让我知道进展如何.
总结以上是内存溢出为你收集整理的javascript-解析一个Bundle,它是Remotemessage中的JSONString到JSON对象全部内容,希望文章能够帮你解决javascript-解析一个Bundle,它是Remotemessage中的JSONString到JSON对象所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)