简体   繁体   English

Notification.Builder添加操作

[英]Notification.Builder add action

I'm trying to know which button is pressed so I do this on onReceive 我想知道按下了哪个按钮,所以我在onReceive上执行此操作

Log.e(TAG, "Clicked " + extras.getInt("ACTION"));

and I always, no matter which button I press, get 3 (ActionEnum.GO_TO_REMINDERS) which is the setContentIntent . 我总是,无论我按哪个按钮,都得到3 (ActionEnum.GO_TO_REMINDERS)这是setContentIntent

another issue is that the notification is not closed unless I press on the notification buddy, but it's not closed when I press the button. 另一个问题是通知没有关闭,除非我按下通知伙伴,但是当我按下按钮时它没有关闭。

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

Your code in getPendingAction() will always return the same PendingIntent . 您在getPendingAction()代码将始终返回相同的PendingIntent You are not creating a separate PendingIntent each time you call this method. 每次调用此方法时,您都不会创建单独的PendingIntent To ensure that each call creates a separate PendingIntent , you need to make the Intent unique. 要确保每个调用都创建一个单独的PendingIntent ,您需要使Intent唯一。 You can do this by setting the ACTION in the Intent , like this: 您可以通过在Intent设置ACTION来完成此操作,如下所示:

intent.setAction(action.name());

To ensure that any old PendingIntent s with the same ACTION are overwritten by the latest extras, I would also call getBroadcast() like this: 为了确保具有相同ACTION的任何旧PendingIntent被最新的附加内容覆盖,我还会像这样调用getBroadcast()

return PendingIntent.getBroadcast(context, 0, intent,
                    PendingIntent.FLAG_UPDATE_CURRENT);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM