Search code examples
androidkotlinnotificationslistenerandroid-permissions

NotificationListenerService requires to toggle notification access on every launch to work


Manifest.xml

    <service android:name=".CustomNotificationListene"
        android:label="@string/app_name"
        android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE">
        <intent-filter>
            <action android:name="android.service.notification.NotificationListenerService" />
        </intent-filter>
    </service>

customnotificationlistene class

class CustomNotificationListene : NotificationListenerService() {
private var muted = false
private var originalVolume = 0
private var zeroVolume = 0
private var blocklist = listOf<String>("Advertisement","spotify")


override fun onUnbind(intent: Intent?): Boolean {
    return super.onUnbind(intent)
}


override fun onStartCommand(intent: Intent, flags: Int, startID: Int): Int {
    timer = Timer()
    isRunning = true
    muted = false
    originalVolume = 0
    zeroVolume = 0

    //Some function



override fun onDestroy() {
    try {
        killService()
    } catch (ex: NullPointerException) {
    }
}

override fun onNotificationPosted(notification: StatusBarNotification) {}
override fun onNotificationRemoved(notification: StatusBarNotification) {}


}

Mainactivity call for customnotificationlistene class in oncreate method

  var serviceIntent = Intent(this, CustomNotificationListene::class.java)
  startService(serviceIntent)

Logcat information

2020-06-02 23:22:24.401 3375-3671/com.example.verse W/NotificationListenerService[CustomNotificationListene]: Notification listener service not yet bound.

(times 4 every second till the end)

Problem: I have to manually toggle notification access every time i launch the app to get the notification listener to work.

My questions: I need to read notification of another app and not spam the

startActivity(Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS"))

every time user opens my app.
Other apps which require notification access like mi fit or truecaller requires to toggle the notification access only once and works perfectly till its toggles back off. So there must be a way.

Is there any way to save the given access like using getSharedPreferences to detect first launch of the app?

Tried fixes:
Pretty much all the stackoverflow solutions regarding NotificationListenerService but no luck.

Answer that i require: Need to prompt user whenever the notification access for my app is toggled off and not get

2020-06-02 23:22:24.401 3375-3671/com.example.verse W/NotificationListenerService[CustomNotificationListene]: Notification listener service not yet bound.

Please help me!! i already spent like 3 days in this single issue.


Solution

  • I don't know if its the correct answer but after doing some research I found out that the NotificationListenerService shows some anomaly when the device is connected to pc for debugging. Temporarily i fixed it by prompting the user to turn on and off the notification access to my app whenever its not working and then it works properly.

    Please let me know if there are any valid fix with 100% working probability.