如果单击,如何删除通知?

How do I remove the notification if clicked?

所以我想知道如何在用户单击后删除 NotificationCenter 中的待处理通知。这是我的 BroadcastReceiver:

public class MyBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
    Intent startServiceIntent = new Intent(context, BackgroundChecks.class);
    startServiceIntent.putExtra("notificationActivityAmtsblatt", AmtsblattActivity.class.getName());
    startServiceIntent.putExtra("notificationActivityAmtstafel", AmtsblattActivity.class.getName());
    context.startService(startServiceIntent);
}
}

我可以把它放在 BCReceiver 的某个地方吗? 谢谢

最简单的解决方案是在构建通知时将通知的 autoCancel 属性设置为 true,例如:

yourNotification.setAutoCancel(true);

但是如果你想关闭 BCReceiver 中的通知,你可以使用 NotificationManager 的 cancel(int notificationId) 方法,例如:

NotificationManager mNotifyMgr = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
mNotifyMgr.cancel(youNotificationId);