Display notification text in status bar - Android

In my application, I need to display a notification to the user. The following code snippet worked perfectly, displaying the icon and content title in the title bar of the Android device.

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 create a package for deploying the application, I get the following error:

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

So, I found this piece of code that I should use, and it shows the icon in the status bar not the content header

 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 install in a new code snippet to display the content header in the status bar of an Android device?

+6
source share
2 answers

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

+19
source

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

 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()); 
+1
source

Source: https://habr.com/ru/post/975003/


All Articles