2014-09-08 18 views
6

Trong ứng dụng của tôi, tôi cần hiển thị thông báo cho người dùng. Đoạn mã sau hoạt động tốt bằng cách hiển thị tiêu đề biểu tượng và nội dung trong thanh tiêu đề thiết bị Android.Hiển thị văn bản thông báo trong thanh trạng thái - 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); 

Khi tôi cố gắng xây dựng gói cho việc triển khai ứng dụng, tôi nhận được lỗi sau:

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

Vì vậy, tôi tìm thấy đoạn mã này, tôi nên sử dụng và nó cho thấy các biểu tượng trong tình trạng thanh không phải là tiêu đề nội dung

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

Tôi cần phải đặt gì trong đoạn mã mới để hiển thị tiêu đề nội dung trên thanh trạng thái thiết bị Android?

+0

không setcontenttitle làm điều đó? – njzk2

+0

Không, nó không. –

+0

bản sao có thể có của [Cách triển khai các phương pháp thông báo không được chấp nhận] (http://stackoverflow.com/questions/16852270/how-to-implement-the-deprecated-methods-of-notification) – njzk2

Trả lời

19

tôi cần phải thêm .setTicker() để đoạn mã thứ hai để có hiển thị văn bản trong thanh trạng thái thiết bị Android

+0

Đây phải là câu trả lời được chấp nhận! Các câu trả lời trong câu hỏi khác nói rằng bạn nên cung cấp cho một smallIcon, nhưng họ là vô ích. – Ninja

1

Dưới đây đang làm việc cho tôi. Chỉ cần một vài sửa chữa trong đoạn thứ hai.

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()); 
Các vấn đề liên quan