Notification Manager creates only one notification channel but i need to create three channels

683 views Asked by At

in my application, i have created 3 notification channels (Follow, Comment, Like) to receive notifications from Firebase Cloud Messaging.

When i run app and go to Settings > Apps > My App > App Notifications , I found it show only one notification channel (ex. Follow channel) and other two channels are't be shown ...

But when i receive notification from Comment channel for example, it updates current channel with Comment channel and when i receive notification from Like channel, it updates current channel with Like channel and so on..

so why it doesn't show three channels as separated channels at same time ?!

Here is 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());

            }
    }
}

Can Anyone help me...

1

There are 1 answers

0
Mohamed A. Yousef On BEST ANSWER

I know how to solve it ... You should change this code

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

Into specific channel id to each one like :

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