应用程序启动时关闭 android 通知

Close android notification when application starts

我的应用程序中有一个系统通知,它会在应用程序处于后台时通知用户收到特定事件。当我通过单击通知打开应用程序时,应用程序可以正常打开并关闭通知。但是,当我使用应用程序图标(从通知栏或主屏幕)打开应用程序时,不会关闭通知。

有没有办法在启动应用程序时关闭通知(如果它在通知栏中可见)?

请在下面找到我的通知代码:

final int notificationId = 9999;

final NotificationCompat.Builder mBuilder =
    new NotificationCompat.Builder(context)
    .setSmallIcon(R.drawable.ic_launcher)
    .setContentTitle("Notification title")
    .setContentText("Notification text")
    .setAutoCancel(true)
    .setContentIntent(PendingIntent.getActivity(context, 0, new Intent(context, MyActivity.class), 0));
final NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(notificationId, mBuilder.build());

只需这样做:

mNotificationManager.cancel(notificationId);

或取消您应用的所有通知:

mNotificationManager.cancelAll();