在状态栏中显示通知文本

编程入门 行业动态 更新时间:2024-10-24 20:18:09
本文介绍了在状态栏中显示通知文本 - Android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在我的应用程序中,我需要向用户显示通知。通过在Android设备标题栏中显示图标和内容标题,下面的代码片段效果很好。

$ p $ var notificationManager = GetSystemService(Context .NotificationService)作为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,通知);

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

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

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

意图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); //这是显示 的图标NotificationManager nm = GetSystemService(Context.NotificationService)作为NotificationManager; nm.Notify(_TipOfTheDayNotificationId,builder.Build());

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

解决方案

我需要将 .setTicker()添加到第二个代码片段在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?

解决方案

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

更多推荐

在状态栏中显示通知文本

本文发布于:2023-11-27 12:09:33,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1638014.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:栏中   文本   状态   通知

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!