Android Question Notification problems with Android 9 and 10.

davemorris

Active Member
Licensed User
Hi, Guys
I would appreciate some suggestions or pointers.

I am having problems with my App recently (on Android 9 and 10 phones), not responding to notifications whilst the app is left running but the phone is locked for about 5-10 minutes.
The notifications work OK as expected whilst the App is running or for a short period after the phone is locked.
Also, when the Android 9 or 10 phones are unlocked any previous notifications then appear.
Not sure about Android 10 but the Android 9 phone notifications have worked previously when the phone was locked for long periods.

Android 6 phones work great (I do not have an Android 7 or 8 phone to test). Also the iPhones with iOS v12 and v13 work OK.

I have tested the same phones using WhatsApp and the notifications appear whilst the phone is locked, regardless of how long they have been locked.

As all phones work OK on WhatsApp and older Android version phones work with my App, I hope is just some setting I need for Android 9 and 10.

Thanks in advance
Dave
 

davemorris

Active Member
Licensed User
Hi, Erel - thanks for the quick response

Sorry I should have made it clearer.

The Push notifications don't appear after the phone is locked for about 5 minutes (prior to this the work ( Problem with Android 9 & 10 and Android 6 and iOS are OK).

Kind regards
Dave
 
Upvote 0

davemorris

Active Member
Licensed User
Hi, Pendrush

As far as I can see All battery optimization is OFF (and I can't see how to exclude my App from Battery Optimization)

Have you experience with this problem on Android 9 or 10, if so what settings did you change?

However, I would agree it does appear to be a battery saving problem - but how is it that WhatsApp does not suffer from the same problem?

Regards

Dave
 
Upvote 0

Pendrush

Well-Known Member
Licensed User
WhatsApp, Viber, FB Messenger, etc., are excluded in factory rom.

Try this code
B4X:
    Dim p As Phone
    If p.SdkVersion >= 23 Then
        Dim JavaObjectOpt As JavaObject
        JavaObjectOpt.InitializeContext
        Dim batteryIgnore As Boolean = JavaObjectOpt.RunMethod("isIgnoringBatteryOptimizations", Null)
        Log ("Battery optimization ignored: " & batteryIgnore) ' should be TRUE if app is excluded from optimization
        If batteryIgnore = False Then
            Dim msgOptiBatt As Object = Msgbox2Async("Do you want to exclude app from battery optimization?", "Battery optimization", "Yes", "", "No", Null, False)
            Wait For (msgOptiBatt) Msgbox_Result (Result As Int)
            If Result = DialogResponse.POSITIVE Then
                'if YES, after starting intent you need to select all application in drop down, then you need to find your application and turn on or tick box after application name.
                Dim StartIntentBatt As Intent
                StartIntentBatt.Initialize("android.settings.IGNORE_BATTERY_OPTIMIZATION_SETTINGS", "")
                StartActivity(StartIntentBatt)
            End If
        Else
            MsgboxAsync("App is already excluded from battery optimization"))
        End If
    End If
 
Upvote 0

Pendrush

Well-Known Member
Licensed User
As from my experience, excluded app work great, never miss StartExactAt, never miss notification.

EDIT:
Non excluded app on most models from Oppo, Vivo, Huawei, Xiaomi, etc, will be killed after max 10 minutes even if app is running Foreground service.
One of my apps have over 1.000.000 downloads (with over 500.000 active installations). I have experienced everything so far.
 
Last edited:
Upvote 0

marcick

Well-Known Member
Licensed User
Notification are never missed. But delayed. Some seconds, some minutes, it depends on the doze windows timings.
On my galaxy note 10 with the last Android version, I see this also with whatsapp.
It looks like the "ignore battery optimization" option is "ignored" by the device.
I'll repeat again my test but this was my conclusion.
I hope I'm wrong somewhere because this is a very unpleasant issue as I need instant notifications.
 
Upvote 0

davemorris

Active Member
Licensed User
Hi Guys
I am overwhelmed by the suggestions, thanks they all need considering.
However, it all appears to relate to a Battery optimization problem, but Erel has yet to give a suggestion.

Starting with marcick, he appears to have the same problem and looking for an answer - that's good as I am not the only one seeing this problem.

Pendrush - provided some code so I need to give it a try - Sorry but I can't understand your last post placed after you provided the code.
As from my experience, excluded app work great, never miss StartExactAt, never miss notification.

EDIT:
Non excluded app on most models from Oppo, Vivo, Huawei, Xiaomi, etc, will be killed after max 10 minutes even if app is running Foreground service.
One of my apps have over 1.000.000 downloads (with over 500.000 active installations). I have experienced everything so far.
Could you please re-phase the quote - I would suggest that using your code does the trick, am I correct?

However, Erel did respond with a question so maybe later he can throw some light on the problem.

Once again thanks for the input, lets all try to work to a solution - I am sure it would help all Apps requiring Push notifications.

Dave
 
Upvote 0

davemorris

Active Member
Licensed User
Hi, Erel

Can I start by saying this don't not look like the standard "I can't get notifications to work" the phone App has worked since I switched to using notifications as a method of sending important messages about 2 years ago. Also, I don't have problems with Android 6 and iOS phones (can't get Android 7 or 8 phone to test) only Android 9 and 10.

So in answer to your questions.
I am way beyond using B4j, the sender is a Windows 10 machine using .net (combination of vb.net and #c). I do have the original b4j code on archive - I can certainly try it if you believe its a Windows related problem.

I will certainly try a restart (I have known it work in the past for other straight problems). However, I must stress the notifications system works great until the phone (sill running the App) goes into a locked state (either manual or automatic timeout) for more than about 5 minutes. Prior to that point notifications get through.


Regards
Dave
 
Upvote 0

marcick

Well-Known Member
Licensed User
Erel can do nothing to fight Android or device behaviour and you will experience the same problem sending notification with the standard B4j tool.
The concept in battery optimization is that if you are using the device you are interested in real time notification. If your device is on the table since 10 minutes you are doing other and no matter if you receive the notification with two minutes delay.
There are a few people (like you and me ...) that really need instant notifications and they are not interested to us .....
If you have your own server you can follow an alternate method. Follow the sample "background location tracking" to learn how to run a service forever in background. Then create a sort of communication between the device and the server (for example with a websocket) so that you can send your custom notification and have also feedback when it is received.

But .. again ... The device can decide to kill your service and you are in troubles. It can happens on some device.
 
Upvote 0

Pendrush

Well-Known Member
Licensed User
I have bough Xiaomi Pocophone F1 phone, just for testing my app.
If exclude my app from MI battery optimizer, notification has delay when phone go to doze mode, but notification is delivered.
If my app is not excluded from Mi battery saver, 50% of notification arrive, 50% are lost.
Standard battery optimizer cannot be found in phone from any menu in settings. Code from post #8 is only way to turn off system battery optimization for my app on Xiaomi phone.
Also I have an app with widget. Widget need to be updated every hour in exact time, for example 07:15h, 08:15h, 09:15h, etc. This won't work if app is not excluded from battery optimization.
Without exclusion, widget is updated 2-3 times a day, instead 24 times a day.
 
Upvote 0

marcick

Well-Known Member
Licensed User
Question: are you sure your widget has no delay, since the screen is off when they occour ?
I'm not saying that "exclude from battery optimization" has no effect (I use the method also) but does not avoid delay in notification (I have no experience with widgets).
 
Upvote 0

DonManfred

Expert
Licensed User
There is no warranty that a Notification is send in realtime by FCM. This is documented in the Firebase FCM Docs.
A delay may occur.
 
Upvote 0

marcick

Well-Known Member
Licensed User
There is no warranty that a Notification is send in realtime by FCM. This is documented in the Firebase FCM Docs.
A delay may occur.
Really, it works always fine and realtime. The problem is not Firebase, but the device that "sleep" and cause delay in reception.
 
Upvote 0

davemorris

Active Member
Licensed User
Hi, Guys

Thanks marcick for answering DonManfred's post. I totally agree with you (noting that this problem does not apply to all versions of Android), this is NOT a problem with Firebase delays.
However, DonManfred is correct the FCM message can be delayed (I have found up to 2 minutes but usually received immediately) and Apps need to take this delay into account.

Also marcick's quote from his earlier post
If you have your own server you can follow an alternate method. Follow the sample "background location tracking" to learn how to run a service forever in background. Then create a sort of communication between the device and the server (for example with a websocket) so that you can send your custom notification and have also feedback when it is received.
This is an interesting point about using WebSockets - We have our own server so that could be a solution, I will investigate (however currently the server would need an upgrade to support WebSockets).

Once again, thanks to everybody for their input

Dave
 
Upvote 0
Top