Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is my NotificationListenerService not working?

I have implemented a NotificationListenerService, yet it is not working. Here is the service:

public class NotificationListener extends NotificationListenerService {

@Override
public void onCreate() {
    Log.d("MYAPP", "Created");
}

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

@Override
public void onNotificationPosted(StatusBarNotification sbn) {
    Log.d("MYAPP", "Notification");
}

}

I have implemented this in my manifest file:

<service android:name="com.rodrigopontes.whatsappbubbles.NotificationListener"
         android:label="Test"
         android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE" >
    <intent-filter>
         <action android:name="android.service.notification.NotificationListenerService" />
    </intent-filter>
</service>

This is how I initialize it from my MainActivity:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_activity);
    startService(new Intent(this, NotificationListener.class));
}

Thank you for the help!

like image 246
RoDeLoPo Avatar asked Sep 14 '15 17:09

RoDeLoPo


People also ask

How do I use NotificationListenerService?

To use NotificationListenerService, we need to create a java file which extends NotificationListenerService and implement two callback methods. Both methods have a parameter named “sbn”, which is an object of StatusBarNotification class. StatusBarNotification provides necessary information about Notifications.

What is a Notification listener?

↳ android.service.notification.NotificationListenerService. A service that receives calls from the system when new notifications are posted or removed, or their ranking changed.


2 Answers

After many wasted hours I found the answer for this. This is an issue with Android see here Welcome to Android :)

like image 190
user1163234 Avatar answered Oct 19 '22 17:10

user1163234


I had the same problem. It turned out my app was not allowed to receive notification as it did not have permission. So I enabled the notification permission by following-

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

Above code opens a notification permission page then select your app and allow.

like image 22
Shahid Kamal Avatar answered Oct 19 '22 17:10

Shahid Kamal