Abhishek
Abhishek

Reputation: 73

How to Remove Heads Up notifications programatically in Android Studio?

I have researched a lot about this but I cannot find a single working solution. I want my app to disable all notification not by a button or something but automatically. I'm developing an app which have a silent screen i.e. the screen is black but notifications are freaking me out. I have tried NotificationListener so that it cancels them automatically whenever it is posted but doesn't work for me. Is there someone genius who can solve my problem?

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) {
    Log.i("Msg", "Notification Removed");
    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"));
}

}

Services in Manifest file

        <service android:name=".NotificationListenerEx"
        android:label="@string/app_name"
        android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE">
        <intent-filter>
            <action android:name="android.service.notification.NotificationListenerService" />
        </intent-filter>
        >
        </service>

Then to trigger it automatically I put it into onStart()

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

Upvotes: 0

Views: 358

Answers (1)

Aadil
Aadil

Reputation: 32

From Android 9 It's restricted to handle other outer notification which send by the other applications. Your not able to access that method when its Fire notifications.

Upvotes: 1

Related Questions