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

标签 android android-notifications google-cloud-messaging

在我的应用程序中,我需要向用户显示通知。以下代码 fragment 非常适合在 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)' is obsolete: 'deprecated'

所以我找到了我应该使用的代码 fragment ,它在状态栏中显示图标而不是内容标题

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() 添加到第二个代码 fragment ,以便在 Android 设备状态栏中显示文本

关于android - 在状态栏中显示通知文本 - Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25734370/

相关文章:

java - ReadObject 返回 OptionalDataException

android - Flutter:如何构建改变移动设备方向时移动 block 的逻辑?

Android 解析推送通知在 Marshmallow 中不显示图标

java - fragment 和通知 : Target different Activities from Notification; depending on screen configuration

php - 使用 php 向 Google Cloud Messaging 发送通知给我未经授权的错误 401

android:如何选择联系人电话号码

javascript - 如何在android中检测webview或浏览器

java - Android 颜色通知图标

java - 使用 regID 创建 mysql 数据库

google-cloud-messaging - 从 Google 项目 ID 获取 Google 帐户所有者