推送通知未在 android 上显示
Posted
技术标签:
【中文标题】推送通知未在 android 上显示【英文标题】:Push Notifications are not showing on android 【发布时间】:2021-02-23 16:07:58 【问题描述】:我正在我的应用程序上实现推送通知。不幸的是,在实现它们之后,这些不会在某些设备上显示。一般来说,我可以在 MIUI 设备上试用该应用程序,并且可以正确显示通知。相反,在 android stock(例如模拟器)上,这些没有显示。我确定通知会到达设备上,因为调用了显示通知的函数,但没有显示任何内容。你能帮帮我吗?
谢谢!
private final String NOTIFICATION_CHANNEL_ID = "XXXXX_CHAT_CHANNEL";
private final String GROUP_KEY_CHAT = "XXXXX_GROUP_CHANNEL";
private static NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
private static int messageRecived = 0;
private void SendNotification(String body, String title)
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
intent.putExtra("firstKeyName","FirstKeyValue");
PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
if(!isNotificationVisible())
messageRecived = 0;
inboxStyle = null;
inboxStyle = new NotificationCompat.InboxStyle();
Notification summaryNotification;
if(messageRecived == 0)
inboxStyle.addLine(title +": " + body);
messageRecived += 1;
summaryNotification =
new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID)
.setContentTitle(title)
.setContentText(body)
.setSmallIcon(R.drawable.ic_logo)
.setGroup(GROUP_KEY_CHAT)
.setGroupSummary(true)
.setAutoCancel(true)
.setContentIntent(pendingIntent)
.build();
else
inboxStyle.addLine(title +": " + body);
inboxStyle.setBigContentTitle("Messaggi");
messageRecived += 1;
summaryNotification =
new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID)
.setContentTitle("xxxxxxx")
.setContentText("Ci sono " + messageRecived + " nuovi messaggi")
.setSmallIcon(R.drawable.ic_logo)
.setStyle(inboxStyle)
.setGroup(GROUP_KEY_CHAT)
.setGroupSummary(true)
.setAutoCancel(true)
.setContentIntent(pendingIntent)
.build();
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
notificationManager.notify(MessageHandler.getInstance().getNotificationID(), summaryNotification);
【问题讨论】:
【参考方案1】:解决了。我错过了在经理上添加通知渠道。
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
int importance = NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel channel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "CHAT_CHANNEL", importance);
channel.setDescription("NOTIFICATION CHANNEL CHAT");
NotificationManager notificationManager = getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(channel);
【讨论】:
以上是关于推送通知未在 android 上显示的主要内容,如果未能解决你的问题,请参考以下文章