Диспетчер уведомлений создает только один канал уведомлений, но мне нужно создать три канала

# #java #firebase-cloud-messaging #android-notifications #notificationmanager #notification-channel

Вопрос:

в моем приложении я создал 3 канала уведомлений (Подписывайтесь, комментируйте, Нравится) для получения уведомлений Firebase Cloud Messaging .

Когда я запускаю приложение и захожу в Настройки > Приложения >> Мое приложение >>> Уведомления о приложениях>>> , я обнаружил, что он показывает только один канал уведомлений (например. Следите за каналом), а два других канала не отображаются …

Но, например, когда я получаю уведомление от канала комментариев, он обновляет текущий канал каналом комментариев, а когда я получаю уведомление от канала «Нравится«, он обновляет текущий канал каналом «Нравится» и так далее..

так почему же он не показывает три канала как отдельные каналы одновременно ?!

Вот Code I work with :

 public class MyFireBaseMessagingService extends FirebaseMessagingService {

    String title, content, type;

    @Override
    public void onMessageReceived(@NonNull RemoteMessage remoteMessage) {
        super.onMessageReceived(remoteMessage);


        title = remoteMessage.getData().get("Notif_Title");
        content= remoteMessage.getData().get("Notif_Content");
        type = remoteMessage.getData().get("Type");
        

        if (type.equals("Follow")) {

            NotificationManager manager1 = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
         
            Intent result = new Intent(this, MainActivity.class);
            PendingIntent resultPendingIntent = PendingIntent.getActivity(this, 0, result, PendingIntent.FLAG_UPDATE_CURRENT);

            NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext())
                    .setSmallIcon(R.drawable.notification_icon)
                    .setContentTitle(title)
                    .setContentText(postContent)
                    .setSound(Settings.System.DEFAULT_NOTIFICATION_URI)
                    .setContentIntent(resultPendingIntent)
                  
            //to work on android 8 and high..
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

                builder.setChannelId("YOUR_PACKAGE_NAME");
                builder.setSmallIcon(R.drawable.logo);

                NotificationChannel channel = new NotificationChannel(
                        "YOUR_PACKAGE_NAME",
                        "Follow",
                        NotificationManager.IMPORTANCE_HIGH);

                manager1.createNotificationChannel(channel);


            }

            manager1.notify(0, builder.build());

        } else if (type.equals("Comment")) {

            NotificationManager manager2 = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

            Intent result = new Intent(this, MainActivity.class);
            PendingIntent resultPendingIntent = PendingIntent.getActivity(this, 1, result, PendingIntent.FLAG_UPDATE_CURRENT);

             NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext())
                    .setSmallIcon(R.drawable.notification_icon)
                    .setContentTitle(title)
                    .setContentText(content)
                    .setSound(Settings.System.DEFAULT_NOTIFICATION_URI)
                    .setContentIntent(resultPendingIntent)
                  
  //to work on android 8 and high..
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

                builder.setChannelId("YOUR_PACKAGE_NAME");
                builder.setSmallIcon(R.drawable.logo);

                NotificationChannel channel = new NotificationChannel(
                        "YOUR_PACKAGE_NAME",
                        "Comment",
                        NotificationManager.IMPORTANCE_HIGH);

                manager3.createNotificationChannel(channel);
                }

            manager3.notify(1, builder.build());


        } else if (type.equals("Like")) {
            Intent result = new Intent(this, MainActivity.class);
         
            PendingIntent resultPendingIntent = PendingIntent.getActivity(this, 2, result, PendingIntent.FLAG_UPDATE_CURRENT);

            NotificationManager manager3 = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);


                NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext())
                        .setSmallIcon(R.drawable.notification_icon)
                        .setContentTitle(title)) 
                        .setContentText(content)
                        .setSound(Settings.System.DEFAULT_NOTIFICATION_URI)
                        .setContentIntent(resultPendingIntent)
                     

                //to work on android 8 and high..
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

                    builder.setChannelId("YOUR_PACKAGE_NAME");
                    builder.setSmallIcon(R.drawable.logo);

                    NotificationChannel channel = new NotificationChannel(
                            "YOUR_PACKAGE_NAME",
                            "Like",
                            NotificationManager.IMPORTANCE_HIGH);

                    manager3.createNotificationChannel(channel);

                }

                manager3.notify(2, builder.build());

            }
    }
}

 

Кто-нибудь может мне помочь…

Ответ №1:

Я знаю, как это решить … Вам следует изменить этот код

      builder.setChannelId("YOUR_PACKAGE_NAME");
     builder.setSmallIcon(R.drawable.logo);
 

В конкретные channel id для каждого из них, такие как :

     builder.setChannelId("com.myapp.first_notification");
    builder.setSmallIcon(R.drawable.logo);