如何保留旧通知'创建新通知后活动的按钮处于活动状态

发布于 2025-02-07 19:45:56 字数 2469 浏览 1 评论 0原文

我正在创建一个包含零件的Android Java应用程序,该零件会创建以确认或拒绝用户的通知,并且用户不能忽略

通知工作完美,,但是如果我创建另一个通知,则旧的通知按钮不再起作用

这会导致用户试图确认或拒绝任何旧通知(通过他们的按钮'confrm'& “拒绝”),按钮必须执行的操作不会发生,而只会发生在最后的(最新)通知中,

因此我的问题是如何在创建新的通知后保持旧通知的按钮活动

我使用下面的代码(更改NotificationID),

NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Notification.Builder mbuilder = new Notification.Builder(MainActivity.this);
mbuilder.setContentTitle("Confirmation");
mbuilder.setContentText("Do you want to confirm that you want to wake up at 7 am?");
mbuilder.setSmallIcon(R.drawable.app_icon); 
mbuilder.setDefaults( Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE);
mbuilder.setOngoing(true);
mbuilder.setAutoCancel(false);
Intent NotificationIntent = new Intent(getApplicationContext(), MainActivity.class);
NotificationIntent.setClass(getApplicationContext(), MainActivity.class);
NotificationIntent.putExtra("NotificationID", String.valueOf((long)(NotificationID)));
NotificationIntent.setType("CONFIRMATION=true");
PendingIntent PendingIntent1 = PendingIntent.getActivity(getApplicationContext(), 1, NotificationIntent, Intent.FLAG_ACTIVITY_NEW_TASK);
mbuilder.addAction(R.drawable.app_icon, "CONFIRM", PendingIntent1);
NotificationIntent.setType("CONFIRMATION=false");
PendingIntent PendingIntent2 = PendingIntent.getActivity(getApplicationContext(), 1, NotificationIntent, Intent.FLAG_ACTIVITY_NEW_TASK);
mbuilder.addAction(R.drawable.app_icon, "DENY", PendingIntent2);

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        NotificationChannel channel = new NotificationChannel("1", "Confirmations", NotificationManager.IMPORTANCE_DEFAULT);
        channel.enableLights(true);
        channel.setLightColor(Color.BLUE);
        channel.setShowBadge(true);
        channel.enableVibration(true);
        mbuilder.setChannelId("1");
        if (mNotificationManager != null) {
                mNotificationManager.createNotificationChannel(channel);
            }
} else {
            mbuilder.setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_LIGHTS | Notification.DEFAULT_VIBRATE);
        }
        if (mNotificationManager != null) {
                mNotificationManager.notify((int)(NotificationID), mbuilder.build());
            }

我更改了意图类型以了解单击按钮,因为意图只能包含一种类型,但是如果我添加标志,它将复制,这会在识别时会引起问题启动主动脉后,单击哪个按钮,因此我更容易做到这一点。

谢谢

I'm creating an android java app that contains a part creates notifications to be confirmed or denied by the user, and the notifications cannot be dismissed by the user, only via the app

The problem is that once I create the notification, the action buttons work perfectly, but if I create another notification, the old notifications' buttons don't work anymore

Which causes that if the user tried to confirm or deny any old notification (via their button 'Confrm' & 'Deny'), the actions that the button must perform don't happen, but only happen with the last(newest) notification

So my question is how to keep the old notifications' buttons active after creating a new notification

I use the code below (with changing the NotificationID)

NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Notification.Builder mbuilder = new Notification.Builder(MainActivity.this);
mbuilder.setContentTitle("Confirmation");
mbuilder.setContentText("Do you want to confirm that you want to wake up at 7 am?");
mbuilder.setSmallIcon(R.drawable.app_icon); 
mbuilder.setDefaults( Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE);
mbuilder.setOngoing(true);
mbuilder.setAutoCancel(false);
Intent NotificationIntent = new Intent(getApplicationContext(), MainActivity.class);
NotificationIntent.setClass(getApplicationContext(), MainActivity.class);
NotificationIntent.putExtra("NotificationID", String.valueOf((long)(NotificationID)));
NotificationIntent.setType("CONFIRMATION=true");
PendingIntent PendingIntent1 = PendingIntent.getActivity(getApplicationContext(), 1, NotificationIntent, Intent.FLAG_ACTIVITY_NEW_TASK);
mbuilder.addAction(R.drawable.app_icon, "CONFIRM", PendingIntent1);
NotificationIntent.setType("CONFIRMATION=false");
PendingIntent PendingIntent2 = PendingIntent.getActivity(getApplicationContext(), 1, NotificationIntent, Intent.FLAG_ACTIVITY_NEW_TASK);
mbuilder.addAction(R.drawable.app_icon, "DENY", PendingIntent2);

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        NotificationChannel channel = new NotificationChannel("1", "Confirmations", NotificationManager.IMPORTANCE_DEFAULT);
        channel.enableLights(true);
        channel.setLightColor(Color.BLUE);
        channel.setShowBadge(true);
        channel.enableVibration(true);
        mbuilder.setChannelId("1");
        if (mNotificationManager != null) {
                mNotificationManager.createNotificationChannel(channel);
            }
} else {
            mbuilder.setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_LIGHTS | Notification.DEFAULT_VIBRATE);
        }
        if (mNotificationManager != null) {
                mNotificationManager.notify((int)(NotificationID), mbuilder.build());
            }

I change the intent type to know the clicked button because the intent can contain only one type, but if I add a flag instead, it will duplicate which will cause a problem in recognizing which button is clicked after launching the MainActivity, so it's easier to me to do that.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文