getActiveNotifications always null when using NotificationListenerService

10,006

After I enable this application in Notification Access section in Settings -> Security.

I can received data from getActiveNotifications now.

Thanks,

Share:
10,006
Huy Tower
Author by

Huy Tower

​Huy Tower --- Flutter Developer | Senior Android Developer --- Skype: huytower Location : Ho Chi Minh, Viet Nam

Updated on June 04, 2022

Comments

  • Huy Tower
    Huy Tower almost 2 years

    I followed this Sample Code of kpBird and this Developer Guide

    I can :

    • Call intent to this service.

    • Can catch Broadcast from Notification.

    So I got the error getActiveNotifications always null.

    I don't know why,

    People who know, please help me,

    Thanks,

    p/s : Here is source code and error I get.

    Error :

    04-28 08:46:11.625: E/AndroidRuntime(7651): FATAL EXCEPTION: main
    04-28 08:46:11.625: E/AndroidRuntime(7651): java.lang.RuntimeException: Error receiving broadcast Intent { act=app.trekband.NotificationListener flg=0x10 (has extras) } in utils.NotificationListener$NLServiceReceiver@42680780
    04-28 08:46:11.625: E/AndroidRuntime(7651):     at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:778)
    04-28 08:46:11.625: E/AndroidRuntime(7651):     at android.os.Handler.handleCallback(Handler.java:730)
    04-28 08:46:11.625: E/AndroidRuntime(7651):     at android.os.Handler.dispatchMessage(Handler.java:92)
    04-28 08:46:11.625: E/AndroidRuntime(7651):     at android.os.Looper.loop(Looper.java:176)
    04-28 08:46:11.625: E/AndroidRuntime(7651):     at android.app.ActivityThread.main(ActivityThread.java:5419)
    04-28 08:46:11.625: E/AndroidRuntime(7651):     at java.lang.reflect.Method.invokeNative(Native Method)
    04-28 08:46:11.625: E/AndroidRuntime(7651):     at java.lang.reflect.Method.invoke(Method.java:525)
    04-28 08:46:11.625: E/AndroidRuntime(7651):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046)
    04-28 08:46:11.625: E/AndroidRuntime(7651):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
    04-28 08:46:11.625: E/AndroidRuntime(7651):     at dalvik.system.NativeStart.main(Native Method)
    04-28 08:46:11.625: E/AndroidRuntime(7651): Caused by: java.lang.NullPointerException
    04-28 08:46:11.625: E/AndroidRuntime(7651):     at android.os.Parcel.readException(Parcel.java:1437)
    04-28 08:46:11.625: E/AndroidRuntime(7651):     at android.os.Parcel.readException(Parcel.java:1385)
    04-28 08:46:11.625: E/AndroidRuntime(7651):     at android.app.INotificationManager$Stub$Proxy.getActiveNotificationsFromListener(INotificationManager.java:518)
    04-28 08:46:11.625: E/AndroidRuntime(7651):     at android.service.notification.NotificationListenerService.getActiveNotifications(NotificationListenerService.java:149)
    04-28 08:46:11.625: E/AndroidRuntime(7651):     at utils.NotificationListener$NLServiceReceiver.onReceive(NotificationListener.java:73)
    04-28 08:46:11.625: E/AndroidRuntime(7651):     at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:768)
    04-28 08:46:11.625: E/AndroidRuntime(7651):     ... 9 more
    

    Source Code :

    I called NotificationListener class by using this :

    private NotificationReceiver notificationReceiver;
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
            if (!Constants.connectivity.isNetworkOnline()) {
                // If there is error
                Toast.makeText(getActivity(), getString(R.string.toast_failed_connection),
                        Toast.LENGTH_SHORT).show();
    
                // Exit the application
                android.os.Process.killProcess(android.os.Process.myPid());
            }
    
            Intent intent = new Intent(getActivity(), NotificationListener.class);
    
            getActivity().startService(intent);
    
            notificationReceiver = new NotificationReceiver();
            IntentFilter filter = new IntentFilter();
    
            // Add action
            filter.addAction(Notification.NOTIFICATION);
    
            // Register receiver
            getActivity().registerReceiver(notificationReceiver,filter);
        }
    
        public class NotificationReceiver extends BroadcastReceiver{
    
            @Override
            public void onReceive(Context context, Intent intent) {
                String temp = intent.getStringExtra("notification_event");
    
                // CURRENTLY THIS VALUE IS NULL
                Log.i(TAG, temp + "");
            }
        }
    

    This is NotificationListener class which extends from NotificationListenerService

    public class NotificationListener extends NotificationListenerService{
    
        private String TAG = NotificationListener.class.getSimpleName();
        private NLServiceReceiver mNLServiceReciver;
    
        @Override
        public void onCreate() {
            super.onCreate();
    
            Log.i(TAG, "onCreate");
    
            mNLServiceReciver = new NLServiceReceiver();
            IntentFilter filter = new IntentFilter();
            filter.addAction(Notification.NOTIFICATION);
            registerReceiver(mNLServiceReciver,filter);
        }
    
        @Override
        public void onDestroy() {
            super.onDestroy();
    
            Log.i(TAG, "onDestroy");
    
            unregisterReceiver(mNLServiceReciver);
        }
    
        @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(Notification.NOTIFICATION);
            i.putExtra("notification_event","onNotificationPosted :" + sbn.getPackageName() + "\n");
            sendBroadcast(i);
        }
    
        @Override
        public void onNotificationRemoved(StatusBarNotification sbn) {
            Log.i(TAG,"onNOtificationRemoved");
            Log.i(TAG,"ID :" + sbn.getId() + "\t" + sbn.getNotification().tickerText +"\t" + sbn.getPackageName());
    
            Intent i = new  Intent(Notification.NOTIFICATION);
            i.putExtra("notification_event","onNotificationRemoved :" + sbn.getPackageName() + "\n");
    
            sendBroadcast(i);
        }
    
        public class NLServiceReceiver extends BroadcastReceiver{
    
            @Override
            public void onReceive(Context context, Intent intent) {
    
                if(intent.getStringExtra("command").equals("clearall")){
                    NotificationListener.this.cancelAllNotifications();
                } else if(intent.getStringExtra("command").equals("list")){
                    Intent i1 = new Intent(Notification.NOTIFICATION);
                    i1.putExtra("notification_event","=====================");
                    sendBroadcast(i1);
    
                    // ERROR HERE
                    Log.i(TAG, "getActiveNotifications " + getActiveNotifications());
                    Log.i(TAG, "length " + getActiveNotifications().length);
    
                    int i=1;
                    for (StatusBarNotification sbn : NotificationListener.this.getActiveNotifications()) {
                        Intent i2 = new  Intent(Notification.NOTIFICATION);
                        i2.putExtra("notification_event",i +" " + sbn.getPackageName() + "\n");
                        sendBroadcast(i2);
                        i++;
                    }
    
                    Intent i3 = new  Intent(Notification.NOTIFICATION);
                    i3.putExtra("notification_event","===== Notification List ====");
                    sendBroadcast(i3);
    
                }
            }
        }
    }