Android推送通知无显示



我使用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 Cloud Messaging:

const data = {
token: context.params.token,
data: {
title: document.username,
body: document.message,
conversation: document.conversationid,
},
android: {
notification: {
channel_id: "Messages_Channel_ID",
},
},
};

所有sdk中的通知都是这样返回的

编辑1:

做了一些测试,我意识到onreceived方法从未被调用,即使在后台或前台!即使在manifest中声明了类!但只是显示空白通知,因为我有颜色和图标默认声明在清单太。如果没有声明,可能不显示任何内容。

很奇怪,onmessagerreceived方法至少应该在前台调用。请检查通知的样式,并尝试我在此存储库中的示例代码。

public class PushNotificationHandbookService extends FirebaseMessagingService {
public static final String TAG = "PushHandbookService";
@Override
public void onMessageReceived(@NonNull RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
// You will receive the push notifications here!
Log.d(TAG, "From: " + remoteMessage.getFrom());
// Check if message contains a notification payload.
if (remoteMessage.getNotification() != null) {
Log.d(TAG, "Title: " + remoteMessage.getNotification().getTitle() +
"Body: " + remoteMessage.getNotification().getBody());
}
// Check if message contains a data payload.
if (remoteMessage.getData().size() > 0) {
Log.d(TAG, "Title: " + remoteMessage.getData().get("title") +
"Body: " + remoteMessage.getData().get("body"));
sendNotification(remoteMessage);
}
}
...
private void sendNotification(RemoteMessage remoteMessage) {
String title = remoteMessage.getData().get("title");
String messageBody = remoteMessage.getData().get("body");
String score = remoteMessage.getData().get("score");
String country = remoteMessage.getData().get("country");
Intent intent = new Intent(this, PushReceiverActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("score", score);
intent.putExtra("country", country);
@SuppressLint("UnspecifiedImmutableFlag")
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
intent, PendingIntent.FLAG_ONE_SHOT);
String channelId = getString(R.string.default_notification_channel_id);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(this, channelId)
.setSmallIcon(R.drawable.ic_stat_name)
.setContentTitle(title)
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// Since android Oreo notification channel is needed.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(channelId,
"Channel human readable title",
NotificationManager.IMPORTANCE_DEFAULT);
notificationManager.createNotificationChannel(channel);
}
notificationManager.notify(0, notificationBuilder.build());
}
...
}

使用这样的消息:

var message = {
notification: {
title: title,
body: text
},
data: {
title: title,
body: text,
score: '4.5',
country: 'Canada'
},
android: {
notification: {
priority: 'high',
sound: 'default',
clickAction: '.PushReceiverActivity'
},
},
tokens: registrationTokens
};

最新更新