【问题标题】:Display Notification Text in Status Bar - Android在状态栏中显示通知文本 - Android
【发布时间】:2014-09-08 23:04:26
【问题描述】:

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

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)' 已过时:'deprecated'

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

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

我需要在新代码sn-p中设置什么才能在android设备状态栏中显示内容标题?

【问题讨论】:

标签: android android-notifications google-cloud-messaging


【解决方案1】:

我需要在第二个代码 sn-p 中添加.setTicker() 以在 Android 设备状态栏中显示文本

【讨论】:

  • 这应该是公认的答案!其他问题的答案说你应该给一个小图标,但它们没用。
【解决方案2】:

以下代码对我有用。只是在第二个 sn-p 中进行了一些更正。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多