【问题标题】:Clear notification on Action Button click单击操作按钮时清除通知
【发布时间】:2016-10-26 07:46:54
【问题描述】:

我正在尝试在用户单击操作按钮时清除通知。我在stackoverflow和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”,我想清除(关闭)通知。

【问题讨论】:

  • 你最好自己操作。创建具有特定 ID 的通知。在您的自定义布局上添加一个自定义按钮并将其设置为取消具有该特定 ID 的通知的操作

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


【解决方案1】:

要在单击通知上的“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()

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

【讨论】:

  • 我已经编辑了我的答案。我忽略了您需要的重要代码行。
  • 好吧,我没有自定义通知视图,所以找不到findViewById(R.id.notificationBtn)
  • 那部分无关紧要。这就是我调用 showNotification() 函数来测试它的地方。你可以随意调用 showNotification
  • 确实有效!!!谢谢伙计,你让我开心!是否也可以在不打开应用程序的情况下关闭通知? (例如我启动通知,离开应用程序,然后单击“Sluiten”,它只会关闭通知而不打开应用程序)
  • 很高兴听到!如果您最初的问题得到了回答,请投票并选择它作为答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-21
  • 2019-12-17
相关资源
最近更新 更多