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?
Yes!
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.
- PM> Install-Package NotificationCenter
Subscribe to an action
With this code, you can subscribe to an action with key.
- //In inbox page controller
- NotificationCenter.Subscribe("MessageReceivedKey",SetBadge);
- private void SetBadge()
- {
- Debug.WriteLine("Great message received so increase inbox badge");
- }
- //In database class
- NotificationCenter.Subscribe("MessageReceivedKey",StoreOnDatabase);
- private void StoreOnDatabase(object message)
- {
- Debug.WriteLine("Great message received so i should insert 'message' argument to database");
- }
So now, when a message is received, it’s time to notify the subscribers of their actions like this code.
- // In message receiver event method
- NotificationCenter.Notify(key: "MessageReceivedKey",data: MessageObject);
Unsubscribe on key
If you have some unused key, it’s better to unsubscribe from it like this,
- NotificationCenter.Unsubscribe(key: "MessageReceivedKey");
- NotificationCenter.UnsubscribeAll();
This library is opensource and is available on GitHub website.

Alaa MasalmehPosted Mar 12, 2018, 9:23 AM
Would you please apply Singleton pattern on NotificationCenter class and abstract it to interface for IoC?
Tridip BhattacharjeePosted Mar 9, 2018, 3:41 AM
Now i guess this library will broadcast in my application because i though it can communicate over the internet. anyways thanks for answering.
Tridip BhattacharjeePosted Mar 9, 2018, 3:30 AM
Suppose i have developed a windows application where i will use NotificationCenter lib where i will use key like demo. the same windows application one will run in india pc and another one run in london pc. so when app run in india pc will broadcast something then how the apps which is running in london pc will be notofied? there is no server so how one can send notification to other ? please share the info. thanks for your all reply.
Tridip BhattacharjeePosted Mar 9, 2018, 3:12 AM
I guess i can use this library to notify others when my data changes. if possible when i will send notification then i guess i will get the same notification....so how to know which client sending this notification ?
Tridip BhattacharjeePosted Mar 9, 2018, 3:11 AM
You did not mention how to acquire the key.....from where to acquire the key?
Tridip BhattacharjeePosted Mar 9, 2018, 3:10 AM
Can i used this library when working with dotnet v4.0 or v4.5 ?