dotnet / maui

.NET MAUI is the .NET Multi-platform App UI, a framework for building native device applications spanning mobile, tablet, and desktop.
https://dot.net/maui
MIT License
21.94k stars 1.7k forks source link

When a FCM notification is received, the application activates the "App Class" even when the application is closed #19030

Open alzubitariq opened 9 months ago

alzubitariq commented 9 months ago

Description

When a FCM notification is received, the application activates the "App Class" even when the application is closed So

public App()
{
     InitializeComponent();
     MainPage = new NavigationPage(new ....());

     **///will be called and so on ...** 
}

how do I discovered this issue , I was receiving extra log on my server and my phone Samsung telling me always the app is crashed

FCM Service sam1

Main Activity sam2

Steps to Reproduce

No response

Link to public reproduction project repository

No response

Version with bug

8.0.3

Is this a regression from previous behavior?

No, this is something new

Last version that worked well

Unknown/Other

Affected platforms

Android

Affected platform versions

tested on android 12 and 13

Did you find any workaround?

No response

Relevant log output

No response

alzubitariq commented 9 months ago

Same issue exist in .NET MAUI 7 AND 8

drasticactions commented 8 months ago

I don't know much about Firebase, but this doesn't seem like a bug, nor related to the MAUI UI Framework. It seems like when you get a notification, your application activity is starting up. So you would need to handle this situation to respond to the service notification and not fully start up the application.

If you create a new .NET Android application (dotnet new android) that doesn't implement the MAUI UI framework, and implement the same Firebase service, does the same behavior occur? My guess is that it will.

ghost commented 8 months ago

Hi @alzubitariq. We have added the "s/needs-info" label to this issue, which indicates that we have an open question for you before we can take further action. This issue will be closed automatically in 7 days if we do not hear back from you by then - please feel free to re-open it if you come back to this issue after that time.

alzubitariq commented 8 months ago

@drasticactions

It seems like when you get a notification, your application activity is starting up.

I don't think so , Maybe it is about MainApplication class and OnCreate method which is called when the application is starting before any activity or service

but this doesn't seem like a bug, nor related to the MAUI UI Framework

If it's not a bug. So what is it? feature !!

If you create a new .NET Android application

I'm talking about .NET MAUI not .NET Android

I was very clear

1- FirebaseMessagingService OnMessageReceived When a notification arrives, this function (CreateMauiApp) is called when the application is closed, why? How is it called? MauiProgram.CreateMauiApp();

2- FirebaseMessagingService OnDeletedMessages When the notification is removed from the top menu, this function (CreateMauiApp) is called when the application is closed. Why? How is it called? MauiProgram.CreateMauiApp();

and this Function lives inside

  [Application]
    public class MainApplication : MauiApplication
    {
        public MainApplication(IntPtr handle, JniHandleOwnership ownership)
            : base(handle, ownership)
        {
        }

        protected override MauiApp CreateMauiApp() => MauiProgram.CreateMauiApp();
    }

Comparing to Xamarin Forms it was not there

@jfversluis Could you please take a look

jfversluis commented 8 months ago

I'm talking about .NET MAUI not .NET Android

We understand you're talking about .NET MAUI, however what is being asked here is that you try to make a small sample with .NET for Android, without .NET MAUI and see if the behavior is the same or different there. This will help you, and us, better understand where the root cause of this potential issue might be. If you could still do this and let us know the result, that would be amazing.

I was very clear

Remember that we do not have the context that you have. We did not see your debugging session, nor do we have access to your code. We're trying to understand, we're doing our best!

If it's not a bug. So what is it? feature !!

No, this seems to be just the way how lifecycles work in Android. As mentioned in the docs, when your notification has a payload, which I assume it has, then it will be delivered to the Activity, hence it has to create that activity, but it seems like according to you something goes wrong there.

how do I discovered this issue , I was receiving extra log on my server and my phone Samsung telling me always the app is crashed

Do you have those logs for us? What is the error message inside of it? Are you using any extra library to implement push notifications? You mention a Samsung phone, does it also seem to happen on other phones? Can you maybe provide us with a reproduction sample for a better overview of what is going on in your code so we can have a look?

ghost commented 8 months ago

Hi @alzubitariq. We have added the "s/needs-info" label to this issue, which indicates that we have an open question for you before we can take further action. This issue will be closed automatically in 7 days if we do not hear back from you by then - please feel free to re-open it if you come back to this issue after that time.

alzubitariq commented 8 months ago

@jfversluis

Do you have those logs for us?

No

What is the error message inside of it?

I don't know

Are you using any extra library to implement push notifications?

No , Only Xamarin.Firebase.Messaging package

You mention a Samsung phone, does it also seem to happen on other phones?

I don't know

Can you maybe provide us with a reproduction sample for a better overview of what is going on in your code so we can have a look?

it is an new Maui 8 app with Xamarin.Firebase.Messaging debug and release ,,, remember when the app is closed but I will prepare a sample

after receiving up to two notifications this message will come

Untitled

alzubitariq commented 8 months ago

How can we describe the application as live if the notifications feature does not work properly

alzubitariq commented 8 months ago

@jfversluis

how do I discovered this issue , I was receiving extra log on my server and my phone Samsung telling me always the app is crashed

I mean different log

Like I'm sending an HttpRequest after calling MainPage = new NavigationPage(new ....()); inside App Contractor So when the notification arrives to phone , App Contractor is called and the crash maybe happens there

jfversluis commented 8 months ago

Without the additional information and/or reproduction sample its near impossible to tell what is going wrong here. And because of that, it's also not possible to determine whether this is a bug in .NET MAUI or your own application code.

Looking forward to the reproduction sample so we can investigate further.

ghost commented 8 months ago

Hi @alzubitariq. We have added the "s/needs-repro" label to this issue, which indicates that we require steps and sample code to reproduce the issue before we can take further action. Please try to create a minimal sample project/solution or code samples which reproduce the issue, ideally as a GitHub repo that we can clone. See more details about creating repros here: https://github.com/dotnet/maui/blob/main/.github/repro.md

This issue will be closed automatically in 7 days if we do not hear back from you by then - please feel free to re-open it if you come back to this issue after that time.

alzubitariq commented 8 months ago

@jfversluis Hello again,

Here you can find the solution file https://download.zuzmu.com/MauiAppFirebase.zip

Here you can find the description video https://download.zuzmu.com/Video_2023-11-27_221302.mp4 or https://download.zuzmu.com/Video_2023-11-27_221302.gif

Here you can find the api to test the notification https://app.zuzmu.com/swagger/index.html

alzubitariq commented 8 months ago

I think things are executed in the following order when notifications are received

I/MyApp: Constructor which lives inside MainApplication : MauiApplication I/MyApp: AttachBaseContext which lives inside MainApplication : MauiApplication I/MyContentProvider: Constructor (Firebase service) I/MyContentProvider: OnCreate (Firebase service) I/MyApp: OnCreate: which lives inside MainApplication : MauiApplication I/MyApp: CreateMauiApp : which lives inside MauiApplication and called from OnCreate

alzubitariq commented 8 months ago

Any updates or workaround?

alzubitariq commented 8 months ago

Any updates or workaround?

AmrAlSayed0 commented 8 months ago

@alzubitariq

This is expected. Xamarin.Form's App class was initialized from the MainActivity not the MainApplication. When a notification was received in the background the MainApplication would be called but not the MainActivity or Xamarin.Form's App class. If the user then maybe taps the notification this would then create a MainActivity that would start the application.

In MAUI the App class is tied to the lifetime of the app including when it's started in the background by a service. What your doing in the first code snippet shows you're initializing the UI with the app itself which isn't always what you want. Try moving MainPage = new NavigationPage(new ....()); and the following dependant code out of the constructor and maybe into Application.CreateWindow https://github.com/dotnet/maui/blob/d21fae3591b9ea1f961f693008d3d36fe06cc755/src/Controls/src/Core/Application/Application.cs#L496 or Application.OpenWindow https://github.com/dotnet/maui/blob/d21fae3591b9ea1f961f693008d3d36fe06cc755/src/Controls/src/Core/Application/Application.cs#L473 overrides, I'm not sure which. May be one of the MAUI team can help with this.

alzubitariq commented 8 months ago

thanks @AmrAlSayed0 it works , but I think the memory leak still there and the objects that created from MauiProgram.CreateMauiApp() live forever

 protected override Window CreateWindow(IActivationState? activationState)
 {
     MainPage = new MainPage();
     return base.CreateWindow(activationState);
 }
alzubitariq commented 6 months ago

Any news ?

ninachen03 commented 1 month ago

@alzubitariq The sample seems to have been deleted, Could you provide a new sample to reproduce the memory leak problem?