onNotificationPosted not being called after some time - java

I use NotificationListenerService to catch notifications and elaborate them in my application. App works great and onNotificationPosted is always called, but after a while (Few hours) it stops being called and the app stops working.
It gets back to work only if I reboot my device or if I revoke and grant again notification access to the application.
Important info
App does have notification access enabled.
App does have service on AndroidManifest.
<service
android:name=".NotificationService"
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>
App is a release version (Same problem on debug version though).
It has been tested on Android Lollipop, Marshmallow and Nougat, same result.
It has been tested in different devices running the same Android version.
App stops working in both background and activity displayed on screen.
It works again if I reinstall the application, but this is already included in 'revoke and grant notification access again' previously mentioned.
App includes AdMob banner on bottom, is it a possible problem?
I read Android has a cache problem or something like that. Is this the problem? Is there any permanent solution that I couldn't find anywhere?

I found the answer myself here:
https://gist.github.com/xinghui/b2ddd8cffe55c4b62f5d8846d5545bf9
Just create a new class as in the link above, then put in onCreate:
startService(new Intent(this, NotificationCollectorMonitorService.class));
And in AndroidManifest:
<service android:name=".NotificationCollectorMonitorService"/>
This will restart your NotificationListenerService whenever Android kills it.

Related

Some doubts regarding `BroadcastReceiver` in Android

I have been scratching my head for the past few days. I am trying to intercept incoming calls in Android. For that I have used a BroadcastReceiver and registered it in my AndroidManifest.xml as follows
<receiver android:name="org.myapp.IncomingCallReceiver"
android:permission="android.permission.READ_PHONE_STATE" android:exported="true">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE"/>
</intent-filter>
</receiver>
I am able to get the incoming call number in the onReceive method of my IncomingCallReceiver. But the broadcast receiver does not work if I close my app. I have seen some libraries that does work in the background. For example telephony is a flutter plugin that receives incoming SMSs using broadcast receiver. When my app using the telephony plugin is killed, it continues to receives messages in its IncomingSmsReceiver. I am not quite sure how this is happening. From adb logcat I have seen that a new proecess is created with the broadcast receiver when a new Sms is received (and the app is closed).
D Zygote : Forked child process 6287
I ActivityManager: Start proc 6287:org.sudipta.sms_handler_example/u0a525 for broadcast {org.sudipta.sms_handler_example/com.shounakmulay.telephony.sms.IncomingSmsReceiver}
One thing to notice is that the plugin does not use any Service. There is a closed issue in github regarding this. The author of the plugin replied that
Yes listening in the background will work even if you don't open the app. The reason this works is we are registering a static broadcast receiver in the app's manifest file. That means the android system can initialize that broadcast receiver independent of the app.
My question is that if this really is the case. Will Android system keep the broadcast receiver alive (by forking a new process)? My understanding is that by default this is not going to work, al least that is not happening with my code, the receiver does not get hit when a call is received and the app is not running.
The author of the plugin further replied that
In most cases the device manufacturers and custom android OS environments block most of the app from performing operations in the background. That means your app won't receive any broadcasts and if it tries to start services in the background those will be killed as well.
But I have created a single sample app and using Telephony plugin and my own code (a simple receiver with the above manifest entry). While the plugin works in the background, my code doesn't.
Some help would be nice.
Thanks

LocationUpdatesForegroundService app process is still running after closing app

I am doing some investigation on Google Play Location Service
Google Play Location Samples
I am experiment the following issue on LocationUpdatesForegroundService sample application: the app process is still running after closing the app.
Steps to reproduce it:
Open app
Press "request location updates" button
Press device home button for sending application in background
Close the app
My expectation is that the app process (and all its threads) has to be stopped but it does not.
The app process is still running after closing the app
It happens when I close the app after calling
startForeground(NOTIFICATION_ID, getNotification());
How can avoid it?
It looks related with the foreground service for collecting location.
How can I stop the service while the app is closing?
As per the documentation the Service is the Android component which run continues in background even if the app is closed.
If you want to stop service when app is remove from recent task(i.e app kill) you have to used android:stopWithTask="true" in AndroidMenifest.xml for explicitly closed the service.
Example ,
<service
android:name=".LocationUpdatesForegroundService"
android:stopWithTask="true" />

Android USB ATTACH open app again even if the app is already running

My app is trying to do something with an external USB device. In the Manifest I have added the following lines of code:
<intent-filter>
<action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" />
</intent-filter>
<meta-data
android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED"
android:resource="#xml/device_filter" />
Now when the USB is attached, a prompt for me to choose which app to open is shown. The chosen app will be opened after I click onto it. If I check the "remember defaults" checkbox, my app will be automatically opened when the USB is attached. However, if I open the app first manually then plug in the USB device, it seems another instance of the app is launched and my app crashed. Any ideas?

Get notification history for android application

I am trying to do something that I am not sure is possible or not . I have created my android application "ABC" . I have used GCM to issue notifications from my 3rd party service.
Now my client asks that he wants a history of all notifications issued by the 3rd party server . I am sceptical about this .
I believe I might be able to show a list of all notifications issued by the server WHICH ARE RESPONDED upon by the user . However I think the notifications on which the user has not responded cannot be recorded by my application .
Is this true ?
I have this broadcast receiver in my code that responds to GCM messages.
<receiver
android:name="com.mob.uae.notifications.GcmBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<category android:name="com.evento.uae" />
</intent-filter>
</receiver>
Shouldn't this be called regardless of the user's action upon the notification ? . If this is called even when my application is shut down , then I think we do have a way to record all notifications targeted for me .
Any idea geniuses ?
The broadcast receiver is activated every time a GCM message reaches the device, regardless of the user's action. Therefore, if you wish to record the message locally on each device, you can do it.
You can even record it in the server side, if the code in the broadcast receiver would trigger a call to your server to notify it that the message was received by the app (in this case it would be better to start an intent service from the receiver, and let the service do the communication with the server).

Android app cannot connect to internet when try to run automate testing using TestDroid

I'm trying to record my activity in Android application using TestDroid Recorder.
My app need internet to run. And it run perfectly before I use TestDroid Recorder.
But I don't know why my app cannot connect to internet when I try to record it.
Someone said that I must setting "Internet permission in your manifest"
But I don't know how to do that? Can someone tell me what should I do?
Or why my app suddenly cannot connect to internet when I want to record it using TestDroid?
thanks
add this inside your manifest xml tag
<manifest xlmns:android...>
...
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
</manifest>

Categories

Resources