Remove Notification when addAction called?

天涯浪子 提交于 2021-02-07 02:57:51

问题


I'm adding two action buttons to my notification, when I click either of them they perform the action desired however the notification remains in my notification drawer. I know it's possible to remove the notification from the notification drawer when an action button is clicked as that is how Gmail functions. If I click the main notification it opens the app and removes the notification from the notification drawer.

Here is a snippet of my code:

Intent completeIntent = new Intent(getApplicationContext(), MarkComplete.class);
        completeIntent.putExtra("rowid", inrowid);
        completeIntent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);

PendingIntent markCompleteIntent = PendingIntent.getActivity(getApplicationContext(), inrow, completeIntent, PendingIntent.FLAG_UPDATE_CURRENT);

builder.setSmallIcon(R.drawable.ic_launcher)
                .setContentTitle("Title")
                .setContentText("text")
                .setContentIntent(notificationReleaseIntent)
                .setPriority(Notification.PRIORITY_HIGH)
                .setAutoCancel(true)
                .addAction(R.drawable.complete, "Mark Complete", markCompleteIntent);

Edit - As zionpi pointed out I needed to call notification.cancel(); to remove the notification once the addAction button had been clicked. I simply added this method to my classes where the PendingIntent was pointing.

public static void CancelNotification(Context ctx, int notifyId) {
    String  s = Context.NOTIFICATION_SERVICE;
    NotificationManager mNM = (NotificationManager) ctx.getSystemService(s);
    mNM.cancel(notifyId);
}

回答1:


So you want to remove your notification, the preliminary is to have a notification id.
then invoke method similar as below to eliminate it.

 public static void CancelNotification(Context ctx, int notifyId) {
        String  s = Context.NOTIFICATION_SERVICE;
        NotificationManager mNM = (NotificationManager) ctx.getSystemService(s);
        mNM.cancel(notifyId);
    }

You may want to refer to this and this post alternatively.



来源:https://stackoverflow.com/questions/18335037/remove-notification-when-addaction-called

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!