如何解除动作点击通知?

时间:2017-06-30 11:27:58

标签: android

我想在点击通知中的解雇时忽略我的通知。就像我们在刷卡上解雇它一样。

NotificationManager notificationManager = (NotificationManager) ctx
                .getSystemService(Context.NOTIFICATION_SERVICE);

    Intent intent = new Intent(ctx, MainActivity.class);
    intent.putExtra("isFromBadge", false);

    Intent resultIntent = new Intent(ctx, ResultActivity.class);
    TaskStackBuilder stackBuilder = TaskStackBuilder.create(ctx);
    stackBuilder.addParentStack(ResultActivity.class);
    stackBuilder.addNextIntent(resultIntent);
    PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_ONE_SHOT);

    PendingIntent contentIntent = PendingIntent.getActivity(ctx, 0, resultIntent, PendingIntent.FLAG_CANCEL_CURRENT);

  //  PendingIntent contentIntent = PendingIntent.getActivity(ctx, 0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);

    Notification notification = new Notification.Builder(ctx)
            .setContentTitle(
                    ctx.getResources().getString(R.string.app_name))
            .setContentText(message)
            .setWhen(when)
            .setStyle(new Notification.BigPictureStyle().bigPicture(result))
            .setSmallIcon(R.mipmap.ic_launcher)
            .addAction(R.drawable.bell, "Dismiss", resultPendingIntent)
            .addAction(R.drawable.bell, "Share", contentIntent)
            .setLargeIcon(result)
            .setAutoCancel(true)
            .build();

2 个答案:

答案 0 :(得分:3)

试试这个

public void showNotification(Context ctx) {
    int noti_id = 100;
    NotificationManager notificationManager = (NotificationManager) ctx
                .getSystemService(Context.NOTIFICATION_SERVICE);

    Intent actionIntent = new Intent(ctx, ActionReceiver.class);
    actionIntent.putExtra("noti_id", noti_id);

    PendingIntent pendingIntent = PendingIntent.getBroadcast(ctx, noti_id, actionIntent, 
        PendingIntent.FLAG_CANCEL_CURRENT);

    Notification notification = new Notification.Builder(ctx)
                                .setContentTitle("hello")
                                .setContentText("Hello")
                                .addAction(R.drawable.bell, "dismiss", pendingIntent)
                                .setAutoCancel(false)
                                .build();


    notificationManager.notify(noti_id, notification);   
}



public class ActionReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent != null) {
            int noti_id = intent.getIntExtra("noti_id", -1);

            if (noti_id > 0) {
                NotificationManager notificationManager = (NotificationManager) context
                .getSystemService(Context.NOTIFICATION_SERVICE);

                notificationManager.cancel(noti_id);
            }
        }
    }
}

答案 1 :(得分:0)

您可以使用notification_id

创建通知时使用{1}}取消通知

通知 -

NotificationManager

取消通知 -

mNotificationManager.notify(_notificationId /* ID of notification */, mBuilder.build());