0

在我的应用程序中,我创建了 3 个通知渠道(关注评论点赞)来接收来自Firebase Cloud Messaging.

当我运行应用程序并转到Settings > Apps > My App > App Notifications时,我发现它只显示一个通知频道(例如关注频道),而其他两个频道没有显示...

但是,例如,当我从评论频道收到通知时,它会使用评论频道更新当前频道,当我收到来自赞频道的通知时,它会使用赞频道等更新当前频道。

那么为什么它不会同时将三个频道显示为单独的频道呢?!

这里是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());

            }
    }
}

谁能帮我...

4

1 回答 1

0

我知道如何解决它...您应该更改此代码

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

具体channel id到每个喜欢:

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

于 2021-09-04T12:07:40.073 回答