NotificationListenerService Implementation

55,661

Solution 1

Inside the NotificationListenerService you need a looper to communicate with GUI thread so you can create a broadcast to handle the GUI interaction.

Hope this example will help you.

Solution 2

You need to grant access to your app to read notifications: "Settings > Security > Notification access" and check your app.

Solution 3

At least one problem with your code is that your implementation of onBind()

There is no necessity to override this method. But if you must, then at least return the IBinder returned by the superclass.

@Override
public IBinder onBind(Intent intent) {
    return super.onBind(intent);
}

Solution 4

It might be a bit late. But a few months ago I also struggled with making NotificationListenerService work.

Since then I`ve learned how to implement it and felt like building a implementation tutorial to help others who went through the same as me

If anyone is interested, check the project here: https://github.com/Chagall/notification-listener-service-example

Hope it helps someone who is struggling with it.

Solution 5

I know its too late to answer the question , but as I was not able to find Settings > Sound and notifications -> Notification access, so I directly allow access of notifications to my app by firing this intent:

startActivity(new Intent(Settings.ACTION_NOTIFICATION_LISTENER_SETTINGS));

Share:
55,661

Related videos on Youtube

Naga
Author by

Naga

I am a programmer and love to write code in any language currently I am working on android and developed android apps which are available at Google Play Store. I have written an open source tracking library for android. I have also written library for mobile advertisement system. Currently working in a startup and living in Bangalore India. I have exposure to python , Django , Windows 8 mobile OS and slowly learning Haskell (A language which I started loving).

Updated on February 14, 2020

Comments

  • Naga
    Naga over 4 years

    I am trying to implement NotificationListnerService which is added in android 4.3 but I am not able to get the notification details.

    My code are as below

    public class MainActivity extends Activity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this);
            mBuilder.setSmallIcon(R.drawable.ic_launcher);
            mBuilder.setContentTitle("notification test");
            mBuilder.setContentText("Notification text");
            mBuilder.setAutoCancel(true);
            Intent resultIntent = new Intent(this, ResultActivity.class);
            TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
            // Adds the back stack for the Intent (but not the Intent itself)
            stackBuilder.addParentStack(ResultActivity.class);
            // Adds the Intent that starts the Activity to the top of the stack
            stackBuilder.addNextIntent(resultIntent);
            PendingIntent resultPendingIntent =
                    stackBuilder.getPendingIntent(
                        0,
                        PendingIntent.FLAG_UPDATE_CURRENT
                    );
            mBuilder.setContentIntent(resultPendingIntent);
            NotificationManager manager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
            manager.notify(1, mBuilder.build());
        }
    
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.main, menu);
            return true;
        }
    
    }
    
    public class NotificationListenerTesting extends NotificationListenerService{
    
        public static String TAG = "NotificationListenerTesting";
        //private StatusBarNotification[] mStatusBarNotification;
    
        @Override
        public void onCreate(){
            super.onCreate();
            Log.d(TAG, "Inside on create");
        }
        @Override
        public IBinder onBind(Intent intent) {
            return null;
        }
        @Override
        public void onNotificationPosted(StatusBarNotification sbn) {
            TAG = "onNotificationPosted";
            Log.d(TAG, "id = " + sbn.getId() + "Package Name" + sbn.getPackageName() + 
                    "Post time = " + sbn.getPostTime() + "Tag = " + sbn.getTag());
        }
        @Override
        public void onNotificationRemoved(StatusBarNotification sbn) {
            TAG = "onNotificationRemoved";
            Log.d(TAG, "id = " + sbn.getId() + "Package Name" + sbn.getPackageName() + 
                    "Post time = " + sbn.getPostTime() + "Tag = " + sbn.getTag());
    
        }
    
    }
    

    Android manifest file is

    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.example.notificationtest"
        android:versionCode="1"
        android:versionName="1.0" >
        <uses-sdk
            android:minSdkVersion="8"
            android:targetSdkVersion="18" />
        <application
            android:allowBackup="true"
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name"
            android:theme="@style/AppTheme" >
            <activity
                android:name="com.example.notificationtest.MainActivity"
                android:label="@string/app_name" >
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
            <activity android:name="com.example.notificationtest.ResultActivity"></activity>
            <service android:name="com.example.notificationtest.NotificationListenerTesting"
                android:label="notification"
                android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE">
                <intent-filter>
                    <action android:name="android.service.notification.NotificationListenerService"/>
                </intent-filter>
            </service>
        </application>
    </manifest>
    

    but after notification click or on notification post NotificationListenerService is not getting called , Whats wrong in this or did I miss somthing ? How to implement it ?

    • samo
      samo over 10 years
      What about minSdkVersion="8"? NotificationListenerService requires API level 18.
    • Naga
      Naga over 10 years
      It requires but they have provided the support for older version too
    • Anjani
      Anjani over 10 years
      @Nagendra Have they really provided support for older versions too because I couldn't find it anywhere. Can you please guide me somewhere or tell me yourself how that can be done? I have been searching for this from quite many days. I'd really appreciate any help.
  • Naga
    Naga almost 11 years
    Let me check it out will let you know
  • Gal Bracha
    Gal Bracha over 10 years
    Better way is to call the intent to raise this screen and just tell the user to check the checkbox on the next upcoming screen
  • demonslayer319
    demonslayer319 over 10 years
    Don't know why you're getting downvotes, but this is absolutely correct. I wrote in more detail about this in an answer to a related question.
  • demonslayer319
    demonslayer319 over 10 years
    The example you linked is good, but I wish they included an explanation for why they send Broadcasts instead of using a local Binder or Messanger/Handler. It turns out there is a very good reason to use Broadcasts, but that should be explained somewhere in there.
  • black
    black about 10 years
    I didn't face this problem. It works the same with me with or without a ticker on 4.4.2.
  • Taiko
    Taiko almost 10 years
    Any way to ensure it's reliabily started ?
  • guy.gc
    guy.gc over 9 years
    Can anyone please elaborate on who to implement such looper? Thanks.
  • lujop
    lujop about 8 years
    At least in newer versions the option is in Settings > Sound and notifications -> Notification access
  • Vijay
    Vijay over 3 years
    @Taiko Even I waiting for this, it's not working for me in API 23.
  • Vijay
    Vijay over 3 years
    Will it work if the app is not in the foreground. I want to receive the WhatsApp notification and then retrieve the Room DB and reply to the WhatsApp notification after some calculation so I wanted to know if this will work if the App is not running but I have granted the NotificationListnerPermission to the app.