How NotificationListenerService Methods works

164 views Asked by At

In order to read android notifications we need to create a custom class that extends NotificationListenerService class. But I am not able to understand what the inside this method does?

@Override
public void onNotificationPosted(StatusBarNotification sbn) {
    Log.i(TAG,"**********  onNotificationPosted");
    Log.i(TAG,"ID :" + sbn.getId() + "t" + sbn.getNotification().tickerText + "t" + sbn.getPackageName());
    Intent i = new  Intent("com.example.readandroidnotification.NOTIFICATION_LISTENER_EXAMPLE");
    i.putExtra("notification_event","onNotificationPosted :" + sbn.getPackageName() + "n");
    sendBroadcast(i);
}

Can anyone explain the working of this code?

1

There are 1 answers

3
TheWanderer On
  1. It logs to the system log that a notification has been posted.
  2. It logs to the system log the ID of the notification, the summary text of the notification, and the package name of the app that created it.
  3. It then creates a new Intent to broadcast some info about the notification that was posted.
  4. It puts the package name of the app that posted the notification as an extra to the Intent.
  5. It sends that Intent as a broadcast, allowing another component of the app to use that info.

I recommend reading some documentation about Android: