Android Question Show notoficacion programmed to several hours

Cebuvi

Active Member
Licensed User
Longtime User
Hello,

I am working on an app that submits notifications several hours apart by a fixed or variable interval. For example, 10:00, 11:00 and 12:00.
After researching the forum I get it to work using a service in background and StartServiceAtExact but I have the following problems
  • if the interval is longer than 10 minutes, the first notification is made at the correct time, but the following notifications are moved 2 minutes. That is to say the first occurs at 10:00, the second at 11:02 and the third at 12:02 (that increase of 2 minutes this for an hour or for two hours)
  • when the interval is large the system sometimes kills the service and notifications stop occurring.
If anyone could help me, I’d appreciate it.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
StartServiceAt / Exact is supposed to start the process if it is not currently running. However for various reasons it doesn't always work on all devices.

You have several options:

- Create a foreground service and try to keep your app running all the time. It will work on most devices.
- Schedule the app to start a few minutes earlier, use a foreground service with partial lock to keep the process running and show the notification on time.
- Switch to push notifications.

Relevant example: Background location tracking
 
Upvote 0

Cebuvi

Active Member
Licensed User
Longtime User
Thanks, Erel, I’m going to try out the different options. Clearly the problem is with the device because with the virtual device it works properly.
 
Upvote 0

Cebuvi

Active Member
Licensed User
Longtime User
I’ve tried the first option
- Create a foreground service and try to keep your app running all the time. It will work on most devices.

The same thing continues to happen while maintaining the foreground service and lock.PartialLock. What I do not understand is that the 2 minutes are always added between the first notification and the second, but between the second and the third the time that I have set is maintained, for example 60 minutes.

To test the second option,
Schedule the app to start a few minutes earlier, use a foreground service with partial lock to keep the process running and show the notification on time.

I understand that to schedule the app to start a little before I have to do it with a servce that in turn starts another foreground service?
 
Upvote 0

Cebuvi

Active Member
Licensed User
Longtime User
I keep working with the second option and I read in the forum that I need to start the service every less than 15 seconds so that Samsung devices allow me to give the notification at the set time. I’m gonna do this in the last four minutes.

Is it possible to call StartServiceAtExact from a subroutine within the service itself?
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
I read in the forum that I need to start the service every less than 15 seconds so that Samsung devices allow me to give the notification at the set time.
This is wrong. You shouldn't call StartServiceAt with an interval less than 15 or even 30 minutes. You should instead use a foreground service.

Is it possible to call StartServiceAtExact from a subroutine within the service itself?
Yes. Just pass Me as the first parameter.
 
Upvote 0

Cebuvi

Active Member
Licensed User
Longtime User
This is wrong. You shouldn't call StartServiceAt with an interval less than 15 or even 30 minutes. You should instead use a foreground service.

This does not work on a Samsung S8. If, for example, at 9:00 I start a service for him to give a notification at 11:00. To avoid problems I start the service in foreground mode at 10:56 and I use StartServiceAtExact by 11:00, if the screen is off the notification occurs at 11:02.

The first StartserviceAtExact is produced at the scheduled time, but the second is produced 2 minutes later even if there is only a 4 minute difference. If the screen is turned on, it occurs at the scheduled time.
I test that those four minutes the servce is running on foreground for example by making an account and even if the time since the first planning is more than 20 or 30 minutes, the error occurs.
 
Upvote 0

Cebuvi

Active Member
Licensed User
Longtime User
I’m sorry for the delay in answering, but the tests take a long time to prove that everything works.

With the last suggestion I get the notification to occur at the desired time even if they are scheduled a long time in advance, a day or more.

But I have a question, does a service running on foreground consume a lot of battery?
 
Upvote 0

Cebuvi

Active Member
Licensed User
Longtime User
Is there a way to maintain a service or process that determines the hours of notification in backgroud and only start a service in foreground to give the notification, without the system killing the service/process in background? This is in order not to waste battery in excess.

Thanks
 
Upvote 0

Cebuvi

Active Member
Licensed User
Longtime User
But if I use a background service to use StartServiceAtExact, if it is more than 30 minutes after the first service is created until the instruction StartServiceAtExact is executed it is almost certain that the system kills the service in backgroud and the instruction is not executed. It works great if between the two events, service creation and StartServiceAtExact instruction up to 20 minutes. I need more time.
 
Upvote 0

Cebuvi

Active Member
Licensed User
Longtime User
For example , I want to show a notification tomorrow at 09:00h.
I have an app with two services: ser_Crono and ser_Notif.
In the app I enter the time 09:00 and start the service ser_Crono in background in which there is a StartServiceAtExact command to boot it at 8:54 and start on foreground the ser_Notif having a Sleep to display the notification at 9:00.
If the time for the notification is short, 20 minutes, everything works, but if one hour is missing, the system kills the service and everything stops. This happens on the real Samsung device, on the virtual device there is no problem.

Sorry for my bad english.
 
Upvote 0

Cebuvi

Active Member
Licensed User
Longtime User
I send you a test app and a log log file when I run it on a Samsung S8.
As you can see from the log, notifications were scheduled every 10 minutes and two were given, before the third an error occurs.
If the time until the first notification is, for example, one hour, the system kills the service and the app stops without giving any error in the log.
Thank you very much for your patience, many users of my app have asked me for this feature that I found simple but it is driving me crazy.
 

Attachments

  • Logger connected to S8.pdf
    48.1 KB · Views: 302
  • SNotifTest.zip
    36 KB · Views: 313
Upvote 0
Top