Android app that shows notification when server sending data - android

What service should I use to check every few seconds when the app is closed, if there is any data incoming from the server?
I tried Alarm Manager and schedule multiple alarms that schedule each other every few seconds. But the alarm manager is not very accurate. Then I checked out JobScheduler , but it's good for specific conditions. Is there any service that is good for this need?

Related

I want to schedule notification on specific date should i use workmanager?

I am developing one app where I want to schedule notification on a specific date and time.
Specifically on a date when the app trial is over. I am giving a 4-day app trial to the user and when the trial is over.
ex: the user installs the app on 05-03-2020 then the app will give notification on date 08-03-2020
so should I use the work manager to schedule notification or any other option in android?
WorkManager is intended for tasks that are deferrable - that is, not required to run immediately and required to run reliably even if the app exits or the device restarts. For example:
Sending logs or analytics to backend services
Periodically syncing application data with a server
Alarms (based on the AlarmManager class) give you a way to perform time-based operations outside the lifetime of your application. For example, you could use an alarm to initiate a long-running operation, such as starting a service once a day to download a weather forecast.
Alarms have these characteristics:
They let you fire Intents at set times and/or intervals.
They operate outside of your application, so you can use them to
trigger events or actions even when your app is not running, and even
if the device itself is asleep.
So for your requirement, you should be using an AlarmManager instead of WorkManager as you only need to deliver a notification.

Create Reminder Notification for Android

I know how to create scheduled notification in Android by using alarm service but what I want to do now is to create notification in more frequent way.
For example, I want to push notification for 8 hours at the interval of 20 mins. In this case, is it efficient to use alarm service or timertask will be the better choice?
No matter which method, I wish to able to cancel it in the halfway. Thanks.
Timertask starts new thread and works in it. So if your app will work in background and your app will be closed by android, you won't receive any notification. AlarmManager provides access to the system alarm services. When an alarm goes off, the Intent that had been registered for it is broadcast by the system, automatically starting the target application if it is not already running. (link). So it will start your app even if it was closed. And you have to understand how you app will work with this notifications. If it works only while user works in app, you can use timertask, but if it has to work in background(for example you will receive notification even if user doesn't work with a phone/tablet), it will be better use alarmanager. Hope it helps.

Alarm manager with asynctask in android

i have a application where i have to send gps(location) updates every 10 minutes to a server.So i am using alarm manager.I have read that async task is the best way for communicating with a server.is there any other way for sending the location updates?or is this a good method?

Android: Send location data in background periodically (every 30 seconds) and ask server for new data

Scenario:
Post to server to get any new data in background every 30 seconds for long period i.e. 12 hours. Location data needs to be sent along with this.
Current Implementation;
Service Class;
Location listener with interval of 30 seconds which sets the longitude & latitude values to two local variables
Alarm manager fires pending Intent every 30 seconds to a broadcast receiver.
Broadcast receiver starts an IntentService with location variables in the extras.
The IntentService http posts location and asks for any new data from server.
IntentService send server response back to main service class via broadcast receiver.
Service class starts_sticky to ensure restart by the OS.
I have tried a few different variations;
I've tried using a Handler and runnable to handle the timing mechanism for posting to the server however, the postDelay time went from 2 minutes to 7 minutes when device is asleep.
Also, tried firing IntentService directly from alarm manager but could not change PendingIntent extras with the most up-to-date location variables.
Questions;
Is the current implementation the way to go?
Would going down google's GCM route be much more beneficial?
How can you vigorously test the service class especially with respect to recovering from the OS killing it?
Thanks in advance.
To avoid the OS to kill your service, you must notify the user that your service is an ongoing service, like described in http://developer.android.com/guide/components/services.html#Foreground.
I managed to have my service running every 2 min at background using AlarmManager and WakeLock, like described in this answer. Even when the device is sleep, it runs every 2 minutes. Just set the Alarm to repeat like
alarmMgr.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime()+5000, 60000 * ALARM_PERIOD_IN_MINUTES, alarmPendingIntent);
instead of setting it to a time.

android alarm with background goes

In one of my application I am providing multiple alarm and its working perfectly fine.
I want to extend that alarm features with some background internet related tasks. When alarm is set for particular given value application start checking for that value from my server at every interval of 30 seconds. whenever same value is returned the alarm goes off. My server is updated with new data at every 30 seconds.
Right now I am setting multiple alarm with broadcast receiver and different pending intent ids. How should I start to implement?
I am confuse between which should I use for this Alarm manager, Services ,Receivers, Handler, AsyncTask?
Any help is appreciated.
Thanks.
You could create a new BroadcastReceiver and set a repeating alarm for every 30 seconds to trigger the receiver when you wish to start polling your server.
The new receiver could then check your server and if the condition is met could call cancel on the alarm manager to cancel the repeating alarm which triggers it.

Categories

Resources