NSNotificationCenter / BroadcastReceiver In C# .NET

If you are an iOS or Android developer, you must have some experience in using NSNotificationCenter or BroadcastReceiver. But if you don't know what I'm talking about, don’t worry, I will explain what exactly these classes do and why and when we should use them.

What is NSNotificationCenter or BroadcastReceiver?

Apple said,

A notification dispatch mechanism that enables the broadcast of information to registered observers.

Google says,

Android apps can send or receive broadcast messages from the Android system and other Android apps, similar to the publish-subscribe design pattern. These broadcasts are sent when an event of interest occurs. For example, the Android system sends broadcasts when various system events occur, such as when the system boots up or the device starts charging. Apps can also send custom broadcasts, for example, to notify other apps of something that they might be interested in (for example, some new data has been downloaded).

How can I use these in C#?

It’s super easy. As Google said, this feature uses publish-subscribe design pattern. It means you subscribe an action with key and perform/notify this key for all subscribers who have subscribed on this key which means you can perform/notify some actions with key and pass some objects to do a specific action.

Could you explain with an example?


Think about an app you have which supports messaging or PushNotification. So, when a message is received, you should perform some actions for showing the message received or store/read some data from the database. You should show the message in the inbox, set badge number in Tabbar or Toolbar, store that message to the database, and reload your View etc.

So, you should use events or define some static classes and static events to handle all of these actions.

Take to the code

First, install NotificationCenter package from NuGet Package Manager or run this command in NuGet Package Manager console.

  1. PM> Install-Package NotificationCenter  
This library is PCL class and you can use it in .NET Core, Xamarin, and UWP projects.

Subscribe to an action

With this code, you can subscribe to an action with key.

  1. //In inbox page controller   
  2. NotificationCenter.Subscribe("MessageReceivedKey",SetBadge);  
  3. private void SetBadge()  
  4. {  
  5.     Debug.WriteLine("Great message received so increase inbox badge");  
  6. }  
  1. //In database class  
  2. NotificationCenter.Subscribe("MessageReceivedKey",StoreOnDatabase);  
  3. private void StoreOnDatabase(object message)  
  4. {  
  5.     Debug.WriteLine("Great message received so i should insert 'message' argument to database");  
  6. }  
Notify/Perform an Action

So now, when a message is received, it’s time to notify the subscribers of their actions like this code.
  1. // In message receiver event method   
  2. NotificationCenter.Notify(key: "MessageReceivedKey",data: MessageObject);  

Unsubscribe on key

If you have some unused key, it’s better to unsubscribe from it like this,

  1. NotificationCenter.Unsubscribe(key: "MessageReceivedKey");  

  1. NotificationCenter.UnsubscribeAll();   
Thanks for reading my article. I hope this library helps you to make a great app.

This library is opensource and is available on GitHub website.