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

Daniel Kelly 21 Reputation points
2021-10-19T19:26:52.27+00:00

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.

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,292 questions
0 comments No comments
{count} votes

Accepted answer
  1. JarvanZhang 23,936 Reputation points
    2021-10-20T07:24:19.093+00:00

    Hello @Daniel Kelly ,​

    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.


0 additional answers

Sort by: Most helpful