ステータスバーから通知を削除するにはどうすればよいですか?

StackOverflow https://stackoverflow.com/questions/2832251

  •  26-09-2019
  •  | 
  •  

質問

私はaを表示しています Notification 私の状態に依存するステータスバーで。これまで、それは大丈夫です。

私のアプリケーションの問題は、私がアプリケーションに戻ったとき、まだ Notification ステータスバーに表示されます。

私は望んでいません Notification アプリケーションから戻ってきたとき。これについては、いくつかの提案をください。

役に立ちましたか?

解決

あなたはできる cancel() あなた自身の Notifications 経由 NotificationManager. 。いつ決めるのはあなた次第です cancel() それとそれをいつ見せるか。

他のヒント

@commonwareなどのコメントを追加するのに十分な担当者がいません。 。 。あなたが彼らに使用するように言うようなより具体的な詳細を提供した場合、将来の読者にとってより役立つでしょう 「通知ID」 通知を作成し、そのIDを使用して通知をキャンセルするとき。答えを出す場合は、完全な詳細を完了するか、少なくとも詳細を提供します。これらの投稿は、この投稿に出くわす他の人にも役立つOPのためだけではありません。そのようです

final NotificationManager notificationManager = (NotificationManager) getSystemService (NOTIFICATION_SERVICE);

final Notification notification = new Notification(R.drawable.icon,"A New Message!",System.currentTimeMillis());

notification.defaults=Notification.FLAG_ONLY_ALERT_ONCE+Notification.FLAG_AUTO_CANCEL;
Intent notificationIntent = new Intent(this, AndroidNotifications.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,notificationIntent, 0);

notification.setLatestEventInfo(AndroidNotifications.this, title,message, pendingIntent);
notificationManager.notify(NOTIFICATION_ID, notification);

その後、それをキャンセルするために呼び出します

notificationManager.cancel(NOTIFICATION_ID);

または、電話することができます

notificationManager.cancelAll();
notificationManager.cancelAll();
private void cancelNotificationInBar() {
        NotificationCreator notification = new NotificationCreator(getApplicationContext());
        notification.mNotificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);

        if (notification.mNotificationManager != null) {
            notification.mNotificationManager.cancelAll();
        }
    }
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top