仅在响应中获取消息类型“数据”。但应用程序没有在收到通知时调用 onMessageReceive 方法。
{
"data" : {
"body" : "Body of the notification example",
"action_id" : "4"
}
}
}
最佳答案
你做过这样的事吗?这是为了处理数据消息。
public class FirebaseMessaging extends FirebaseMessagingService {
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
showNotification(remoteMessage.getData());
}
private void showNotification(Map<String, String> data) {
String title = data.get("title");
String body = data.get("body");
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
String NOTIFICATION_CHANNEL_ID = "ChannelID";
if(notificationManager != null) {
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel notificationChannel = new NotificationChannel(
NOTIFICATION_CHANNEL_ID, "NotificationManager",
NotificationManager.IMPORTANCE_DEFAULT);
notificationChannel.setDescription("My Channel");
notificationManager.createNotificationChannel(notificationChannel);
}
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID);
notificationBuilder
.setAutoCancel(true)
.setDefaults(Notification.DEFAULT_ALL)
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.drawable.supportop_icon)
.setContentTitle(title)
.setContentText(body);
// Handle notification click
PendingIntent intent = PendingIntent.getActivity(this, 0, new Intent(this, Activity.class), 0);
notificationBuilder.setContentIntent(intent);
notificationManager.notify(id, notificationBuilder.build());
}
}
}
关于当应用程序处于后台时,Android Firebase 通知点击不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58237470/