我试图知道按下了哪个按钮,所以我在onReceive上执行此操作
Log.e(TAG, "Clicked " + extras.getInt("ACTION"));
而且我总是,无论我按哪个按钮,都会获得3 (ActionEnum.GO_TO_REMINDERS)
setContentIntent
。
另一个问题是除非我按下通知伙伴,否则通知不会关闭,但是当我按下按钮时它没有关闭。
public void createNotification(Context context, Reminder reminder) {
// Build notification
Notification noti = new Notification.Builder(context)
.setContentTitle(reminder.getDisplayString())
.setContentText("Pick Action")
.setSmallIcon(R.drawable.icon_remider)
.setContentIntent(
getPendingAction(context, reminder,
ActionEnum.GO_TO_REMINDERS))
.addAction(R.drawable.icon, "Take",
getPendingAction(context, reminder, ActionEnum.TAKE))
.addAction(R.drawable.icon, "Snooze",
getPendingAction(context, reminder, ActionEnum.SNOOZE))
.addAction(R.drawable.icon, "Remove",
getPendingAction(context, reminder, ActionEnum.REMOVE))
.build();
NotificationManager notificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
// hide the notification after its selected
noti.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(0, noti);
}
public PendingIntent getPendingAction(Context context, Reminder reminder,
ActionEnum action) {
// Prepare intent which is triggered if the
// notification is selected
Intent intent = new Intent(context, RemindersReceiver.class);
intent.putExtra("ID", reminder.getIntId());
intent.putExtra("CLICK", true);
intent.putExtra("ACTION", action.getValue());
Log.e(TAG, "set action : " + action.getValue());
return PendingIntent.getBroadcast(context, 0, intent, 0);
}
答案 0 :(得分:4)
getPendingAction()
中的代码将始终返回相同的PendingIntent
。每次调用此方法时,您都不会创建单独的PendingIntent
。要确保每个调用都创建单独的PendingIntent
,您需要使Intent
唯一。您可以通过在Intent
中设置操作来完成此操作,如下所示:
intent.setAction(action.name());
为了确保具有相同ACTION的任何旧PendingIntent
被最新的附加内容覆盖,我还会像这样呼叫getBroadcast()
:
return PendingIntent.getBroadcast(context, 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT);