Notificationlistenerservice Implementation

NotificationListenerService Implementation

Inside the NotificationListenerService you need a looper to communicate with GUI thread so you can create a broadcast to handle the GUI interaction.

Hope this example will help you.

How to start the NotificationListenerService on Android

For listening incoming notifications, their is no need to start it explicitly . First define your service in manifest with the following permission -

android.permission.BIND_NOTIFICATION_LISTENER_SERVICE

 <service android:name=".MyNotificationListener"
android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE">
<intent-filter>
<action android:name="android.service.notification.NotificationListenerService" />
</intent-filter>
</service>

Another thing to take care is that you must grant the permission either by using the following intent -

Intent intent=new Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS");

or by granting the notification access manually.

Unable to implement NotificationListenerService, because of unresolved class in Manifest-file

General comment: hitting Alt+Enter once focused on red-highlighted area (usually) shows possible resolutions.

In your case, once applied over "android:name=".NotificationListener" you should be able to create class that extends Service, thus should be assignable to 'android.app.service'.
You should then encounter another issue 'public class NotificationListener extends Service' marked red. Alt+Enter again and 'Implement methods' -> 'onBind' should be generated.

For '@string/service_name' - either create a string resource or use any other string (e.g. "my_service_tag" or whatever).

Please follow the attached docs and have a look on 'onListenerConnected'. Cheers.

How to use NotificationListenerService to block notification in android

I found a way! Obviously I created a NotificationListener class which extends the NotificationListenerService class and then override the onNotificationPosted(StatusBarNotification sbn) function.

To block all the notifications I created a variable in MainActivity.java named public static boolean BLOCK_ALL = false;. This variable would change as the Block All Notification switch in the screenshot changed.

    SwitchMaterial blockAllNotifications = findViewById(R.id.blockAllNotifications);
blockAllNotifications.setChecked(BLOCK_ALL);
blockAllNotifications.setOnCheckedChangeListener((buttonView, isChecked) -> {
BLOCK_ALL = isChecked;
});

And in the onNotificationPosted(StatusBarNotification sbn) function in the NotificationListener I added following condition;

    if(MainAcitivity.BLOCK_ALL)cancelAllNotifications();

Whenever the Block All Notifiction switch is turned on it's going to call cancelAllNotification() function and block all the notifications.

At last in order to block notifications of each package I did something as follow. As I mentioned in the question, I have all the apps whose switch is turned on in a Room named BlockedList. Therefore I added a logic; if the package which has posted the notification is in the BlockedList I blocked that app's notifications with cancelNotification(String key).

appNameList = appsDB.dbDAO().getAllAppNames();
if(appNameList.contains(getApplicationName(sbn.getPackageName()))){ // if the blocked list has the app name of the notification
cancelNotification(sbn.getKey());
}

/***
* getApplicationName: get the application name of the notification belongs to
* @param pack: the package name; a StatusBarNotification.getPackageName()
* @return
*/
private String getApplicationName(String pack){
final PackageManager pm = context.getPackageManager();
ApplicationInfo appInfo;
try{
appInfo = pm.getApplicationInfo(pack, 0); // getting the application info
}catch(PackageManager.NameNotFoundException e){
appInfo = null;
}
final String appName = (String) (appInfo != null? pm.getApplicationLabel(appInfo) : "(unknown)"); // if the appInfo is not null get the label of the Application
return appName;
}

Android NotificationListenerService throws DeadObjectException

Try not starting the service yourself. If you have enabled the NotificationListenerService in the security settings, the system should bind to it automatically.

Alternatively, check your crash logs to see if your service crashed or its process was killed. I believe there is a bug where if your NotificaitonListerService dies, the system will not rebind until you restart your phone or toggle the notifications permission in security settings.



Related Topics



Leave a reply



Submit