在状态栏中显示通知文本 - Android

2023-12-28

在我的应用程序中,我需要向用户显示通知。以下代码片段非常有效,可以在 Android 设备标题栏中显示图标和内容标题。

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);

当我尝试构建用于部署应用程序的包时,出现以下错误:

Android.App.Notification.SetLatestEventInfo(Android.Content.Context, string, string, Android.App.PendingIntent)' 已过时:'已弃用'

所以我发现我应该使用这个代码片段,它在状态栏中显示图标而不是内容标题

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());

我需要在新代码片段中设置什么才能在 Android 设备状态栏中显示内容标题?


我需要添加.setTicker()到第二个代码片段,在 Android 设备状态栏中显示文本

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在状态栏中显示通知文本 - Android 的相关文章

随机推荐