我试图从我的ArrayMap获取消息,但我无法访问ReceiveMessage包。 我试图直接访问Map,但这是非常错误的
我的代码
public class FbService extends FirebaseMessagingService {
public FbService() {
}
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
// TODO(developer): Handle FCM messages here.
// If the application is in the foreground handle both data and notification messages here.
// Also if you intend on generating your own notifications as a result of a received FCM
// message, here is where that should be initiated. See sendNotification method below.
Map<String, String> params = remoteMessage.getData();
String message = params.get(3);
Log.d("FbService", "Notification Message Body: " + message);
}
}
答案 0 :(得分:1)
感谢@Pritish Joshi
我找到了这个答案并帮助了我很多 ,这是代码片段
您可以使用地图
的形式获取数据public void onMessageReceived(RemoteMessage remoteMessage)
{
Log.e("dataChat",remoteMessage.getData().toString());
try
{
Map<String, String> params = remoteMessage.getData();
JSONObject object = new JSONObject(params);
Log.e("JSON_OBJECT", object.toString());
}
}
确保从服务器上以正确的格式发送数据,即在&#34;数据&#34;键
这是演示Json文件
{
"to": "registration_ids",
"data": {
"key": "value",
"key": "value",
"key": "value",
"key": "value"
}
}
参考:
答案 1 :(得分:0)
使用getJson获取bundle值,现在你将构造的值作为Json格式的String,
private String message = getJson(bundle);
将字符串json转换为JSONObject,
JSONObject object = new JSONObject(message );
从对象获取像object.optInt(“NAME”);
这样的值答案 2 :(得分:0)
public class MyFirebaseMessagingService extends FirebaseMessagingService {
private static final String TAG = "MyFirebaseMsgService";
String title = "";
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
send_Notification(remoteMessage);
}
private void send_Notification(RemoteMessage remoteMessage) {
String notifications_text = "";
String title = remoteMessage.getNotification().getTitle();
notifications_text =remoteMessage.getNotification().getBody();
Intent resultIntent = new Intent(this, SplashActivity.class);
TaskStackBuilder TSB = TaskStackBuilder.create(this);
TSB.addParentStack(SplashActivity.class);
TSB.addNextIntent(resultIntent);
PendingIntent resultPendingIntent = TSB.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder nb = new NotificationCompat.Builder(this);
nb.setSmallIcon(R.drawable.logo);
nb.setContentIntent(resultPendingIntent);
nb.setAutoCancel(false);
nb.setLargeIcon(BitmapFactory.decodeResource(getResources(),
R.drawable.logo2));
nb.setContentTitle(getString(R.string.app_name) + " " + title + " " + getString(R.string.str_notification));
NotificationManager mNotificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(random(), nb.build());
}
int random() {
Random rand = new Random();
int randomNum = 1 + rand.nextInt((100000 - 1) + 1);
return randomNum;
}
}
试试这个。它可能会帮助你。