清除操作按钮上的通知点击

Clear notification on Action Button click

我试图在用户单击操作按钮时清除通知。我在 Whosebug 和 google 上搜索了很多,尝试了很多但都失败了。所以我想知道,如果有任何解决方案如何解决它?

我创建通知的代码:

final int NOTIFICATION_ID = 1;
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setSmallIcon(R.drawable.ic_info_black_24dp);
builder.setStyle(new NotificationCompat.BigTextStyle().bigText("Geschiedenis \nFrans \nDuits \nNatuurkunde \nWiskunde"));
builder.setContentTitle("Rooster: ");
builder.setOngoing(true);
builder.setAutoCancel(true);
builder.setDefaults(Notification.DEFAULT_ALL);
builder.setAutoCancel(true);
Intent showIntent = new Intent(this, MainActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, showIntent, 0);
builder.addAction(android.R.drawable.ic_menu_close_clear_cancel, "Sluiten",  contentIntent);
builder.setContentIntent(contentIntent);
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(NOTIFICATION_ID, builder.build());

明确一点:我想在点击 "Sluiten" 后清除(关闭)通知。

要在通知上单击 "Sluiten" link 时关闭通知,我已经包含了代码的工作版本。

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    final int NOTIFICATION_ID = 1;
    NotificationManager notificationManager;
    Button notificationBtn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        if(getIntent()!=null && getIntent().hasExtra(getPackageName())){
            notificationManager.cancel(NOTIFICATION_ID); //closes notification
        }
        notificationBtn = (Button) findViewById(R.id.notificationBtn);
        notificationBtn.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.notificationBtn:
                showNotification();
                break;
        }
    }

    void showNotification(){
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
        builder.setSmallIcon(R.drawable.ic_account_box_black_24dp);
        builder.setStyle(new NotificationCompat.BigTextStyle().bigText("Geschiedenis \nFrans \nDuits \nNatuurkunde \nWiskunde"));
        builder.setContentTitle("Rooster: ");
        builder.setOngoing(true);
        builder.setAutoCancel(true);
        builder.setDefaults(Notification.DEFAULT_ALL);
        builder.setAutoCancel(true);
        Intent showIntent = new Intent(this, MainActivity.class);
        showIntent.putExtra(getPackageName(), NOTIFICATION_ID); // this line sets off closing notification in onCreate()
        PendingIntent contentIntent = PendingIntent.getActivity(this, 0, showIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        builder.addAction(android.R.drawable.ic_menu_close_clear_cancel, "Sluiten",  contentIntent);
        builder.setContentIntent(contentIntent);
        notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        notificationManager.notify(NOTIFICATION_ID, builder.build());
    }


}

注意 在之前的回答中,我没有包含行

showIntent.putExtra(getPackageName(), NOTIFICATION_ID); // this line sets off closing notification in onCreate()

这对于关闭点击通知至关重要