Program Tip

Android에서 프로그래밍 방식으로 알림 표시 줄에서 알림을 제거하는 방법은 무엇입니까?

programtip 2020. 11. 19. 21:54
반응형

Android에서 프로그래밍 방식으로 알림 표시 줄에서 알림을 제거하는 방법은 무엇입니까?


누구나 Pending 인 텐트를 사용하여 호출되는 프로그래밍 방식으로 애플리케이션에서 알림을 제거 할 수있는 방법을 알고 있습니다.

다음과 같은 방법으로 알림을 취소했습니다.

AlarmManager am=(AlarmManager)getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(Display.this, TwoAlarmService.class);
PendingIntent pi = PendingIntent.getBroadcast(Display.this, AlarmNumber, intent, PendingIntent.FLAG_CANCEL_CURRENT);
am.cancel(pi);

그러나 문제는 알림 표시 줄에서 제거되지 않은 이미 실행 된 알림입니다.

미리 감사드립니다 ...

여기에 이미지 설명 입력


어쩌면 이것을 시도하십시오 :

NotificationManager notificationManager = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.cancel(NOTIFICATION_ID);

또는 주어진 컨텍스트에서 모든 알림을 취소하기 위해 이렇게 할 수도 있습니다.

notificationManager.cancelAll();

문서에 대한이 링크를 참조하십시오. NotificationManager


알림은 다음 중 하나가 발생할 때까지 계속 표시됩니다.

사용자는 개별적으로 또는 "모두 지우기"를 사용하여 알림을 해제합니다 (알림을 지울 수있는 경우). 사용자가 알림을 클릭하고 알림을 만들 때 setAutoCancel ()을 호출했습니다. 특정 알림 ID에 대해 cancel ()을 호출합니다. 이 방법은 진행중인 알림도 삭제합니다. 이전에 발행 한 모든 알림을 제거하는 cancelAll ()을 호출합니다. setTimeoutAfter ()를 사용하여 알림을 생성 할 때 제한 시간을 설정하면 시스템은 지정된 기간이 경과 한 후 알림을 취소합니다. 필요한 경우 지정된 제한 시간이 경과하기 전에 알림을 취소 할 수 있습니다.

public void cancelNotification() {

    String ns = NOTIFICATION_SERVICE;
    NotificationManager nMgr = (NotificationManager) getActivity().getApplicationContext().getSystemService(ns);
    nMgr.cancel(NOTIFICATION_ID);
}

모든 알림 (다른 앱 알림 포함)은 NotificationListenerService 구현에 언급 된대로 'NotificationListenerService'를 수신하여 제거 할 수 있습니다.

서비스에서 당신은 전화해야합니다 cancelAllNotifications().

'앱 및 알림'-> '특별 앱 액세스'-> '알림 액세스'를 통해 애플리케이션에 대해 서비스를 활성화해야합니다.

매니페스트에 추가 :

    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <service android:label="Test App" android:name="com.test.NotificationListenerEx" android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE">
        <intent-filter>
            <action android:name="android.service.notification.NotificationListenerService" />
        </intent-filter>
    </service>

그런 다음 코드에서;

public class NotificationListenerEx extends NotificationListenerService {


public BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        NotificationListenerEx.this.cancelAllNotifications();
    }
};


@Override
public void onNotificationPosted(StatusBarNotification sbn) {
    super.onNotificationPosted(sbn);
}

@Override
public void onNotificationRemoved(StatusBarNotification sbn) {
    super.onNotificationRemoved(sbn);
}

@Override
public IBinder onBind(Intent intent) {
    return super.onBind(intent);
}

@Override
public void onDestroy() {
    unregisterReceiver(broadcastReceiver);
    super.onDestroy();
}

@Override
public void onCreate() {
    super.onCreate();
    registerReceiver(broadcastReceiver, new IntentFilter("com.test.app"));
}

그 후 브로드 캐스트 수신기를 사용하여 모두 지우기를 트리거합니다.

위의 코드에 따라 방송 사용을 트리거합니다.

getContext().sendBroadcast(new Intent("com.test.app"));

참고 URL : https://stackoverflow.com/questions/19268450/how-to-remove-notification-from-notification-bar-programmatically-in-android

반응형