question

DanielKelly-6507 avatar image
0 Votes"
DanielKelly-6507 asked JarvanZhang-MSFT commented

Messaging Center - Correct way to Subscribe / Unsubscribe in App Class

Hi,

I was hoping someone could provide some feedback or advice in stopping Messaging Center from sending multiple messages.

I understand why it is happening, but cant find the way to get around it.

My Xamarin Forms Application has an attached foreground service and I subscribe in the App.xaml.cs (constuctor) file like follows :-

 MessagingCenter.Subscribe<string>(this, MessageKeys.TestCommand.ToString(), (args) => {
    
                 ProcessTestCommand(args);
    
  });

To unsubscribe i use the following :-

 MessagingCenter.Unsubscribe<string>(this, MessageKeys.TestCommand);

I can make it increment the Message Subscription by doing the following :-

1) Hit the android home button and then swipe off the activity
2) My service is still running (which I need) and I can either restart the app from the launcher or press the foreground service notification

When the unsubscribe happens (called before subscribe), the subscription is not removed which I am assuming because "this" is no longer the current instance of the app class but it never seems to get garbage collected. So if I perform the above steps 5 times, debugging the code the "ProcessTestCommand" method is called 5 times.

Most of the examples I have read, all seem to be attached to the Appearing on removed on the disappearing event, but my design needs it to be in the App class.

Has anyone has any similar issues? Or has any feedback to offer?

Thanks,
Daniel.


dotnet-xamarin
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

JarvanZhang-MSFT avatar image
0 Votes"
JarvanZhang-MSFT answered JarvanZhang-MSFT commented

Hello @DanielKelly-6507 ,​

Welcome to our Microsoft Q&A platform!

the subscription is not removed which I am assuming because "this" is no longer the current instance of the app class

To avoid this, please call the unsubscribe command before the app is closed. Try creating a global static instance of the App class so that we can get the App instance to execute the unsubscribe method in other classes.

public partial class App : Application
{
    public static App Instance;
    public App()
    {
        InitializeComponent();
        Instance = this;
        MainPage = new NavigationPage(new TestPage());

        MessagingCenter.Subscribe<TestPage>(this, "hi", (args) => {
            //
        });
    }
}

Then detect the application finished lifecycle method in the native platform. For example, detect the OnDestory method on Android.

public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
    protected override void OnDestroy()
    {
        base.OnDestroy();
        MessagingCenter.Unsubscribe<TestPage>(App.Instance, "hi");
    }
}


Best Regards,

Jarvan Zhang


If the response is helpful, 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.


· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi, @DanielKelly-6507
May I know if you have got any chance to check my answer? I am glad to help if you have any other questions.

0 Votes 0 ·

Thanks for the tip. Works perfectly.

0 Votes 0 ·

You're welcome.

0 Votes 0 ·