Send notifications from your service

In this short recipe, you will learn how to send notifications from the services that we created earlier. Our objective will be to create an application where the call to an Android-bound service is done manually through a button. The notification will inform the user that the service is correctly bound, and the call can safely be made.

Getting ready

For this recipe, we will reuse the code from the recipe related to the bound service.

How to do it...

Let's take a look at these steps:

  1. Add a button containing the Make The Call label and an empty TextView to the MainActivity class. The Button.Click event will trigger a call to the MyBoundService service from the previous recipe and populate the TextView with the answer. The MainActivity class's OnCreate() method now reads as follows:
    protected override void OnCreate (Bundle bundle) {
      base.OnCreate (bundle);
    
      SetContentView (Resource.Layout.Main);
    
      Intent myBoundServiceIntent = new Intent("ca.services.MyBoundService");
      StartService (myBoundServiceIntent);
    
      cnx = new MyBoundServiceConnection (this);
      BindService (myBoundServiceIntent, cnx, Bind.AutoCreate);
    
      Button button = FindViewById<Button>(Resource.Id.myButton);
    
      button.Click += delegate {
        TextView tx = FindViewById<TextView>(Resource.Id.textView1);
        tx.Text = _myBinder.BoundService.SayHello();
      };
    }

    The application is as depicted in the following screenshot:

    How to do it...
  2. Remove the call to the consume method of the MainActivity class in the MyBoundServiceConnection.OnServiceConnected() method, and remove the consume from the MainActivity class. Consequently, the MyBoundServiceConnection.OnServiceConnected() method is as follows:
    public override IBinder OnBind (Intent intent) {
      Log.Debug ("MyBoundService", "NewClient");
    
      varn Mgr = (NotificationManager) GetSystemService(NotificationService);
    
      Notification.Builder builder = new Notification.Builder(this)
      .SetContentTitle ("MyBoundService is Ready")
      .SetTicker ("MyBoundService has something to say")
      .SetContentText ("You can now call MyBoundService")
      .SetSmallIcon (Resource.Drawable.Icon);
    
      nMgr.Notify (0, builder.Notification);
    
      return _myBinder;
    }
  3. Run your application.

    A few moments after the application first appears, you will receive a notification in the notification area, as shown in the following screenshot:

    How to do it...

    If the user opens the notification he/she can see it entirely:

    How to do it...
  4. Click on the Make The Call button:
    How to do it...

How it works...

The modifications performed in order to have the notification are all contained in the following code:

var nMgr = (NotificationManager) GetSystemService(NotificationService);

Notification.Builder builder = new Notification.Builder (this)
.SetContentTitle ("MyBoundService is Ready")
.SetTicker ("MyBoundService has something to say")
.SetContentText ("You can now call MyBoundService")
.SetSmallIcon (Resource.Drawable.Icon);

nMgr.Notify (0,builder.Notification);

In this code, we use a lot of new objects. The first one is the NotificationManager instance. As its name suggests, NotificationManager is a class that notifies the user in case events happen in the background, in other words, in services. Different types of notifications can be sent to the user, for example, an icon and text in the launch bar, turning the notification led, or more intrusively, firing up the back light, playing a sound or vibrating. The kind of notification will depend on the method invoked on the builder. Here, we used a simple notification in the notification area:

var notification = new Notification (Resource.Drawable.Icon,"MyBoundService is Ready")

The arguments for the notification constructor are the icon of the application Resource.Drawable.Icon, and the sentence "MyBoundService is Ready".

Then, we use a Notification.Builder instance:

Notification.Builder builder = new Notification.Builder (this)
.SetContentTitle ("MyBoundService is Ready")
.SetTicker ("MyBoundService has something to say")
.SetContentText ("You can now call MyBoundService")
.SetSmallIcon (Resource.Drawable.Icon);

The Notification.Builder is a helper class used to build the notification. Using this helper class, you can set the title, content, icons, and all the other ways of notifying a user you can imagine. Finally, we send the notification using the notification manager:

nMgr.Notify (0, builder.Build());

In the previous line, 0 is the tag of the notification that may be used later on.

See also

Take a look at the next section to see how to create a service that can periodically execute an action and notify the user.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
52.14.130.13