如何保留旧通知'创建新通知后活动的按钮处于活动状态
我正在创建一个包含零件的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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论