2011-01-31 39 views

Respuesta

14

crear una notificación de la barra de estado, hacer esto en su método onCreate:

  1. obtener una referencia a la NotificationManager:

    String ns = Context.NOTIFICATION_SERVICE; 
        NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns); 
    
  2. una instancia de la Notificación:

    int icon = R.drawable.notification_icon; 
        CharSequence tickerText = "Hello"; 
        long when = System.currentTimeMillis(); 
    
        Notification notification = new Notification(icon, tickerText, when); 
    
  3. Definir el mensaje expandido de la notificación y la intención :

    Context context = getApplicationContext(); 
        CharSequence contentTitle = "My notification"; 
        CharSequence contentText = "Hello World!"; 
        Intent notificationIntent = new Intent(this, MyClass.class); 
        PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0); 
    
        notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent); 
    
  4. Pasar la notificación a la NotificationManager:

    private static final int HELLO_ID = 1; 
    
        mNotificationManager.notify(HELLO_ID, notification); 
    

    Eso es todo. Su usuario ahora ha sido notificado.

+0

new Notification() y setLatestEventInfo están en desuso ahora. Consulte mi respuesta a continuación o vaya a la documentación de google, http: //developer.android.com/guide/topics/ui/notifiers/notifications.html, para ver la forma actualizada de mostrar una notificación. – GLee

3

algunas sugerencias:

  • si quieres icono en la barra de notificaciones, debe enviar alguna notificación.
  • No se puede iniciar la aplicación haciendo clic en el ícono de notificación. Se puede iniciar haciendo clic en la notificación, que estará disponible si la barra de notificaciones desplegable del usuario. Para ello, debe crear PendingIntent.
7

La respuesta aceptada está obsoleta. Aquí está la manera de mostrar un diálogo, desde google documentation.

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this).setSmallIcon(R.drawable 
      .logo_listy).setContentTitle("My notification").setContentText("Hello World!"); 

    Intent resultIntent = new Intent(this, ResultActivity.class); 
    TaskStackBuilder stackBuilder = TaskStackBuilder.create(this); 
    stackBuilder.addParentStack(ResultActivity.class); 
    stackBuilder.addNextIntent(resultIntent); 
    PendingIntent resultPendingIntent = 
      stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT); 
    mBuilder.setContentIntent(resultPendingIntent); 

    NotificationManager mNotificationManager = 
      (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 
    mNotificationManager.notify(0, mBuilder.build()); 
+0

Funciona bien. Podría ser útil para que alguien lo haga una notificación "en curso": notificación noti = mBuilder.build(); noti.flags = Notificación.FLAG_ONGOING_EVENT; – Artur

+0

¿cómo debo configurar la notificación debería ser fuego después de cada 2 minutos? – Neo

Cuestiones relacionadas