Alarm settings when closing the application

How to configure local notifications without forcing the user to open the application. I need my app to set a local notification for sunrise and sunset, but I don't want people to open the app. I know that I can have up to 64 notifications via scheduleLocalNotification , but I need to set it for a year so that I can run the application in the background and set alarms for future sunrises and sunsets in the background.

+1
source share
2 answers

The simple answer is: you cannot. The application cannot work when it wants in the background; he cannot schedule a timer to wake himself, to send more notifications when they should.

The only way you could get closer to something like this is with a server that sends a background push notification to your application as an awakening when a new batch of 64 notifications gets close to the desired message. However, this will rely on the user not terminating your application. If the user does this, you will need to send the user a non-phonon push notification and hope that he clicks on it to launch your application.

+1
source

The Android Awareness API has recently announced new features that provide a simple solution for your use case (this avoids explicitly managing a location request or calculating sunrise times). The way to achieve what you are trying to do is to create and register a TimeFence specified regarding sunrise / sunset.

For instance:

 // Create TimeFence AwarenessFence sunriseFence = TimeFence.aroundTimeInstant(TimeFence.TIME_INSTANT_SUNRISE, 0, 5 * ONE_MINUTE_MILLIS); // Register fence with Awareness. Awareness.FenceApi.updateFences( mGoogleApiClient, new FenceUpdateRequest.Builder() .addFence("fenceKey", sunriseFence, myPendingIntent) .build()) .setResultCallback(new ResultCallback<Status>() { @Override public void onResult(@NonNull Status status) { if (status.isSuccess()) { Log.i(TAG, "Fence was successfully registered."); } else { Log.e(TAG, "Fence could not be registered: " + status); } } }); 

You will receive callbacks when the fence evaluates to TRUE at sunrise, and when it returns to FALSE 5 minutes after sunrise.

Please check the Fence API code snippets to add custom application logic.

+1
source

Source: https://habr.com/ru/post/1271182/


All Articles