【发布时间】:2021-12-01 18:39:33
【问题描述】:
我正在使用 FCM,一切正常,通知是使用“数据”发送的,但是在显示时会发生这种情况。我真的不知道该怎么办了。
public class CloudMessaging extends FirebaseMessagingService {
String NOTIFICATION_CHANNEL_ID = "Messages_Channel_ID";
@Override
public void onMessageReceived(@NonNull RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
createNotificationChannel();
Map<String, String> data = remoteMessage.getData();
final String title = data.get("title");
final String body = data.get("body");
final String conversation = data.get("conversation");
if (conversation == null) return;
NotificationCompat.Builder builder = new
NotificationCompat.Builder(getApplicationContext(), NOTIFICATION_CHANNEL_ID);
builder.setColor(Color.CYAN)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(title)
.setContentText(body)
.setAutoCancel(true);
NotificationManager notificationManager = (NotificationManager)
getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(1, builder.build());
}
private void createNotificationChannel() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
CharSequence name = getString(R.string.message_channel_name);
String description = getString(R.string.message_channel_description);
int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel messagesChannel = new
NotificationChannel(getString(R.string.Message_Notification_ID), name, importance);
messagesChannel.setDescription(description);
messagesChannel.enableVibration(true);
messagesChannel.setVibrationPattern(new long[]{1000, 200, 500});
messagesChannel.enableLights(true);
messagesChannel.setLightColor(Color.CYAN);
NotificationManager notificationManager = getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(messagesChannel);
}
}
后端服务器,在 Firebase 云消息传递中:
const data = {
token: context.params.token,
data: {
title: document.username,
body: document.message,
conversation: document.conversationid,
},
android: {
notification: {
channel_id: "Messages_Channel_ID",
},
},
};
All the notifications in all sdk's return like this
编辑 1:
做了一些测试,我意识到,onreceived 方法永远不会被调用,即使是在后台或前台!即使在清单中声明了类!但是只显示空白通知,因为我在清单中也声明了颜色和图标默认值。如果它没有声明,可能什么都不显示。
【问题讨论】:
标签: android firebase notifications firebase-cloud-messaging push