4

我正在使用 FCM 在 8.1 版本的 Android 设备上发送通知。我正在尝试实现通知的分组,并且我已经实现了。

每当第一个通知在状态栏中并且第二个通知到达时,第一个通知就会消失。从病房的第二个通知开始,所有通知都按预期显示在组中。

如何解决?

在下面发布代码狙击手。

if ("FirstTime".equals(new SharedPrefsOperations(this).getPreferencesData("ActiveNotifs"))) {

                    // I AM EXECUTING THIS ONLY AT FIRST TIME , WHEN THE FIRST NOTIFICATION ARRIVES.

                    String tempData = new SharedPrefsOperations(this).getPreferencesData("ActiveNotifs");

            Notification notification = new NotificationCompat.Builder(this, "MYAPP")
                    .setSmallIcon(R.mipmap.ic_static_notif)
                    .setContentTitle(content.getTitle())
                    .setContentText(tempMsg)
                    .setAutoCancel(true)
                    .setContentIntent(pendingIntent)
                    .setGroup(notifGroup)
                    .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
                    .setChannelId(channel_id)
                    .setStyle(inboxStyle)
                    .setGroupSummary(true)
                    .build();

            notificationManager.notify(id, notification);
            new SharedPrefsOperations(this).storePreferencesData("ActiveNotifs", "1");

        } else {
                     // I AM EXECUTING THIS FROM SECOND NOTIFICATION ONWARDS
                    String tempData = new 

        SharedPrefsOperations(this).getPreferencesData("ActiveNotifs");


            Notification notification = new NotificationCompat.Builder(this, "MYAPP")
                    .setSmallIcon(R.mipmap.ic_static_notif)
                    .setContentTitle(content.getTitle())
                    .setContentText(tempMsg)
                    .setAutoCancel(true)
                    .setContentIntent(pendingIntent)
                    .setGroup(notifGroup)
               .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
                    .setChannelId(channel_id)
                    .setStyle(inboxStyle)
                    .build();

            notificationManager.notify(id, notification);
            new SharedPrefsOperations(this).storePreferencesData("ActiveNotifs", "1");

        }
4

1 回答 1

0

如果您使用相同id的通知,它将不起作用。你需要为此使用不同id

Android 8.0 (Oreo) 以上的更新通知渠道是强制性的。所以像这样添加通知通道。这应该在通知之前完成。

 val mNotificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager

// Android O requires a Notification Channel.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    val name = context.getString(R.string.app_name)
    // Create the channel for the notification
    val mChannel = NotificationChannel("channel_id_sms", name, NotificationManager.IMPORTANCE_DEFAULT)
    // Set the Notification Channel for the Notification Manager.
    mNotificationManager.createNotificationChannel(mChannel)
}
于 2019-01-07T09:11:56.917 回答