Ongoing notifications

Some notifications are used to display a task that is underway and may even include information on progress or state.

Getting ready

If we are going to support Android versions prior to 3.0, we will have to install the Xamarin Support Library v4 NuGet or component into our project.

How to do it...

To show progress in a notification, we can use a progress bar and update the notification as progress is made:

  1. Most ongoing notifications are not going to be dismissed when tapped, so instead of using SetAutoCancel() method, we use SetOngoing() method:
    var notification = new NotificationCompat.Builder(this)
      .SetSmallIcon(Android.Resource.Drawable.StatNotifySync)
      .SetContentTitle("Simple Notification")
      .SetContentText("Starting Work!")
      .SetOngoing(true);
  2. We can also have a progress bar in the notification. This could be indeterminate:
    notification.SetProgress(0, 0, true);
  3. Or the progress bar could display a specific value:
    notification.SetProgress(100, 27, false);
  4. We can update notifications simply by notifying the NotificationManager instance with the same notification ID:
    manager.Notify(notificationId, notification.Build());
  5. If, for some reason, we have to cancel the notification and remove it from the status bar, we use the Cancel() method with the notification ID:
    manager.Cancel(notificationId);

How it works...

Because tasks take some time to complete, we may want to report the progress or the state of the task. Notifications provide a few means of showing such progress. We could use a normal text message to display the state, or we could use a progress bar to show the progress.

To display a progress bar, we use the SetProgress() method. The progress bar can either have a specific progress or be indeterminate. For a specific progress, we specify the maximum and progress values along with false. For indeterminate progress, we pass 0 to both the maximum and progress parameters, but true for indeterminate.

Tip

A progress bar could be indeterminate, such as before real progress is made, or have a specific value.

To prevent the notification from being dismissed by the user, we do not specify the SetAutoCancel() method as true, but rather the SetOngoing() method. This will prevent the notification from being dismissed by the user, even when they swipe the notification out of the notification drawer.

Tip

The notification can be prevented from being dismissed, even manually, by passing true to the SetOngoing() method.

To update the notification once displayed, we simply have to invoke the Notify() method again. We must pass the same notification ID; otherwise, a new notification will be created. Every time the notification changes, we invoke the Notify() method.

Note

The same ID must be used for updating notifications; otherwise, a new notification will be created.

Once the task has completed, we can update the notification with a dismissible notification, or we can manually remove the notification from the notification drawer. Cancelation is done by invoking the Cancel() method on the NotificationManager instance, providing the ID of the notification that we want to cancel.

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

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