6

I would like to access notifications on a Android phone by using the NotificationListenerService. I checked lots of tutorials but I can't find where they call the service.

Should I use bindService or startService on MainActivity? How should the intents look like? Can someone show me the syntax of this?

Check out one of the service implementations I am studying:

public class NLService extends NotificationListenerService {

Context context;

@Override
public void onCreate() {

    super.onCreate();
    context = getApplicationContext();


}


@Override
public void onNotificationPosted(StatusBarNotification sbn) {
    String pack = sbn.getPackageName();
    String ticker ="";
    if(sbn.getNotification().tickerText !=null) {
        ticker = sbn.getNotification().tickerText.toString();
    }
    Bundle extras = sbn.getNotification().extras;
    String title = extras.getString("android.title");
    String text = extras.getCharSequence("android.text").toString();
    int id1 = extras.getInt(Notification.EXTRA_SMALL_ICON);
    Bitmap id = sbn.getNotification().largeIcon;


    Log.i("Package",pack);
    Log.i("Ticker",ticker);
    Log.i("Title",title);
    Log.i("Text",text);

    Intent msgrcv = new Intent("Msg");
    msgrcv.putExtra("package", pack);
    msgrcv.putExtra("ticker", ticker);
    msgrcv.putExtra("title", title);
    msgrcv.putExtra("text", text);
    if(id != null) {
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        //id.compress(Bitmap.CompressFormat.PNG, 100, stream);
        byte[] byteArray = stream.toByteArray();
        msgrcv.putExtra("icon",byteArray);
    }
    LocalBroadcastManager.getInstance(context).sendBroadcast(msgrcv);

}
@Override

public void onNotificationRemoved(StatusBarNotification sbn) {
    Log.i("Msg","Notification Removed");

}

}

user3348949
  • 304
  • 3
  • 4
  • 15

1 Answers1

7

For listening incoming notifications, their is no need to start it explicitly . First define your service in manifest with the following permission -

android.permission.BIND_NOTIFICATION_LISTENER_SERVICE

 <service android:name=".MyNotificationListener"
      android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE">
     <intent-filter>
         <action android:name="android.service.notification.NotificationListenerService" />
     </intent-filter>
 </service>

Another thing to take care is that you must grant the permission either by using the following intent -

Intent intent=new Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS");

or by granting the notification access manually.

Gautam
  • 3,252
  • 3
  • 23
  • 32
  • 1
    To Which method should I pass this intent? – user3348949 Feb 25 '17 at 12:51
  • The above mentioned intent is use to open the notification settings. It will list all the apps asking for notification access. Before using notification listener service, you must grant notification access to your app – Gautam Feb 25 '17 at 15:30
  • Ok. I have previously given app permission to access notifications. But still, the service does not start... – user3348949 Feb 25 '17 at 17:04
  • The word "firing" doesn't mean anything. Should be startActivity using the intent. – pzulw Oct 24 '17 at 16:46
  • 1
    On Android 12, I have call startService. But there's no reponse in `onNotificationPosted`. idk why – Tony Sep 24 '22 at 16:42