2

I'm doing a React Native app (But my problem is mostly related to Android) where I'll capture notifications in the Native Android code and then give the captured data to Javascript via the callback.

I am using BIND_NOTIFICATION_LISTENER_SERVICE service and specified a class NotificationListenerService for the same in the Manifest file.


AndroidManifest.xml

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

NotificationListenerService.java

public class NotificationListener extends NotificationListenerService {

    Context context;

    private String TAG = this.getClass().getSimpleName();

    @Override
    public void onCreate() {
        super.onCreate();

        // this method is not getting executed after 2 or 3 days of installation

        Log.d("KBTCHECK", "NotificationListener - onCreate");
        context = getApplicationContext();
    }
    @Override
    public void onNotificationPosted(StatusBarNotification sbn) {

        // this method is not getting executed after 2 or 3 days of installation

        Log.d("KBTCHECK", "NotificationListener - onNotificationPosted");

            // My Logic here..

            Intent msgrcv = new Intent("NOTIFICATION_POSTED_KBT");
            msgrcv.putExtras(extras);

            // Broadcasting it, so I'll capture in another module and send it to Javascript
            LocalBroadcastManager.getInstance(context).sendBroadcast(msgrcv);
    }
}

Here, I'm not triggering the NotificationListenerService manually. I hope specifying it in Manifest makes the Android system to trigger it automatically.

Problem:

NotificationListenerService's onCreate or onNotificationPosted not being called on receiving notification after 2/3 days of installation while receiving notification.

(I need to disable/enable the notification access manually to get it to work again)

My Expectation:

Trigger the service in specific time interval so that even if it's stopped by android system, it should start again automatically.

If I trigger manually, I can schedule it to trigger in any time interval but since it's being triggered automatically by Android, I don't find a way (Or I don't know how to) to execute the trigger action in the specific time interval.

Please guide me with the correct path to achieve the expected behavior.

Thanks, your time and help will be appreciated.... (:

Ganesh
  • 1,820
  • 2
  • 20
  • 40
  • did you find a solution for that? @Ganesh – Saman Sattari Feb 22 '20 at 10:25
  • duplicate of https://stackoverflow.com/questions/33530807/why-is-this-notificationlistenerservice-not-working – Saman Sattari Feb 22 '20 at 10:33
  • @SamanSattari Not a duplicate, because the problem they addressed is that the service doesn't work only when updating the app (or recompiling the app when in dev mode). Mine is the problem occurs on the first installation itself. – Ganesh Feb 26 '20 at 11:08

0 Answers0