推送通知未在Android上显示

时间:2020-11-11 16:45:42

标签: java android firebase push-notification notifications

我正在应用程序上实现推送通知。不幸的是,实现它们之后,这些功能未在某些设备上显示。通常,我可以在MIUI设备上尝试该应用程序,并且通知会正确显示。相反,在android stock(例如模拟器)上,这些未显示。我确定通知会到达设备,因为显示通知的函数已被调用,但未显示任何内容。你能帮我吗?

谢谢!

private final String NOTIFICATION_CHANNEL_ID = "FALLINLED_CHAT_CHANNEL";
private final String GROUP_KEY_CHAT = "FALLINLED_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("Fall in Led")
                        .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 个答案:

答案 0 :(得分:0)

已解决。我想在经理上添加通知渠道。

    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);
    }