xamarin.formsandroid-14

Braodcast receiver not working in Android 14


I'm trying to run my app on an Android 14 device, where I'm sending a broadcast message from my main app ( 1st app) and it is subscribed by one of my other apps say 2nd app( installed on same device).On the first install, both of my apps are sending the receiving the broadcast message without any issue. However, if i force close my 1st app, and then relaunch it, the broadcast messages send by my 1st app are no more coming to my background app ( my 2nd app).

The target framework version and target API level is Android 13 for both apps and they are developed in Xamarin.Forms

Below is the code that I am using:

//MainActivity of 2nd app :
 protected override async void OnCreate(Bundle savedInstanceState)
{
 IntentFilter intentFilter = new IntentFilter(com.abc.xyz); 
 RegisterReceiver(_recieverClass, intentFilter);
}

 protected override void OnDestroy()
 {
   UnRegisterReceiver(_recieverClass)
 }

//ReceiverClass  of 2nd app
 [BroadcastReceiver(Enabled = true, Exported = true)]
 [IntentFilter(new[] { com.abc.xyz })]
 public class ReceiverClass : BroadcastReceiver
    {

      public override void OnReceive(Context context, Intent intent)
      {
          //Business logic 
      }
    }


//Sending broadcast message in 1st app :
 Intent intent = new Intent();
 intent.SetAction(com.abc.xyz);
 intent.SetFlags(ActivityFlags.FromBackground);
 SendBroadcast(intent);

I already tried declaring the broadcast messages in Android Manifest explicitly of my 2nd app, but that did not work.

<application android:label="@string/app_name" android:allowBackup="false" android:usesCleartextTraffic="false">
        <receiver android:name=".ReceiverClass" android:enabled="true" android:exported="true">
            <intent-filter>
                <action android:name="com.abc.xyz" />
            </intent-filter>
        </receiver>
</application>

Been through the Android 14 and google docs, and didn't find any solution for it. Am I missing something here?


Solution

  • Finally, I got the solution for this issue. Basically, my broadcast for the background app was getting cached and killed on Android 14. To handle this, added Priority = int.MaxValue in the broadcast IntentFilter definition, which fixed the issue.