0

I have a requirement to fetch all the app notifications which already exists in the notification bar. For this purpose am using the NotificationListenerService along with the BroadcastReceiver to get the certain parameters like Notification.EXTRA_TITLE, Notification.EXTRA_SUB_TEXT, etc. But the problem am facing with NotificationListenerService is that I could not able to fetch the existing notification in the panel, wherein I could able to get the future notifications which i receives when the app is in foreground. Kindly help me with this whether is there any property to get all the app notification which already presents in the notification bar. I am also posting the code that I am using for your reference.

SimpleKitkatNotificationListener.java

public class SimpleKitkatNotificationListener extends NotificationListenerService {

    @Override
    public void onCreate() {
        super.onCreate();
        //android.os.Debug.waitForDebugger();
    }

    @Override
    public void onNotificationPosted(StatusBarNotification sbn) {

        Notification mNotification=sbn.getNotification();

        Log.i("notification",mNotification.tickerText.toString());
        if (mNotification!=null){
            Bundle extras = mNotification.extras;

            Intent intent = new Intent(MainActivity.INTENT_ACTION_NOTIFICATION);
            intent.putExtras(mNotification.extras);
            sendBroadcast(intent);

            Notification.Action[] mActions=mNotification.actions;
            if (mActions!=null){
                for (Notification.Action mAction:mActions){
                    int icon=mAction.icon;
                    CharSequence actionTitle=mAction.title;
                    PendingIntent pendingIntent=mAction.actionIntent;
                }
            }
        }
    }

    @Override
    public void onNotificationRemoved(StatusBarNotification sbn) {

    }
}

And in Activity class am receiving the callbacks through BroadcastReceiver as follows

MainActivity.java

public class MainActivity extends Activity {

protected MyReceiver mReceiver = new MyReceiver();
public static String INTENT_ACTION_NOTIFICATION = "it.gmariotti.notification";

protected TextView title;
protected TextView text;
protected TextView subtext;
protected ImageView largeIcon;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    //Retrieve ui elements
    title = (TextView) findViewById(R.id.nt_title);
    text = (TextView) findViewById(R.id.nt_text);
    subtext = (TextView) findViewById(R.id.nt_subtext);
    largeIcon = (ImageView) findViewById(R.id.nt_largeicon);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.main, menu);
    return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.action_autorize:
            Intent intent = new Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS");
            startActivity(intent);
            return true;
    }
    return super.onOptionsItemSelected(item);
}

@Override
protected void onResume() {
    super.onResume();
    if (mReceiver == null) mReceiver = new MyReceiver();
    registerReceiver(mReceiver, new IntentFilter(INTENT_ACTION_NOTIFICATION));
}

@Override
protected void onPause() {
    super.onPause();
    unregisterReceiver(mReceiver);
}

public class MyReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {

        if (intent != null) {
            Bundle extras = intent.getExtras();
            String notificationTitle = extras.getString(Notification.EXTRA_TITLE);
            int notificationIcon = extras.getInt(Notification.EXTRA_SMALL_ICON);
            Bitmap notificationLargeIcon = ((Bitmap) extras.getParcelable(Notification.EXTRA_LARGE_ICON));
            Bitmap notificationSmallIcon = ((Bitmap) extras.getParcelable(Notification.EXTRA_SMALL_ICON));

            CharSequence notificationText = extras.getCharSequence(Notification.EXTRA_TEXT);
            CharSequence notificationSubText = extras.getCharSequence(Notification.EXTRA_SUB_TEXT);

            title.setText(notificationTitle);
            text.setText(notificationText);
            subtext.setText(notificationSubText);

            if (notificationLargeIcon != null) {
                largeIcon.setImageBitmap(notificationLargeIcon);
            }else if(notificationSmallIcon !=null){
                largeIcon.setImageBitmap(notificationSmallIcon);
            }
        }

    }
}

If anything above not clear kindly forgive me. Any piece of code and hind will be very helpful for me. Thanks in advance.

Chandru
  • 5,954
  • 11
  • 45
  • 85

1 Answers1

1

Try reading the documentation:

http://developer.android.com/reference/android/service/notification/NotificationListenerService.html

There is an example there of how you define a service to handle binding to an NLS. This will allow you to get an instance of NLS and use

http://developer.android.com/reference/android/service/notification/NotificationListenerService.html#getActiveNotifications()

JoxTraex
  • 13,423
  • 6
  • 32
  • 45