Xamarin.Forms project having trouble with Android pushes when terminated

Jesse Wonder Clark 1 Reputation point
2023-11-02T22:41:30.9166667+00:00

So I know I have kind of a Frankenstein's monster going here, but...

I have a Xamarin.Forms project, and I'm using Parse as a backend and for push notifications (supplied by Sashi.do).

Push notifications work fine in iOS, and between iOS and Android mostly, and in Android-only communication mostly, with this exception:

  • about 60% of the time, if the Android app is terminated, the push notification will not be received

Here's the Frankenstein part: I couldn't for the life of me get Parse pushes to work on the receiving end in Android, so I'm actually using a nuget called FirebasePushNotificationPlugin (https://github.com/CrossGeeks/FirebasePushNotificationPlugin/tree/master) to receive Android pushes. I'm not using it to send them though, Parse does that just fine.

So yeah, on Android I push with Parse pushes and receive with the FirebasePushNotificationPlugin, and believe me, I'd like to do it another way, but I couldn't find any direction on how to make it work with Parse alone.

So here's some of the relevant code for reference.

AndroidManifest.xml:

My Android Manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.concierxge.Android" android:versionName="4.1.4" android:versionCode="27">
    <uses-sdk android:minSdkVersion="28" android:targetSdkVersion="33" />
    <uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
    <application android:icon="@mipmap/ic_launcher" android:label="CONCIERxGE">
        <meta-data android:name="com.google.firebase.messaging.default_notification_icon" android:resource="@drawable/ic_notification" />
        <receiver android:name="parse.ParsePushBroadcastReceiver" android:exported="true">
            <intent-filter>
                <action android:name="com.parse.push.intent.RECEIVE" />
                <action android:name="com.google.android.c2dm.intent.REGISTRATION" />
                <action android:name="com.parse.push.intent.DELETE" />
                <action android:name="com.parse.push.intent.OPEN" />
                <category android:name="com.concierxge.Android" />
            </intent-filter>
        </receiver>
    </application>
    <uses-feature android:name="android.hardware.telephony" android:required="true" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.VIBRATE" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
    <permission android:name="com.concierxge.Android.permission.C2D_MESSAGE" android:protectionLevel="signature" />
    <uses-permission android:name="com.concierxge.Android.permission.C2D_MESSAGE" />
</manifest>

My Firebase-initializing code, found in my Android MainLauncher activity:

if (Build.VERSION.SdkInt >= Android.OS.BuildVersionCodes.O)
            {
                ////Change for your default notification channel id here

                FirebasePushNotificationManager
                    .DefaultNotificationChannelId =
                    "FirebasePushNotificationChannel";
                FirebasePushNotificationManager.
                    DefaultNotificationChannelName = "SecondChannel";
            }
            // Enable Firebase verbose logging
            firebaseAnalytics = FirebaseAnalytics.GetInstance(this);
            firebaseAnalytics.SetAnalyticsCollectionEnabled(true);

            //If debug you should reset the token each time.
#if DEBUG
            FirebasePushNotificationManager.Initialize(this, true);
#else
            FirebasePushNotificationManager.Initialize(this, false);
#endif
            FirebasePushNotificationManager.ProcessIntent(this, Intent);

            CrossFirebasePushNotification.Current.Subscribe("Todos");

            FirebasePushNotificationManager.IconResource =
                Resource.Drawable.ic_notification;

            FirebasePushNotificationManager.LargeIconResource =
                Resource.Drawable.ic_notification;

            FirebasePushNotificationManager.DefaultNotificationChannelImportance
                = NotificationImportance.Max;

            CrossFirebasePushNotification.Current.OnTokenRefresh += (s, p) =>
            {
                //custom token-logging code
            };
            CrossFirebasePushNotification.Current
                .OnNotificationOpened += (s, p) =>
                {
                    foreach (var data in p.Data)
                    {
                        //output data to console
                    }
                };

            //Handle notification when app is closed here
            CrossFirebasePushNotification.Current.OnNotificationReceived +=
                (sender, payload) =>
                {
                   //code that logs having received and handled notification
                   //...
                };

...and FWIW, yes, I do have Parse's own push-receiving event handler set up, like this:

ParsePush.ParsePushNotificationReceived += (obj, args) =>
            {
                   //code that logs having received and handled notification
                   //...
            };

....so.... I know it's a murky murky stew there, but can anyone help point me in a direction that would tell me why the FirebasePushNotification Plugin isn't working all the time when apps are terminated on Android?

Developer technologies .NET Xamarin
{count} votes

1 answer

Sort by: Most helpful
  1. Yonglun Liu (Shanghai Wicresoft Co,.Ltd.) 50,126 Reputation points Microsoft External Staff
    2023-11-03T05:42:31.4733333+00:00

    Hello,

    I know it's a murky murky stew there, but can anyone help point me in a direction that would tell me why the FirebasePushNotification Plugin isn't working all the time when apps are terminated on Android?

    This is the recycling mechanism of the Android platform for services.

    For specific causes and solutions, please refer to Background Execution Limits in Android 8.0.

    Although this document is for Xamarin.Android, it still works with MAUI for Android-native mechanisms.

    Best Regards,

    Alec Liu.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.