单击“操作按钮”上的清除通知

时间:2016-06-23 19:56:28

标签: android android-intent push-notification notifications android-pendingintent

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

我创建通知的代码:

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"点击。

1 个答案:

答案 0 :(得分:5)

关闭" Sluiten"链接被点击通知我已经包含了一个工作版本的代码。

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()

这对于关闭点击

的通知至关重要