[英]Clear notification on Action Button click
I am trying to clear an notification when an user clicken on the action button. 我正在尝试在用户单击操作按钮时清除通知。 I searched a lot on stackoverflow and google, tried a lot but all failed.
我在stackoverflow和google上搜索了很多,尝试了很多,但是都失败了。 So I was wondering, if there is any solution how to fix it?
所以我想知道,是否有解决方案?
My code for creating notification: 我创建通知的代码:
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());
So to be clear: I would like to clear (close) the notification once "Sluiten" is clicked. 因此要明确:我想在单击“ Sluiten”后清除(关闭)通知。
To close the notification when the "Sluiten" link is clicked on the notification I have included a working version of code. 为了在通知上单击“ 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());
}
}
NOTE In the previous answer I did not include the line 注意在上一个答案中,我没有包含该行
showIntent.putExtra(getPackageName(), NOTIFICATION_ID); // this line sets off closing notification in onCreate()
which is essential to closing the notification on click 这对于关闭点击通知至关重要
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.