Create Local Notification In Xamarin.Android Using Visual Studio

Xamarin Introduction

Xamarin is the best cross platform tool to develop mobile applications. It provides cross platform app development in C#, so we don’t need to write Java or Objective C. We can just use C# and leverage the same on all the major platforms. Xamarin also helps designers by providing with the different platforms, like Android, iOS, etc.

Working with Xamarin application development, we have two gateways- Xamarin Studio and Visual Studio. Xamarin Studio supports for both Windows or Mac. Visual Studio supports only in Windows, provided you can build and debug on Mac.

Prerequisites

  • Visual Studio 2017 RC.

The following steps are to be foorder to create local Notification in Xamarin.Forms in VS 2017.

Step 1

Go to the Visual Studio, File >>New>> Project.

Click on C#>>select Cross Platform>> Then, select Cross Platform App(Xamarin.Forms.Portable).

Enter the application name and click OK.

Now, you can see the homepage of the project.

Step 2

Create a button. By clicking the notification, action will raise.

Xamarin

Step 3

Now, give action for the button click.

  1. Protected override void OnCreate(Bundle bundle) {  
  2.     Base.OnCreate(bundle);  
  3.     SetContentView(Resource.Layout.Main);  
  4.     Var btnSend = FindViewById < Button > (Resource.id.btnSend);  
  5.     btnSend.Click += (s, e) => {  
  6.         Bundle valuesSend = new Bundle();  
  7.         valuesSend.PutString(“sendContent”, ”This is content send from activity 1”);  
  8.     }  
  9. }  

Xamarin

Step 4

In this step, we are going create an activity which gets active when we click on the notification.

Xamarin

Step 5

In this step, we are writing the code to generate the notification when the button click event is raised.

  1. Intent newIntent = new Intent(thistypeof(Second_Activity));  
  2. newIntent.PutExtras(valuesSend) // we are giving which activity should raise when we click the notification  
  3. Android.Support.V4.App.TaskStackBuilder stackBuilder = Android.support.V4.App.TaskStackBuilder.Create(this);  
  4. stackBuilder.AddParentStack(Java.Lang.Class.FromType(typeof(Second_Activity)));  
  5. stackBuilder.AddNextIntent(newIntent);  
  6. PendingIntent resultPendingIntent = stackBuilder.GetPendingIntent(0, (int) PendingIntentFlags.UpdateCurrent);  
  7. NotificationCompat.Builder builder = new NotificationCompat.Builder(this);.SetAutoCancel(true).SetContentIntent(resultPendingIntent).SetContentTitle(“My Notifications”).SetSmallIcon(Resource.Drawable.Icon).SetContentText(“Click here to next Activity”);  
  8. NotificationManager notificationManager = (NotificationManager) GetSystemService(Context.NotificationService);  
  9. notificationManager.Notify(ButtonClickNotification, builder.Build());  

Please follow the screenshot.

Xamarin

Step 6

Here, we display the message which we send through the notification in the second_activity page to verify if the notification is really working or not.

Xamarin

Step 7

Run the app and click the “SEND NOTIFICATION” button. A new notification will be shown.

Xamarin

Xamarin

Xamarin

I hope this article is very useful.


Similar Articles