SoFunction
Updated on 2025-03-11

Android  Usage of NotificationListenerService

Android  Use of NotificationListenerService

Introduction

Many third-party security APPs now have message management functions or message box functions, which can manage and filter some useless messages in the system, making the message bar more refreshing and clean. In fact, the implementation of this function is to use the notification usage rights provided in Android. After Android 4.3, notification usage rights are added. That is to say, when the APP you developed has this permission, you can listen to changes in notifications in the current system. After Android 4.4, you can also obtain notification details. Let’s take a look at the specific use of NotificationListenerService.

use

Create a new service class so that it inherits the NotificationListenerService and implements two important methods:

@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2) 
public class NotificationListener extends NotificationListenerService { 
  privatestatic final String TAG = "NotificationListener"; 
  
  @Override 
  public void onNotificationRemoved(StatusBarNotification sbn) { 
    (TAG,"Notification removed"); 
  } 
  
  @Override 
  public void onNotificationPosted(StatusBarNotification sbn) { 
    (TAG, "Notification posted"); 
  } 
} 

This service class is declared and must declare BIND_NOTIFICATION_LISTENER_SERVICE license and intent filter

, and the label tag we see in the notification right list in the system settings:

<serviceandroid:name=".NotificationListener" 
     android:label="Notification Rights Test Program" 
     android:permission=".BIND_NOTIFICATION_LISTENER_SERVICE"> 
  <intent-filter> 
    <actionandroid:name=""/> 
  </intent-filter> 
 </service> 

OK, it is so simple that you can complete the function of the APP monitoring system notification bar. Next, let's take a look at the NotificationListenerService class also provides some important methods:

StatusBarNotification[] sbns = getActiveNotifications();         // Return an array of all notifications in the current systemcancelAllNotifications();                        // Delete all cleared notifications in the systemcancelNotification(String pkg, String tag, int id);           // Delete a specific notification

There are also two important rewriting methods we mentioned above:

onNotificationRemoved(StatusBarNotification sbn);            // Callback when notification is removedonNotificationPosted(StatusBarNotification sbn);             // Callback when adding a notification 

These two important callback methods have their parameter StatusBarNotification object that is the details of the current triggering change notification. Let’s take a look at the use of StatusBarNotification:

();                               // Return to the corresponding id of the notification();                          // Return to the notification object();                          // Return to the package name corresponding to the notification();                            // Return to the time when the notification was initiated();                              // Return the tag of the notification, if no settings are set, return null();                            // Return to whether the notification can be clear, whether it is FLAG_ONGOING_EVENT, FLAG_NO_CLEAR();                             // Return whether the notification is running or not, whether it is FLAG_ONGOING_EVENT

Among them, the notification object returned by getNotification() can also see other related information about the notification in detail, such as:

Notification notification = (); 
;                        // RemoteViews for notifications;                       // Notified PendingIntent;                          // Array of notification behavior// After Android 4.4, it has also been expanded to get notification detailsif (.SDK_INT >Build.VERSION_CODES.JELLY_BEAN_MR2) { 
     Bundle extras = ; 
     String notificationTitle = (Notification.EXTRA_TITLE); 
     int notificationIcon = (Notification.EXTRA_SMALL_ICON); 
     Bitmap notificationLargeIcon = ((Bitmap)(Notification.EXTRA_LARGE_ICON)); 
     CharSequence notificationText = (Notification.EXTRA_TEXT); 
     CharSequence notificationSubText = (Notification.EXTRA_SUB_TEXT); 
} 

Jump the notification usage rights page in the system settings

private boolean gotoNotificationAccessSetting(Contextcontext) { 
  try { 
    Intent intent = new Intent(".ACTION_NOTIFICATION_LISTENER_SETTINGS"); 
    (Intent.FLAG_ACTIVITY_NEW_TASK); 
    (intent); 
    return true; 
  } catch(ActivityNotFoundException e) { 
    try { 
      Intent intent = new Intent(); 
      (Intent.FLAG_ACTIVITY_NEW_TASK); 
      ComponentName cn = new ComponentName("","$NotificationAccessSettingsActivity"); 
      (cn); 
      (":settings:show_fragment", "NotificationAccessSettings"); 
      (intent); 
      return true; 
    } catch(Exception ex) { 
      (); 
    } 
    return false; 
  } 
} 

Determine whether you have the right to use the notification

private boolean notificationListenerEnable() { 
  boolean enable = false; 
  String packageName = getPackageName(); 
  String flat= (getContentResolver(),"enabled_notification_listeners"); 
  if (flat != null) { 
    enable= (packageName); 
  } 
  return enable; 
} 

Thank you for reading, I hope it can help you. Thank you for your support for this site!