Android 单击通知时如何删除通知

Android 单击通知时如何删除通知,android,notifications,push-notification,Android,Notifications,Push Notification,我可以在点击通知中删除此通知吗 当使用setAutoCancel(true)单击此通知时,我可以取消通知 但单击通知后未清除通知 private void sendNotification(String msg, Bundle extras) { notificationManager = (NotificationManager) this .getSystemService(Context.NOTIFICATION_SERVICE); Intent

我可以在点击通知中删除此通知吗

当使用setAutoCancel(true)单击此通知时,我可以取消通知

但单击通知后未清除通知

private void sendNotification(String msg, Bundle extras) {
    notificationManager = (NotificationManager) this
            .getSystemService(Context.NOTIFICATION_SERVICE);

    Intent intent;
    if (extras != null) {
        intent = new Intent(this, MessageConversationActivity.class);
        intent.putExtra("sender_id", extras.get("sender_id").toString());
        intent.putExtra("receiver_id", Application.ACTIVE_USER.getUser_id());
        intent.putExtra("gender", extras.get("gender").toString());
        intent.putExtra("profile_picture", extras.get("profile_picture")
                .toString());
        intent.putExtra("username", extras.get("username").toString());
    } else {
        intent = new Intent(this, MainActivity.class);
    }
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
            intent, 0);

    String app_name = getString(R.string.app_name);
    NotificationCompat.Builder builder = new NotificationCompat.Builder(
            this).setSmallIcon(R.drawable.app_icon)
            .setContentTitle(app_name)
            .setAutoCancel(true)
            .setStyle(new NotificationCompat.BigTextStyle().bigText(msg))
            .setContentText(msg);

    builder.setContentIntent(contentIntent);
    notificationManager.notify(NOTIFICATION_ID, builder.build());
}
notificationManager.cancel(notifyId);

您可以使用

Notification.FLAG_AUTO_CANCEL

然后启动所需页面的意图,如果将通知设置为“自动取消”,则一旦用户选择该页面,该页面也将被删除

您还可以调用特定通知ID的cancel()来取消通知

private void sendNotification(String msg, Bundle extras) {
    notificationManager = (NotificationManager) this
            .getSystemService(Context.NOTIFICATION_SERVICE);

    Intent intent;
    if (extras != null) {
        intent = new Intent(this, MessageConversationActivity.class);
        intent.putExtra("sender_id", extras.get("sender_id").toString());
        intent.putExtra("receiver_id", Application.ACTIVE_USER.getUser_id());
        intent.putExtra("gender", extras.get("gender").toString());
        intent.putExtra("profile_picture", extras.get("profile_picture")
                .toString());
        intent.putExtra("username", extras.get("username").toString());
    } else {
        intent = new Intent(this, MainActivity.class);
    }
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
            intent, 0);

    String app_name = getString(R.string.app_name);
    NotificationCompat.Builder builder = new NotificationCompat.Builder(
            this).setSmallIcon(R.drawable.app_icon)
            .setContentTitle(app_name)
            .setAutoCancel(true)
            .setStyle(new NotificationCompat.BigTextStyle().bigText(msg))
            .setContentText(msg);

    builder.setContentIntent(contentIntent);
    notificationManager.notify(NOTIFICATION_ID, builder.build());
}
notificationManager.cancel(notifyId);

MessageConversationActivity
类onCreate方法中使用此选项。

使用notificationManager.cancel(通知ID); 它将从通知管理器中取消通知id

请尝试以下操作
builder.setAutoCancel(true)