首页 > 解决方案 > 单击通知时,所有其他通知也消失了

问题描述

我正在使用 Firebase 生成通知,假设有 3 个通知,如果用户点击任何一个所有通知都消失了,我需要的是当用户点击一个通知而不是其他 2 个通知时应该在通知中这是我的代码 wt我已经做好了。

    Intent intent;
        if (pref.getString("id","").equals("")){
             intent = new Intent(this, Login_Activity.class);
        }else {
             intent = new Intent(this, TicketListActivity.class);

        }
     //   intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, (int) System.currentTimeMillis(), intent, PendingIntent.FLAG_ONE_SHOT);
        String channelId = "Default";

        String msgBody = remoteMessage.getData().get("body");
        String ticketid = remoteMessage.getData().get("ticket_Id");

        Notification notification = new NotificationCompat.Builder(this, channelId)
                .setSmallIcon(R.drawable.app_launch)
                .setContentTitle(remoteMessage.getData().get("title"))
                .setContentText(remoteMessage.getData().get("body")).setAutoCancel(true).setContentIntent(pendingIntent)
                .setStyle(new NotificationCompat.BigTextStyle().bigText(remoteMessage.getData().get("body")))
                .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
                .setOngoing(true)
                .build();

        NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel channel = new NotificationChannel(channelId, "Default channel", NotificationManager.IMPORTANCE_DEFAULT);
            manager.createNotificationChannel(channel);
        }
//        manager.notify((int) ((new Date().getTime() / 1000L) % Integer.MAX_VALUE), notification);
        manager.notify((int) System.currentTimeMillis(), notification);

标签: androidandroid-studiopush-notificationandroid-notifications

解决方案


一些关于PendingIntent.FLAG_ONE_SHOT

指示此 PendingIntent只能使用一次的标志。用于 getActivity(Context, int, Intent, int)、getBroadcast(Context, int, Intent, int) 和 getService(Context, int, Intent, int)。

如果设置,则在调用 send() 后,它将自动为您取消,并且将来通过它发送的任何尝试都将失败。

尝试将此标志更改为FLAG_UPDATE_CURRENT

也避开这条线

(int) System.currentTimeMillis()

currentTimeMillis总有一天它long可能会抛出异常 - 是的,我知道我们还有很多时间直到这一刻,但仍然......使用“真实” id Integer- 因为PendingIntent你可以放0,因为manager.notify我建议你设置一些int id = 0;并增加它与每个发布的通知一起,所以每个下一个都会有id = prevNotificationId + 1(例如SharedPreferences用于存储上次使用的id


推荐阅读