Push notification every day - java

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.

Related

Run service frequently also after app was closed

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...

Best way to send firebase message or notification that trigger pull request from server

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 !!!!)

Update Location after every 2 minutes on server android

Method to upload location to server every 2 minutes.
I am currently using Alarm Manager currently and some operating system close the Alarm Manager and the alarm donot ring after a particular interval of time. What is some other solutions that I can use?
Please read this article:
https://developer.android.com/topic/performance/scheduling.html#am
The AlarmManager API is another option that the framework provides for scheduling tasks. This API is useful in cases in which an app needs to post a notification or set off an alarm at a very specific time.
Location Manager - Android system offers location changes in runtime through location manager. Repeating updates can be achieved by using LocationManager#requestLocationUpdates

Android alarm repeated every (less then) 1 sec

I'm trying to check what app is in foreground and launch my app if user locked that app. To do so I fire alarm, start service to check for active app and set alarm to fire in one socound from now. But somehow the alarm is allways delayed to 5 secounds..
This is how Im setting it:
alarmMgr.setExact(AlarmManager.ELAPSED_REALTIME,
SystemClock.elapsedRealtime() + 1000, alarmIntent);
I've tried it on android 5.1 and also on 6.0. It's the same.
Do you know why it is delayed? or do you know about any better way to check for active app as soon as possible? because I think this way consume lot of battery.
Thanks.
So this is what I was looking for. Only disadvantage is that user has to enable accessibility service manualy from settings.
https://stackoverflow.com/a/27642535/4506191

How to send notification even if app is killed

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.

Categories