Android 调用通知后未取消通知。取消
我使用操作按钮创建通知。单击“操作”按钮时,将调用广播接收器。我正在意向中传递通知ID 在广播接收器中,我执行以下操作Android 调用通知后未取消通知。取消,android,Android,我使用操作按钮创建通知。单击“操作”按钮时,将调用广播接收器。我正在意向中传递通知ID 在广播接收器中,我执行以下操作 int notifId = intent.getIntExtra(Constants.NOTIF_ID, 0); NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
int notifId = intent.getIntExtra(Constants.NOTIF_ID, 0);
NotificationManager mNotificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.cancel(notifId);
这就是我生成通知的方式
int notifId=Util.random.nextInt(9000);
Intent minent=新的Intent(con,NotificationBroadcastReceiver.class);
minent.putExtra(Constants.NOTIF_代码,codeReason);
Minent.putExtra(常数.NOTIF_ID,notifId);
pendingent mpendingent=pendingent.getBroadcast(con,0,minent,0);
NotificationCompat.Builder mBuilder=新建NotificationCompat.Builder(con)
.setSmallIcon(R.drawable.icon)
.setContentTitle(“测试”)
.setPriority(通知.PRIORITY_MAX)
.setAutoCancel(真)
.addAction(R.drawable.ic_发射器,“动作”,mpendingent);
但是,尽管我知道我正在使用代码(使用日志语句),但通知不会被隐藏/取消
为什么呢
startForground()
检查通知是否未与其他服务绑定我找到了答案。我“认为”由于两个挂起的意图具有相同的con和req代码,它们最终修改了它们的意图值。我通过使用2个不同的请求代码来确保唯一的挂起意图来修复它。因此,我得到了相同的notifId您能显示生成通知的代码吗?代码已更新,notifId是随机生成的数字,在Previous中传递。您在哪里设置notifId?因为我认为问题在于您的notifId和取消用于生成的通知的id应该相同。您是否检查了调用
intent.getIntExtra(Constants.NOTIF_id,0)时得到的值代码>为零???我在生成通知的函数中传递它。但是我更新了代码以显示如何生成通知Id
// Creates an explicit intent for an Activity in your app
Intent resultIntent = new Intent(con, MainActivity.class);
PendingIntent resultPendingIntent = PendingIntent.getActivity(
con, 0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT
);
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager =
(NotificationManager) con.getSystemService(Context.NOTIFICATION_SERVICE);
mBuilder.setDefaults(
Notification.DEFAULT_SOUND |
Notification.DEFAULT_VIBRATE |
Notification.DEFAULT_LIGHTS
);
// mId allows you to update the notification later on.
mNotificationManager.notify(notifId, mBuilder.build());