Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display Notification Text in Status Bar - Android

In my application I need to display notification to the user. The following code snippet worked great by display the icon and content title in the Android device title bar.

var notificationManager = GetSystemService(Context.NotificationService) as NotificationManager;
var uiIntent = new Intent(this, typeof(MainActivity));
var notification = new Notification(Resource.Drawable.AppIcon, title);
notification.Flags = NotificationFlags.AutoCancel;
notification.SetLatestEventInfo(this, title, desc, PendingIntent.GetActivity(this, 0, uiIntent, 0));
notificationManager.Notify(1, notification);

When I tried to build the package for deploying the application, I get the following error:

Android.App.Notification.SetLatestEventInfo(Android.Content.Context, string, string, Android.App.PendingIntent)' is obsolete: 'deprecated'

So I found this code snippet I should be using and it shows the icon in the status bar by not the content title

Intent resultIntent = new Intent(this, typeof(MainActivity));

PendingIntent pi = PendingIntent.GetActivity(this, 0, resultIntent, 0);

NotificationCompat.Builder builder = new NotificationCompat.Builder(Forms.Context)
    .SetContentIntent(pi) 
    .SetAutoCancel(true) 
    .SetContentTitle(title)
    .SetSmallIcon(Resource.Drawable.AppIcon)
    .SetContentText(desc); //This is the icon to display

NotificationManager nm = GetSystemService(Context.NotificationService) as NotificationManager;
nm.Notify(_TipOfTheDayNotificationId, builder.Build());

What do I need to set in the new code snippet to display the content title in the android device status bar?

like image 651
Michael Kniskern Avatar asked Sep 08 '14 23:09

Michael Kniskern


People also ask

How do I show progress notifications on Android?

To display a determinate progress bar, add the bar to your notification by calling setProgress(max, progress, false) and then issue the notification. The third argument is a boolean that indicates whether the progress bar is indeterminate (true) or determinate (false).

What is a status bar notification?

Status bar (or notification bar) is an interface element at the top of the screen on Android devices that displays the notification icons, minimized notifications, battery information, device time, and other system status details.


1 Answers

I need to add .setTicker() to the second code snippet to have text display in the Android device status bar

like image 182
Michael Kniskern Avatar answered Oct 20 '22 17:10

Michael Kniskern