群组通知不会显示

huangapple 未分类评论57阅读模式
标题翻译

Group notification wont show

问题

以下是翻译好的部分:

我正在使用 FCM 在用户收到新消息时发送通知。

我希望从我的应用程序中创建一个分组通知,以便它显示如下:

(图片已省略)

然而,由于某种原因,我收到的每个新通知都会删除其他通知,因此它始终只显示最新的通知。

我的代码如下:

public class MyFirebaseMessagingService extends FirebaseMessagingService {

    private FirebaseAuth auth;
    private FirebaseFirestore db;

    private static String GROUP_KEY = "NOTIFICATION";

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {

        createNotificationChannel();
        notifyThis(remoteMessage.getData().get("title"), remoteMessage.getData().get("body"), remoteMessage.getData().get("var1"), remoteMessage.getData().get("var2"), remoteMessage.getData().get("var3"));

    }

    private void createNotificationChannel() {

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            CharSequence name = "ABC";
            String description = "ABCDE";
            int importance = NotificationManager.IMPORTANCE_DEFAULT;
            NotificationChannel channel = new NotificationChannel("191919", name, importance);
            channel.setDescription(description);

            NotificationManager notificationManager = getSystemService(NotificationManager.class);
            notificationManager.createNotificationChannel(channel);

        }
    }

    public void notifyThis(String title, String message, String var1, String var2, String var3) {

        Intent intent = new Intent(this, ChatActivity.class);

        intent.putExtra("ChatID", var1);
        intent.putExtra("ReceiverID", var2);
        intent.putExtra("itemID", var3);

        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);

        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this, "191919")
                .setSmallIcon(R.drawable.ic_launcher_custom_background)
                .setContentTitle(title)
                .setContentText(message)
                .setContentIntent(pendingIntent)
                .setPriority(NotificationCompat.PRIORITY_DEFAULT)
                .setGroup(GROUP_KEY)
                .setAutoCancel(true);

        NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);

        notificationManager.notify(0, mBuilder.build());

    }

}

是否有任何原因导致这种情况?是否有可能是因为 channelId

另外,我看到我可能需要构建多个通知,但是如何根据用户收到的消息数量来动态生成通知呢?

假设我收到了 2 条通知,然后我又收到了一条消息,但我只构建了 2 条通知,它将如何显示第三条通知?

谢谢。

英文翻译

I am using FCM to send notifications between users when they receive a new message.

I had like to make a group notification from my app so it will show like this:

群组通知不会显示

However, for some reason every new notification that I get, it deletes the other so it always shows only the latest notification.

My code is:

public class MyFirebaseMessagingService extends FirebaseMessagingService {

    private FirebaseAuth auth;
    private FirebaseFirestore db;

    private static String GROUP_KEY = "NOTIFICATION";

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {

        createNotificationChannel();
        notifyThis(remoteMessage.getData().get("title"),remoteMessage.getData().get("body"),remoteMessage.getData().get("var1"),remoteMessage.getData().get("var2"),remoteMessage.getData().get("var3"));
        
    }

    private void createNotificationChannel() {

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            CharSequence name = "ABC";
            String description = "ABCDE";
            int importance = NotificationManager.IMPORTANCE_DEFAULT;
            NotificationChannel channel = new NotificationChannel("191919", name, importance);
            channel.setDescription(description);

            NotificationManager notificationManager = getSystemService(NotificationManager.class);
            notificationManager.createNotificationChannel(channel);

        }
    }

    public void notifyThis(String title, String message, String var1, String var2, String var3) {

        Intent intent = new Intent(this, ChatActivity.class);

        intent.putExtra("ChatID",var1);
        intent.putExtra("ReceiverID",var2);
        intent.putExtra("itemID",var3);

        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);

        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this, "191919")
                .setSmallIcon(R.drawable.ic_launcher_custom_background)
                .setContentTitle(title)
                .setContentText(message)
                .setContentIntent(pendingIntent)
                .setPriority(NotificationCompat.PRIORITY_DEFAULT)
                .setGroup(GROUP_KEY)
                .setAutoCancel(true);

        NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);

        notificationManager.notify(0, mBuilder.build());

    }

}

Is there any reason? Any chance that it is because of the channelId?

Also, I saw that I might need to build multiple notifications however how can I make it dynamic based on the amount of messages that the user gets?

Lets set I get 2 notifications and then I get one more message but I built only 2 notifications, how it will show me the third?

Thank you

huangapple
  • 本文由 发表于 2020年5月30日 19:37:31
  • 转载请务必保留本文链接:https://java.coder-hub.com/62101862.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定