wiki android par frandroid

Afficher une notification

De FrAndroid - Android docs.

Voyons comment afficher une notification dans votre application

Sommaire


Dans la barre de notification

Nous appelons barre de notification, la barre qui se situe tout en haut de votre téléphone

ThisIsNotificationBar.png


Voilà ce que ça donne quand une notification est affichée

WhatHappenInNotificationBar.png


Il faut tout d'abord créer une instance du système de notification

  1. mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);


Créons ensuite quelques paramètres

  1. int icon = R.drawable.iconNotification;
  2. CharSequence text = "Ma notification";
  3. long duration = System.currentTimeMillis();


Créons ensuite notre notification avec ces paramètres

  1. Notification notification = new Notification(icon, text, time);


Maintenant, nous allons créer la sorte de bouton qui sera affichée dans le "menu déroulant" de la barre de notification

ButtonNotification.png


  1. CharSequence contentTitle = "Le titre du bouton";
  2. CharSequence contentText = "Le sous-titre";
  3. Intent notificationIntent = new Intent(this, MonActivity.class);
  4. PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);


Dans cet exemple, MonActivity.class renvoi à l'activity qui devra être affichée lors d'un clique sur le bouton.


Et on termine tout ça en affichant notre notification :

  1. Context context = getApplicationContext();
  2. notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
  3. mNotificationManager.notify(NOTIFY_ID, notification);



Code final

  1. mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE;);
  2.  
  3. int icon = R.drawable.iconNotification;
  4. CharSequence text = "Ma notification";
  5. long duration = System.currentTimeMillis();
  6.  
  7. Notification notification = new Notification(icon, text, time);
  8.  
  9. notification.flags|=Notification.FLAG_NO_CLEAR;
  10. notification.flags|=Notification.FLAG_ONGOING_EVENT;
  11.  
  12. CharSequence contentTitle = "Call+";
  13. CharSequence contentText = "Cliquez ici pour accéder à la fenêtre principale";
  14. Intent notificationIntent = new Intent(this, Main.class);
  15. PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
  16.  
  17. Context context = getApplicationContext();
  18.  
  19. notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
  20.  
  21. mNotificationManager.notify(NOTIFY_ID, notification);


Affichage d'un simple message

Voilà ce que nous voulons faire :

Toast.png

Un simple message qui vient informer l'utilisateur sans le faire changer de fenêtre.

Pour ça c'est extrêmement simple !

On met en place quelques informations

  1. CharSequence text = "Lancement de Call+";
  2. int duration = Toast.LENGTH_SHORT;

On créé un objet Toast, et on l'affiche

  1. Context context = getApplicationContext();
  2. Toast toast = Toast.makeText(context, text, duration);
  3. toast.show();


Code final

Celui-ci tient sur 4 lignes !

  1. CharSequence text = "Lancement de Call+";
  2. int duration = Toast.LENGTH_SHORT;
  3. Context context = getApplicationContext();
  4. Toast toast = Toast.makeText(context, text, duration);
  5. toast.show();