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
oronNotificationPosted
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.... (: