FCM 通知未显示
所以我的通知系统无缘无故无法工作,我尝试了很多 stackoverflow 解决方案,但没有一个真正起作用。我思考了好几个小时,所以请帮忙!
这是 MessagingService.java 类:
public final class MessagingService extends FirebaseMessagingService {
@Override
public void onMessageReceived(@NonNull RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
User user = new User(
remoteMessage.getData().get(Constants.KEY_NAME),
"",
"",
remoteMessage.getData().get(Constants.KEY_FCM_TOKEN),
remoteMessage.getData().get(Constants.KEY_USER_ID)
);
String channelId = "chat_message";
Intent intent = new Intent(this, ChatActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
intent.putExtra(Constants.KEY_USER, user);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, channelId);
builder.setSmallIcon(R.drawable.ic_notification);
builder.setContentTitle(user.getName());
builder.setContentText(remoteMessage.getData().get(Constants.KEY_MESSAGE));
builder.setStyle(new NotificationCompat.BigTextStyle()
.bigText(remoteMessage.getData().get(Constants.KEY_MESSAGE)));
builder.setPriority(NotificationCompat.PRIORITY_HIGH);
builder.setContentIntent(pendingIntent);
builder.setAutoCancel(true);
builder.addAction(R.drawable.ic_notification, "eee", pendingIntent);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
CharSequence channelName = "Chat Message";
String channelDescription = "Used for message notifications";
NotificationChannel channel = new NotificationChannel("", "", NotificationManager.IMPORTANCE_DEFAULT);
channel.setDescription(channelDescription);
NotificationManager notificationChannel = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationChannel.createNotificationChannel(channel);
}
int id = ThreadLocalRandom.current().nextInt(100);
NotificationManagerCompat notificationManagerCompat = NotificationManagerCompat.from(this);
notificationManagerCompat.notify(id, builder.build());
}
}
这是我的 AndroidManifest.xml:
<service
android:name=".firebase.MessagingService"
android:exported="false">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
so my notification system isn't working for no reason, I've tried many stackoverflow solutions but none of them really worked. I have been thinking for many hours, so please help!
here is MessagingService.java class:
public final class MessagingService extends FirebaseMessagingService {
@Override
public void onMessageReceived(@NonNull RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
User user = new User(
remoteMessage.getData().get(Constants.KEY_NAME),
"",
"",
remoteMessage.getData().get(Constants.KEY_FCM_TOKEN),
remoteMessage.getData().get(Constants.KEY_USER_ID)
);
String channelId = "chat_message";
Intent intent = new Intent(this, ChatActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
intent.putExtra(Constants.KEY_USER, user);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, channelId);
builder.setSmallIcon(R.drawable.ic_notification);
builder.setContentTitle(user.getName());
builder.setContentText(remoteMessage.getData().get(Constants.KEY_MESSAGE));
builder.setStyle(new NotificationCompat.BigTextStyle()
.bigText(remoteMessage.getData().get(Constants.KEY_MESSAGE)));
builder.setPriority(NotificationCompat.PRIORITY_HIGH);
builder.setContentIntent(pendingIntent);
builder.setAutoCancel(true);
builder.addAction(R.drawable.ic_notification, "eee", pendingIntent);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
CharSequence channelName = "Chat Message";
String channelDescription = "Used for message notifications";
NotificationChannel channel = new NotificationChannel("", "", NotificationManager.IMPORTANCE_DEFAULT);
channel.setDescription(channelDescription);
NotificationManager notificationChannel = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationChannel.createNotificationChannel(channel);
}
int id = ThreadLocalRandom.current().nextInt(100);
NotificationManagerCompat notificationManagerCompat = NotificationManagerCompat.from(this);
notificationManagerCompat.notify(id, builder.build());
}
}
and here's my AndroidManifest.xml:
<service
android:name=".firebase.MessagingService"
android:exported="false">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论