mauron85 / react-native-background-geolocation

Background and foreground geolocation plugin for React Native. Tracks user when app is running in background.
Apache License 2.0
1.33k stars 559 forks source link

Foreground service notification appears with delay 10s in Android 12 (SDK 31) #560

Open andkom opened 2 years ago

andkom commented 2 years ago

Your Environment

Context

In Android 12 there is UX delay for foreground service notifications:

"To provide a streamlined experience for short-running foreground services, devices that run Android 12 or higher can delay the display of foreground service notifications by 10 seconds, with a few exceptions. This change gives short-lived tasks a chance to complete before their notifications appear."

Services that show a notification immediately If a foreground service has at least one of the following characteristics, the system shows the associated notification immediately after the service starts, even on devices that run Android 12 or higher:

The service is associated with a notification that includes action buttons. The service has a foregroundServiceType of mediaPlayback, mediaProjection, or phoneCall. The service provides a use case related to phone calls, navigation, or media playback, as defined in the notification's category attribute. The service has opted out of the behavior change by passing FOREGROUND_SERVICE_IMMEDIATE into setForegroundServiceBehavior() when setting up the notification."

Expected Behavior

When service starts, foreground service notification appears without dalay

Actual Behavior

When service starts, foreground service notification appears with 10s delay

Possible Fix

Set notification's category to CATEGORY_NAVIGATION in https://github.com/mauron85/background-geolocation-android/blob/58464febf6901f1e489817e4425418d48443929b/src/main/java/com/marianhello/bgloc/sync/NotificationHelper.java#L70

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
    builder.setCategory(Notification.CATEGORY_NAVIGATION);
}

Steps to Reproduce

1. 2. 3. 4.

Context

Debug logs

yterletskyi commented 1 year ago

This is expected behaviour starting from Android 12 (31). For full info and changing the behaviour please see: https://developer.android.com/guide/components/foreground-services#notification-immediate

One of the most popular ways to make a notification appear immediately is to add:

builder.setForegroundServiceBehavior(NotificationCompat.FOREGROUND_SERVICE_IMMEDIATE)
enesaslngrn commented 2 months ago

builder.setForegroundServiceBehavior(NotificationCompat.FOREGROUND_SERVICE_IMMEDIATE) it worked! Your're amazing bro. Thanks.