TobiasBuchholz / Plugin.Firebase

Wrapper around the native Android and iOS Firebase Xamarin SDKs
MIT License
210 stars 48 forks source link

Purple screen when notification tapped while app in the background #264

Open nine-2-five opened 7 months ago

nine-2-five commented 7 months ago

Hi,

I managed implementing the Plugin.Firebase.CloudMessaging with NET MAUI Blazor, and the notifications are arriving by the device token. However I have a bug where app in the background upon MainActivity.OnCreate and HandleIntent, the Current_NotificationTapped gets handled, and the app displays with just the purple background (not even the ".NET" logo). I should note that it only PSOD when the MainPage.xml contains BlazorWebView.

I'm using: Plugin.Firebase.Analytics v2.0.2 Plugin.Firebase.CloudMessaging v2.0.4

This is my MainActivity file:

using Android.Content;
using Android.Content.PM;
using Android.OS;
using Android.Runtime;

using Microsoft.Extensions.Configuration;
using Plugin.Firebase.CloudMessaging;

namespace MauiSample;
[Activity(Theme = "@style/Maui.SplashTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation | ConfigChanges.UiMode | ConfigChanges.ScreenLayout | ConfigChanges.SmallestScreenSize | ConfigChanges.Density)]
public class MainActivity : MauiAppCompatActivity
{
    public static bool AppCenterConfigured { get; set; }

    protected override void OnCreate(Bundle? savedInstanceState)
    {
        try
        {
            var intent = this.Intent;

            //var config = MauiProgram.GetService<IConfiguration>();
            //var secret = config.GetSection("AppCenter:Secret")?.Value;
            //if (!string.IsNullOrEmpty(secret))
            //{
            //    if (!AppCenterConfigured)
            //    {
            //        AppCenter.Configure(secret);
            //        AppCenter.Start(typeof(Crashes), typeof(Analytics));
            //    }
            //}

            base.OnCreate(savedInstanceState);

            HandleIntent(Intent);

            CreateNotificationChannelIfNeeded();

        }

        catch (Exception e)
        {
            Console.WriteLine(e.ToString());
        }
    }

    protected override void OnNewIntent(Intent? intent)
    {
        try
        {
            base.OnNewIntent(intent);
            HandleIntent(intent);
        }
        catch (Exception e)
        {
            Console.WriteLine(e.ToString());
        }
    }

    private static void HandleIntent(Intent? intent)
    {
        try
        {
            if (intent is not null)
            {
                FirebaseCloudMessagingImplementation.OnNewIntent(intent);
            }
        }
        catch (Exception e)
        {
            Console.WriteLine(e.ToString());
        }
    }

    public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Permission[] grantResults)
    {
        try
        {
            Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);
#if ANDROID23_0_OR_GREATER
#pragma warning disable CA1416 // Validate platform compatibility
            base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
#pragma warning restore CA1416 // Validate platform compatibility
#endif
        }
        catch (Exception e)
        {
            Console.WriteLine(e.ToString());
        }
    }

    private void CreateNotificationChannelIfNeeded()
    {
        try
        {
            if (Build.VERSION.SdkInt >= BuildVersionCodes.O)
            {
                CreateNotificationChannel();
            }
        }
        catch (Exception e)
        {
            Console.WriteLine(e.ToString());
        }
    }

    private void CreateNotificationChannel()
    {
        try
        {
            var channelId = $"{PackageName}.general";
#if ANDROID26_0_OR_GREATER
            var notificationManager = GetSystemService(NotificationService) as NotificationManager;
            if (notificationManager is not null)
            {
                if ((int)Build.VERSION.SdkInt > 26)
                {
#pragma warning disable CA1416 // Validate platform compatibility
                    var channel = new NotificationChannel(channelId, "General", NotificationImportance.Default);
                    notificationManager.CreateNotificationChannel(channel);
#pragma warning restore CA1416 // Validate platform compatibility
                }
            }

            FirebaseCloudMessagingImplementation.ChannelId = channelId;
#endif
            //FirebaseCloudMessagingImplementation.SmallIconRef = Resource.Drawable.ic_push_small;
        }
        catch(Exception e)
        {
            Console.WriteLine(e.ToString());
        }

    }
}

This is my AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.myapp.myapp" android:versionCode="1">
    <application android:allowBackup="true" android:supportsRtl="true" android:networkSecurityConfig="@xml/network_security_config" android:label="myapp">
        <meta-data android:name="google_analytics_default_allow_analytics_storage" android:value="true" />
        <receiver android:name="com.google.firebase.iid.FirebaseInstanceIdInternalReceiver" android:exported="false" />
        <receiver android:name="com.google.firebase.iid.FirebaseInstanceIdReceiver" android:exported="true" android:permission="com.google.android.c2dm.permission.SEND">
            <intent-filter>
                <action android:name="com.google.android.c2dm.intent.RECEIVE" />
                <action android:name="com.google.android.c2dm.intent.REGISTRATION" />
                <category android:name="com.myapp.myapp" />
            </intent-filter>
        </receiver>
        <!-- Other application components like activities -->
    </application>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
    <!-- <uses-sdk android:minSdkVersion="21" android:targetSdkVersion="34" /> -->
    <uses-permission android:name="com.google.android.gms.permission.AD_ID" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-sdk />
</manifest>
mrnams commented 6 months ago

I have same issue

Zonorai commented 5 months ago

I modified my main activity attribute to look like this [Activity(Theme = "@style/Maui.SplashTheme", MainLauncher = true, ClearTaskOnLaunch = true, LaunchMode = LaunchMode.SingleTask, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation | ConfigChanges.UiMode | ConfigChanges.ScreenLayout | ConfigChanges.SmallestScreenSize | ConfigChanges.Density)]

And it resolved this for me

I think this might be related to this dotnet 8 issue: .NET MAUI Stuck on splash screen when reopen the app after start a foreground service