Introduction
A notification service is the mechanism provided by android system to notify user about from background. Like you updating the applications through Google Play store, its show you message like downloading status or successful updation notification.
Using the code
Now to achieve this you have to follow these steps :-
- Get object of Notification system service using GetSystemService method which of type NotificationManager.
- Now create
Notification
object by Passing Application Icon (or any other icon you wish to show to user) and string message which you wish to seen by user like this :-
- Now create PendingIntent object, which will used by notification object to produce message notification. PendingIntent in brief "allows the foreign application to use your application's permissions to execute a predefined piece of code." Bold text is taken from StackOverflow website here
- Use SetLatestEventInfo method of notification object with message to be displayed, here second parameter correspond to Header Text and third parameter corresponding smaller text.
- Now using NotificationManager notify method display message to User, here is the code for same:-
var notificationMgr = GetSystemService (Context.NotificationService) as NotificationManager;
var notificationObj = new Notification (Resource.Drawable.Icon,
"Message From the CodeProject");
var pendingIntentObj =
PendingIntent.GetActivity(this,0,new Intent(this, typeof(MainActivity)),0);
notificationObj.SetLatestEventInfo (this,
"CodeProject Notification",
"Message From MonoAndroid",
pendingIntentObj);
notificationMgr.Notify (0, notificationObj);
You can clearly see the text "CodeProject Notification
" on header text and "Message From MonoAndroid"
as smaller text in the image shown below
Articles in this Series
Tips/Tricks in this Series