通过使用remoteMessage传递自定义对象

时间:2020-09-12 14:35:24

标签: java android firebase-cloud-messaging

我创建了一个聊天室,其中包含多种通知。

在常规通知中,我像字符串一样传递数据,然后使用remoteMessage.getData().get( "XX" )来获取它们。

但是,其中一个通知需要自定义对象。当我尝试在其上使用.get()时,出现错误,提示它必须是字符串。

然后如何将唯一数据传递到我的FirebaseMessagingService

以下是我在代码中如何使用它的示例。

这是我创建通知的一部分:

JSONObject json = new JSONObject();
json.put( "to", userDeviceIdKey );
json.put( "priority", "high" );
JSONObject info = new JSONObject();
info.put( "title", title );
info.put( "body", body );
info.put( "uniqueID", uniqueID );
info.put("MyUniqueObject", uniqueObject);
info.put( "NOTIFY_STYLE", "NOTIFY" );
json.put( "data", info );

OutputStreamWriter wr = new OutputStreamWriter( conn.getOutputStream() );
wr.write( json.toString() );
wr.flush();

并在我的FirebaseMessagingService中:

notifyAvailable( remoteMessage.getData().get( "title" ), remoteMessage.getData().get( "body" ), remoteMessage.getData().get( "uniqueID" ), remoteMessage.getData().get( "MyUniqueObject" ) );

谢谢。

0 个答案:

没有答案