How to handle onNotificationReceived from Firebase every 10 minutes while app is in background. Android 9.0

Tim 1 :

I need to be able to send my GPS-location to a server every time I receive a silent-push notification from Firebase. The timer for sending a notification is currently set to every 10 minutes. When the phone is charging this is not a problem, but when it's idle and the application is in the background, the onMessageReceived from FirebaseMessagingService is only called every few hours. This leads me to believe it has something to do with the new power management rules for Android 9.0. But for my application to work I need to be able to send my location every 10 minutes. Not just sometimes.

I have tried to set the battery optimisation to 'not optimised' using Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS
This does not seem to have any effect.

I also tried setting the priority of the Firebase notification to High. This works, but only for 10 messages per day, which means it's not a solution for my application. (Source: Power Management restrictions)

I am now trying to always have the application in the 'Active' bucket by opening a foregroundService. This should make sure the app is never in any other bucket, where the notifications can be deferred for a few hours.

An app is in the active bucket if the user is currently using the app, for example:

  • The app has launched an activity
  • The app is running a foreground service
  • The app has a sync adapter associated with a content provider used by a foreground app
  • The user clicks on a notification from the app

If an app is in the active bucket, the system does not place any restrictions on the app's jobs, alarms, or FCM messages.

(Source: Power Buckets Android9).

This does not seem like a solution I should want, though, since it might not be best practice. And it doesn't seem to work either anyways.

This is my onMessageReceived function:

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    Log.d(TAG, "From: " + remoteMessage.getFrom());

    if (remoteMessage.getData().size() > 0) {
        Log.d(TAG, "Message data payload: " + remoteMessage.getData());

        Map<String, String> params = remoteMessage.getData();
        JSONObject object = new JSONObject(params);

        PushNotificationManager.getInstance().handlePushNotification(object, getApplicationContext());
    }
}

Right now I am clueless as to why the Firebase messages do not enter the onMessageReceived function.

Tim 1 :

Fixed by using AlarmManager. Schedule an alarm in 10 minutes, and when completing the code within the alarm, schedule a new alarm in 10 minutes.

AlarmManager can wake the phone if it is in doze mode, which means all code inside the trigger will be executed properly.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=123783&siteId=1