7

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?

Michael Kniskern
  • 24,792
  • 68
  • 164
  • 231
  • doesn't setcontenttitle do that? – njzk2 Sep 08 '14 at 23:07
  • possible duplicate of [How to implement the deprecated methods of Notification](http://stackoverflow.com/questions/16852270/how-to-implement-the-deprecated-methods-of-notification) – njzk2 Sep 08 '14 at 23:09
  • (different language, but same api, so possible duplicate?) – njzk2 Sep 08 '14 at 23:10
  • I answered this question here: [Link](https://stackoverflow.com/questions/45376300/how-do-i-show-text-in-android-system-status-bar/65155581#65155581) – Shoaib Kakal Dec 05 '20 at 09:41

2 Answers2

21

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

gprathour
  • 14,813
  • 5
  • 66
  • 90
Michael Kniskern
  • 24,792
  • 68
  • 164
  • 231
  • This should be the accepted answer! The answers in other question say you should give a smallIcon, but they are useless. – Ninja Mar 17 '17 at 02:52
2

Below code worked for me. Just a few corrections in the second snippet.

Intent resultIntent = new Intent(this, TestService.class); 

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

    NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext())
        .setContentIntent(pi) 
        .setAutoCancel(true) 
        .setContentTitle("title")
        .setSmallIcon(R.drawable.ic_notif) //add a 24x24 image to the drawable folder
                                           // and call it here 
        .setContentText("desc"); 

    NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    nm.notify(0, builder.build());
Ganesh Subramanian
  • 193
  • 1
  • 2
  • 9