2014-09-08 23 views
6

Uygulamamda kullanıcıya bildirim göstermem gerekiyor. Aşağıdaki kod snippet'i, Android cihaz başlık çubuğundaki simge ve içerik başlığını göstererek harika çalıştı.Durum Çubuğunda Bildirim Metnini Görüntüleme - 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); 

Uygulamayı dağıtmak için paket oluşturmaya çalıştığımda, şu hatayı alıyorum:

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

Ben de kullanıyor olmalıdır bu kod parçacığını buldum ve statü simgesini gösterir

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

ne android cihaz durum çubuğunda içerik başlığı görüntülemek için yeni kod parçacığının oluşturmam gerekiyor değil içerik başlığa göre bar?

+0

bunu yapamaz mı? – njzk2

+0

Hayır, öyle değil. –

+0

Mümkün Bildirimlerin nasıl uygulanması (http://stackoverflow.com/questions/16852270/how-to-implement-the-deprecated-methods-of-notification) – njzk2

cevap

19

Ben Kodun altında

+0

Bu kabul edilen cevap olmalı! Diğer sorudaki cevaplar küçük bir fikir vermeniz gerektiğini söylüyor, ama işe yaramıyorlar. – Ninja

1

benim için çalıştı Android cihazı durum çubuğunda metin ekranı olması ikinci kod parçalarında .setTicker() eklemeniz gerekir. İkinci snippet'te sadece birkaç düzeltme.

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()); 
İlgili konular