web-dev-qa-db-ja.com

Android:通知バーから通知を削除する

アプリケーションを作成し、Android通知バーに通知を追加するイベントを管理しました。次に、イベントの通知バーからその通知を削除する方法のサンプルが必要ですか?

150
Nezir

これは非常に簡単です。 NotificationManagerでcancelまたはcancelAllを呼び出す必要があります。 cancelメソッドのパラメーターは、キャンセルする必要がある通知のIDです。

APIを参照してください: http://developer.Android.com/reference/Android/app/NotificationManager.html#cancel(int)

162

このクイックコードを試すことができます

public static void cancelNotification(Context ctx, int notifyId) {
    String ns = Context.NOTIFICATION_SERVICE;
    NotificationManager nMgr = (NotificationManager) ctx.getSystemService(ns);
    nMgr.cancel(notifyId);
}
205
Ankit Patial

通知マネージャーでcancelAllを呼び出すこともできるため、通知IDについて心配する必要もありません。

NotificationManager notifManager= (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notifManager.cancelAll();

編集:私はダウン投票されたので、これはあなたのアプリケーションから通知のみを削除することを指定する必要があります。

60
Stephane Mathis

次のコードのようにsetAutoCancel(True)を設定するだけです。

Intent resultIntent = new Intent(GameLevelsActivity.this, NotificationReceiverActivityAdv.class);

PendingIntent resultPendingIntent =
        PendingIntent.getActivity(
                GameLevelsActivity.this,
                0,
                resultIntent,
                PendingIntent.FLAG_UPDATE_CURRENT
                );

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
        getApplicationContext()).setSmallIcon(R.drawable.icon)
        .setContentTitle(adv_title)
        .setContentText(adv_desc)
        .setContentIntent(resultPendingIntent)
         //HERE IS WHAT YOY NEED:
        .setAutoCancel(true);

NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(547, mBuilder.build());`
10
farhad.kargaran

サービスからの通知を生成している場合フォアグラウンドで開始されます

startForeground(NOTIFICATION_ID, notificationBuilder.build());

次に発行

notificationManager.cancel(NOTIFICATION_ID);

通知をキャンセルしても機能しません。通知は引き続きステータスバーに表示されます。この特定のケースでは、次の2つの方法でこれらを解決します。

1>サービス内でstopForeground(false)を使用:

stopForeground( false );
notificationManager.cancel(NOTIFICATION_ID);

2>呼び出しアクティビティでそのサービスクラスを破棄します:

Intent i = new Intent(context, Service.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
if(ServiceCallingActivity.activity != null) {
    ServiceCallingActivity.activity.finish();
}
context.stopService(i);

2番目の方法は、音楽プレーヤー通知を優先します

6
Dhruv Raval

これは役立ちます:

NotificationManager mNotificationManager = (NotificationManager)
            getSystemService(NOTIFICATION_SERVICE);
mNotificationManager.cancelAll();

これにより、アプリによって行われたすべての通知が削除されます

呼び出して通知を作成する場合

startForeground();

Service.side内で呼び出す必要があります

stopForeground(false);

最初に通知をキャンセルします。

5
pilotmario

NotificationManagerを使用して、通知をキャンセルします。通知IDのみを提供する必要があります

mNotificationManager.cancel(YOUR_NOTIFICATION_ID);

このリンクも確認してください 開発者リンクを参照

NotificationManager.cancel(id)が正しい答えです。ただし、通知チャネル全体を削除することで、Android Oreo以降の通知を削除できます。これにより、削除されたチャネル内のすべてのメッセージが削除されます。

Androidドキュメント の例を次に示します。

NotificationManager mNotificationManager =
        (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// The id of the channel.
String id = "my_channel_01";
mNotificationManager.deleteNotificationChannel(id);
1
gil.fernandes

これを試してください、

public void removeNotification(Context context, int notificationId) {      
    NotificationManager nMgr = (NotificationManager) context.getApplicationContext()
            .getSystemService(Context.NOTIFICATION_SERVICE);
    nMgr.cancel(notificationId);
}
1
Akansha patel

Android AP​​I> = 23では、このようなことを実行して、通知のグループを削除できます。

  for (StatusBarNotification statusBarNotification : mNotificationManager.getActiveNotifications()) {
        if (KEY_MESSAGE_GROUP.equals(statusBarNotification.getGroupKey())) {
            mNotificationManager.cancel(statusBarNotification.getId());
        }
    }
1
Szabolcs Becze

ただIDを呼び出す:

public void delNoti(int id) {((NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE)).cancel(id);}
0
phnghue