[英]Clear notification on Action Button click
我正在嘗試在用戶單擊操作按鈕時清除通知。 我在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”后清除(關閉)通知。
為了在通知上單擊“ 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()
這對於關閉點擊通知至關重要
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.