Reputation: 355
NOTE: I'm aware that this question somehow can be a duplicate. Still I'm asking this as I found no tutorial or any proper answer for the above question.
I'm making an app which has a feature that user can block the notifications. The user can select either to block all the notifications or block only selected apps. To select apps for blocking notifications I have created a RecyclerView
where user can choose which app to be blocked, and to block all notifications there is a master switch. The android Rooms
are used to save data even after the activity is destroyed.
Following is a screenshot of the app.
All I need to know is how can I implement the NotificationListenerService
in order to block notifications. As I mentioned before I want only to block notifications when the app is selected in the RecyclerView
or the Block all notifications
, master switch is turned on. Anyone who can give me a proper guide is appreciated.
PS: There are 8 files in the project and I have no idea how to post them here, any solutions for that also?
Upvotes: 3
Views: 1173
Reputation: 355
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;
}
Upvotes: 4
Reputation: 1
Every notification(StatusBarNotification) has its own packagename,
public String getPackageName() {
return pkg;
}
I think you can block it through StatusBarNotification attribute
Upvotes: -1