You are currently viewing How to create Android local notification using Xamarin C#?
Xamarin

How to create Android local notification using Xamarin C#?

How to create Android local notification using Xamarin C#?

Are you looking for Xamarin C# example to display a local notification or simply notification in android? then you are on the right page.

Here is the sample code to display a local notification in Android using Xamarin C#.

[Activity(Label = "MyNotify", MainLauncher = true, Icon = "@drawable/Icon")]
	 public class MainActivity : AppCompatActivity
	 {
	 // Unique ID for our notification: 
	 static readonly int NOTIFICATION_ID = 100;
	 static readonly string CHANNEL_ID = "my_location_notification";
	 static readonly string CHANNEL_NAME = "Local Notification Channel";
	  static readonly string CHANNEL_DESCRIPTION = "Local Notification Channel Description";
	 
	 protected override void OnCreate(Bundle bundle)
	 {
	 base.OnCreate(bundle);
	 SetContentView(Resource.Layout.Main);
	 
	 CreateNotificationChannel();
	 //Key Value pair to send with pending activity 
IDictionary<int, string> dict = new Dictionary<int, string>();
dict.Add("Key1","One");
dict.Add("Key2","Two");
dict.Add("Key3","Three");
SendNotification("This is Title","This is body", dict);
	
	 }
	void CreateNotificationChannel()
        {
            if (Build.VERSION.SdkInt < BuildVersionCodes.O)
            {
                // Notification channels are new in API 26 (and not a part of the
                // support library). There is no need to create a notification
                // channel on older versions of Android.
                return;
            }

            var channel = new NotificationChannel(CHANNEL_ID,
                                                 CHANNEL_NAME,
                                                  NotificationImportance.Default)
            {

                Description = CHANNEL_DESCRIPTION 
            };

            var notificationManager = (NotificationManager)GetSystemService(Android.Content.Context.NotificationService);
            notificationManager.CreateNotificationChannel(channel);
        }

void SendNotification(string title,string messageBody, IDictionary<string, string> data)
        {
 // When the user clicks the notification, SecondActivity will start up.
            var intent = new Intent(this, typeof(SecondActivity));
            intent.AddFlags(ActivityFlags.ClearTop);
 // Pass some values to SecondActivity:
            foreach (var key in data.Keys)
            {
                intent.PutExtra(key, data[key]);
            }

            var pendingIntent = PendingIntent.GetActivity(this,
                                                          CHANNEL_ID,
                                                          intent,
                                                          PendingIntentFlags.OneShot);

            var notificationBuilder = new NotificationCompat.Builder(this,CHANNEL_NAME)
                                      .SetSmallIcon(Resource.Drawable.ic_small_icon)  // This is the icon to display
                                      .SetContentTitle(title) // Set the title
                                      .SetContentText(messageBody) // the message to display.
                                      .SetAutoCancel(true) // Dismiss the notification from the notification area when the user clicks on it
                                      .SetColor(Android.Support.V4.Content.ContextCompat.GetColor(this, Resource.Color.colorPrimary)) //Set color for icon and App name text
.SetLargeIcon(Android.Graphics.BitmapFactory.DecodeResource(Resources, Resource.Drawable.ic_large_icon)) //large icon to display
                                      .SetContentIntent(pendingIntent); // Start up this activity when the user clicks the intent.

            var notificationManager = NotificationManagerCompat.From(this);
            notificationManager.Notify(CHANNEL_ID, notificationBuilder.Build());
        }
	 }