I am working on an Android project that has a part which dose this:
1) the user enter a data from a data field and save it in a text file
2) the app should send notification even if the application is killed by the os, at that date, the one wrote by the user.
For example:
I write 31.01.2015
The app will notify my only on 31.01.2015 even if i don't open that app anymore.
The question is how do i have to do this?
Thanks!
It sounds like you want a notification to be posted to the notification bar.
If so I advise using an alarm.
However, chances are if this is days in the future, the phone may be shut off. So you should store when the alarm should go off, create a Broadcast receiver for the on boot complete event (this requires a permission), and re-setup the alarm when the boot is complete.
This should allow the notification to appear, independent of the apps life-cycle, as long as the app is not uninstalled.
Note: You will have to calculate the milliseconds between the date for the alarm, and the current time. Calendar should help.
Related
I am trying to create a timer (in a game project) that will countdown 24 hours (even when the user closes the app) and then it will send the user a notification that says the timer finished, but I can't code a Countdown timer that runs when the user closes the app, I have used countdown timer that works with broadcast receiver but it stopped working after the use closed the app completely(probably due to Oreo+ background process limitations).
Can anybody help me?
Not close to my computer now, so can't write code, but you could do several things.
If you already have some kind of a backend, you could then set up an endpoint that will schedule a notification to the device.
So I guess once the user clicks a buttom, You would send a request to the backend and then after 24 hours, server sends a notification (via Firebase cloud messaging, for example).
That way, you are 100% sure user will receive it no matter whether the app is open or closed.
I suggest that you explore (remote) push notifications.
I hope this helps.
For my app I want to fetch data from a server in the background. When the user wants to be notified about specific changes, I want my app to keep fetching data even when the app was closed. I was researching about this topic and found out that the possibilities of running services continuously also after the app is closed changed with Android Oreo. What I don‘t see are the alternatives to handle running services in background with newer versions of android. The most proposed solution I saw is using a Foreground Service. But since I don‘t have any user interaction, I don‘t think this solution fits perfectly. Also I read about the WorkManager but unfortunately it has a minimum time interval of 15 minutes. Are there other alternatives to process data in the background continuously than a Foreground service and a WorkManager?
You don't need "user interaction" to have a Foreground Service, what you need is a Notification that cannot be removed while the service is in foreground.
If your notification Context is the service itself, tapping on the notification will take the user to the "settings" where he/she can force close the service for example).
You can start your FG service in a BOOT_COMPLETED broadcast receiver. You have to quickly (5 seconds or less) send it to Foreground or android will throw an exception. Unfortunately, there's a bug which Google thinks it's ok and closed as "won't fix" but you'll still need to work around.
Long story short, have an onCreate in your service:
#Override
public void onCreate() {
super.onCreate();
final Notification notification = createNotification();
startForeground("SOME_UNIQUE_ID", notification);
}
You get the idea (the createNotification() is whatever you want to create a Channel (if O+) + Notification.
HOWEVER, all this is nice, but it will have a service running all the time, which is probably not what you should do.
You can use an Alarm a trigger a more efficient (in terms of scope) Intent Service to do the job, schedule another alarm in, say 5 minutes, and do it again.
Or -as commented- you could use Firebase, and Notifications to react to a change.
All in all, those are the options.
I'd say that unless you truly need "real time" updating, the 15 ~ of WorkManager is usually fine, if you can deal with the fact that you have to design your code to be very independent, because WorkManager cannot receive much information and it's a pain to use for some things...
IN bound service when I lock the device after the 70- 80 seconds service stops I am testing on realme 3 pro and one plus 5 both having API 9 PIE. How to keep service running until the app is in recent activity. Even foreground services are also stopped. Thanks in advance.
Realme, Oneplus, Samsung, Xiaomi, Huawei and a few other manufacturers have their own layer of "Battery Saver" or "Security" that kills or restricts background running apps in order to improve security/battery backup, unless user white-lists your app in the relevant device settings.
What i found out about Xiaomi is that they white-list well known apps like Facebook, Whatsapp, etc. But you cannot request Xiaomi to white-list your app.
These manufacturer apps simply terminate your app process. So your app should behave in such a way that termination at any moment should not create inconsistent state.
Also, you can always show a popup to user after installation asking him/her to go and white-list your app in battery saver. In Xiaomi, the user will need to select 'Do not restrict background activity' and also enable 'Auto Start' for your app. As far as i know, there is no other solution.
Conclusion: Visit https://dontkillmyapp.com and see how the apps are whitelisted for your OEM. Once whitelisted, it will work as expected.
But out there in real world, it will get killed and users won't manually whitelist it.
Custom Roms have it's own security apps and it close the app service when u remove it from recent or after few minutes.
To keep running your app in background you have to enable autoStart from settings
Refer this : How to fix the issue that the activity is killed automatically after some minutes in newly version as like vivo and oppo
Facing same issue from long time, Try to add FCM in your application and start service from it directly or else set alarm and startservice.
In some cases above solution work. i have added above solution in 4 project but succeed in only 1.
Issues which I have found while overcoming problem with firebase.
Firebase Notification coming to app but Background service not starting
Firebase Notification Not coming to application
Firebase Notification coming and service starting with Broadcast Receiver (Sometimes
service not starting directly from onMessageReceived() method of Firebase so i
decided to set broadcast Receiver ) but sometime its stop working.
Issue which I have found without implementing Firebase
If we set background service to run after every 5 mins than for 1-2 hours its
running on 5 mins interval but after 2 hours time gap increased and service runs on
15 mins of interval and in some devices its run on 20-30 mins of interval(i found
this in right time because phone may goes in ideal mode).
As mention in your question, sometimes any of above implementation doesn't work.
Tip : What I found in system alarm log ?
I have set alarm for 2019-08-08
11:07:00 which not fired and current time is 2019-08-08 11:10:00 but its showing in
pending in system alarm log.
You can check system all Alarm set by your applications
D:\SDK\platform-tools>adb shell dumpsys alarm
Currently the Android application I have taken in charge performs a "pull" on the server every minute to recover data and update a fragment with this data. Obviously this does not work when the device switches to doze mode. So I decided to use FCM as Google recommends.
Constraints :
The user needs to know that new data is available even in doze mode.
To not change the application too much, I do not want to send the data in the firebase message but rather send an https request to the server when I receive the fcm message.
The fcm message must:
Advise the user that new data is available with a notification.
If the user presses the notification OR returns to the application after turning the screen on, the https request must be triggered and fragment has to be updated.
I will add that it must be triggered at the latest when the user returns to the application.
My solution for now
I used a data message with a high priority instead of a notification message because a notification message need the user to tape the notification to trigger action.
In onMessageReceived :
I send the notification that redirects to my application.
I send my request to the server and update my application.
Disadvantage of my solution :
If my app is killed by the system while the screen was off what's going on?
My request has a time limit to complete when the phone come out of doze mode.
Questions :
Is this solution the best possible ?
Is there another way to proceed?
Perhaps could i schedule a task that runs immediately when user resume my app in onMessage received ? But i dont know how to do that.
Use WorkManager to schedule the background work to run immediately. It will also retry until your code indicates that it's successful.
I answer myself to close the subject and hope it can help someone !
After a night ... I definitely do not like the workmanager solution. Even if in practice it can work I do not like to use APIs for unplanned uses. If google tells me that it is not garanty to launch task immediately, they are (much :)) better than me and I believe them!
In this case, I think that my task may be postponed, for example because the OS wants to group calls with other apps. There may be internal conditions i dont know about.
Especially since I found a much simpler solution.
In onMessageReceived, I use a simple boolean indicator.
In the onResume of the activity I want to update I read this indicator and I launch my request if true for example.
I no longer need a scheduler, and after all if it is not done 100% reliable, it does not matter :
If fcm could send me a message there is a lot of chance there is a network when the user turn on the screen.
Even if there is none it is not very serious since I can warn the user who can proceed otherwise (by phoning for example !!!!)
How can I create a service which will push a notification at a specific time everyday? I've been trying many method on google but they did not work well. Anyone know a good way to do this? Someone say its better to use AlarmManager, while some others say its should be JobScheduler because AlarmManager is deprecated.
Anyone have a good reference?
If you want something to be executed on exact time then you should use AlarmManager.
Based on the documentation:
Standard AlarmManager alarms (including setExact() and setWindow())
are deferred to the next maintenance window.
If you need to set alarms that fire while in Doze, use setAndAllowWhileIdle() or setExactAndAllowWhileIdle().
Alarms set with setAlarmClock() continue to fire normally — the system exits Doze shortly before those alarms fire.
You cannot trigger something at specific time using JobScheduler. The execution of job is under OS control. The jobs will be deferred during doze mode hence trigger at exact time won't be possible.
Use JobScheduler when you want to ensure that a Job must be triggered within specific interval however the execution at exact time is not crucial.
Set up a backend server for Push notification . When Api hit request to FCM for push request with device id then device will get notification. For Automatic push from backend , use cron from backend .A background process run in backend api and will hit FCM in some time interval and device will receive PUSH Notification.