【问题标题】:When Notification is Clicked all other notification are also gone单击通知时,所有其他通知也消失了
【发布时间】:2020-08-21 06:06:44
【问题描述】:

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

    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);

【问题讨论】:

    标签: android android-studio push-notification android-notifications


    【解决方案1】:

    关于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()
    

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

    【讨论】:

    • 好的,先生@snachmsm
    • 我们可以区分消息正文中的通知有一个 id 我需要那个 id 并打开另一个活动是可能的吗?
    • 将此 id 添加到您的 IntentBundle,例如 intent.putExtra("ticketid", ticketid);。在Activity 中,您可以使用getIntent(),接下来您可以访问此Bundle(通常称为extras
    猜你喜欢
    • 2015-07-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多