xamarin.formsbroadcastreceiverandroid-12

BroadcastReceiver does not work on Android 12


In my Xamarin Forms project I have:

[BroadcastReceiver(Permission = "RECEIVE_BOOT_COMPLETED",
    Exported = true,
    Enabled = true)]
[IntentFilter(new[] {Intent.ActionBootCompleted})]
public class GeofenceReceiver: BroadcastReceiver

I use it for GeofenceTransitionEnter and GeofenceTransitionExit events. I also have ACCESS_FINE_LOCATION and ACCESS_BACKGROUND_LOCATION permissions.

But OnReceive method is not called on API 31. I have not this problem with lower APIs.

Android 12 targetSDKVersion 31 challenges (Broadcast Receiver, Pending Intent) Crash Issues - doesn't work for me


Solution

  • I just added something like this into GepfencePendingIntent:

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
            PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_MUTABLE)
        } else {
            PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT)
        }
          
    

    I found it here: https://www.flybuy.com/android-12-pendingintent-mutability-and-geofences