2011-11-25 9 views
11
int icon = R.drawable.icon4;   
CharSequence tickerText = "Hello"; // ticker-text 
long when = System.currentTimeMillis();   
Context context = getApplicationContext();  
CharSequence contentTitle = "Hello"; 
CharSequence contentText = "Hello";  
Intent notificationIntent = new Intent(this, Example.class); 
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0); 
Notification notification = new Notification(icon, tickerText, when); 
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent); 

To nie zadziała dla mnie.Jak tworzyć powiadomienia, które nie znikają po kliknięciu w systemie Android?

Jak mogę utworzyć powiadomienie, które można kliknąć i przejść do mojej aplikacji, ale nie znika po kliknięciu?

+0

podobne: http://stackoverflow.com/questions/6391870/how-exactly-to-use-notification-builder/35279147#35279147 –

Odpowiedz

25

Powinieneś przeczytać całość, a nie tylko część, kolego. Przeczytaj uważnie krok po kroku.

// this 
String ns = Context.NOTIFICATION_SERVICE; 
NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns); 

int icon = R.drawable.icon4;   
CharSequence tickerText = "Hello"; // ticker-text 
long when = System.currentTimeMillis();   
Context context = getApplicationContext();  
CharSequence contentTitle = "Hello"; 
CharSequence contentText = "Hello";  
Intent notificationIntent = new Intent(this, Example.class); 
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0); 
Notification notification = new Notification(icon, tickerText, when); 
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent); 

// and this 
private static final int HELLO_ID = 1; 
mNotificationManager.notify(HELLO_ID, notification); 
+6

'setLatestEventInfo()' została zaniechana poziomu API 11 Zamiast tego należy użyć narzędzia [Notification.Builder] (http://developer.android.com/reference/android/app/Notification.Builder.html). – AnujAroshA

+0

Używam tego samego kodu do powiadomienia .. Teraz, jeśli chcę dodać przycisk w powiadomieniu, to jak można osiągnąć ...? Proszę pomóż mi – ADT

+0

Mam problem z ustawieniem ikony na zielony kolor, ale kiedy pojawi się powiadomienie, automatycznie zmieni się na biały. –

12
int icon = R.drawable.ic_launcher; 
long when = System.currentTimeMillis(); 
NotificationManager nm=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE); 
Intent intent=new Intent(context,MainActivity.class); 
PendingIntent pending=PendingIntent.getActivity(context, 0, intent, 0); 
Notification notification; 
    if (Build.VERSION.SDK_INT < 11) { 
     notification = new Notification(icon, "Title", when); 
     notification.setLatestEventInfo(
       context, 
       "Title", 
       "Text", 
       pending); 
    } else { 
     notification = new Notification.Builder(context) 
       .setContentTitle("Title") 
       .setContentText(
         "Text").setSmallIcon(R.drawable.ic_launcher) 
       .setContentIntent(pending).setWhen(when).setAutoCancel(true) 
       .build(); 
    } 
notification.flags |= Notification.FLAG_AUTO_CANCEL; 
notification.defaults |= Notification.DEFAULT_SOUND; 
nm.notify(0, notification); 

lub można pobrać bezpośrednio z samouczka tutaj: http://www.demoadda.com/demo/android/how-to-create-local-notification-notification-manager-demo-with-example-android-source-code_26

+1

Czy to prawda? Tworzysz 'PendingIntent', ale nie jest on wówczas używany. Przynajmniej gdy <11, powinien być ostatnim parametrem dla 'setLatestEventInfo', czy też się mylę? – hgoebl

+0

@hgoebl: zobacz moją edycję .. – Kishan

+0

Mam problem z ustawieniem ikony na zielony kolor, ale gdy pojawi się powiadomienie, automatycznie zmieni się na biały. –

2

Jeśli używasz Androida 5.0> stało się o wiele łatwiejsze, funkcjonalność zmieniło, ale można użyć tego samego kodu.

//Some Vars 
public static final int NOTIFICATION_ID = 1; //this can be any int 


//Building the Notification 
NotificationCompat.Builder builder = new NotificationCompat.Builder(this); 
builder.setSmallIcon(R.drawable.ic_stat_notification); 
builder.setContentTitle("BasicNotifications Sample"); 
builder.setContentText("Time to learn about notifications!"); 

NotificationManager notificationManager = (NotificationManager) getSystemService(
      NOTIFICATION_SERVICE); 
notificationManager.notify(NOTIFICATION_ID, builder.build()); 

Upewnij się, że w kontekście aplikacji, jeśli nie można zdać kontekst i zmienić źródłowego następująco

NotificationCompat.Builder builder = new NotificationCompat.Builder(context); 
... 
.. 
. 

NotificationManager notificationManager = (NotificationManager) context.getSystemService(
      context.NOTIFICATION_SERVICE); 

Można zobaczyć pełny kod źródłowy pod adresem: https://github.com/googlesamples/android-BasicNotifications/blob/master/Application/src/main/java/com/example/android/basicnotifications/MainActivity.java#L73

+0

Mam problem z ustawieniem ikony na zielony kolor, ale po pojawieniu się powiadomienia zmienia się automatycznie na biały. –

0
if (android.os.Build.VERSION.SDK_INT>16) 
    { 
     notificationManager.notify(5, notification.build()); 
    }else 
    { 
     notificationManager.notify(5, notification.getNotification()); 
    } 

aby pracować w android.os.Build.VERSION.SDK_INT<16 pamiętać, aby zrobić kilka zmian

1

Oto kod

Intent intent = new Intent(this, MainActivity.class); 
     intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
     PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 
       PendingIntent.FLAG_ONE_SHOT); 

     Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 
     NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this) 
       .setSmallIcon(R.mipmap.ic_launcher) 
       .setContentTitle("Push Notification") 
       .setContentText(messageBody) 
       .setAutoCancel(true) 
       .setSound(defaultSoundUri) 
       .setContentIntent(pendingIntent); 

     NotificationManager notificationManager = 
       (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 

     notificationManager.notify(0, notificationBuilder.build()); 

tutaj, jeśli chcesz powiadomienia, które nie znika po kliknięciu w Androidzie? tak ustawione

setAutoCancel(false); 
Powiązane problemy