2015-11-03 1 views
0

J'ai essayé setAutocancel ainsi que FLAG_AUTO_CANCEL mais aucun de ces travaux et j'ai deux boutons à l'intérieur notification qui ouvrent deux activités diff .. bien voulu me aiderautohide la notification lorsque vous cliquez dessus sur les boutons dans la notification dans Android

public void createNotification(View view) { 

    Intent yesIntent = new Intent(this, MyDialog.class); 
    yesIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
    yesIntent.putExtra("NOTI_ID", NOTIFICATION_ID); 
    yesIntent.putExtra("ACTION", 1); 
    PendingIntent yespIntent = PendingIntent.getActivity(this, (int) System.currentTimeMillis(), yesIntent, 0); 

    Intent noIntent = new Intent(this, MyDialog.class); 
    noIntent.putExtra("ACTION", 0); 
    PendingIntent nopIntent = PendingIntent.getActivity(this, (int) System.currentTimeMillis(), noIntent, 0); 
    NotificationCompat.Builder noti = new NotificationCompat.Builder (this) 
      .setContentTitle("New Project Approval") 
      .setContentText("Project Description") 
      .setAutoCancel(true) 
      .setSmallIcon(R.mipmap.bell) 
      .addAction(R.mipmap.approve_ic, "Approve", yespIntent) 
      .addAction(R.mipmap.rejecticon, "Reject", nopIntent) ; 

    NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 
    noti.build().flags |= Notification.FLAG_AUTO_CANCEL; 
    notificationManager.notify(NOTIFICATION_ID, noti.build()); 

} 
+0

J'ai eu mon problème résolu par le lien suivant http://stackoverflow.com/questions/22230568/notification-android-does-not-close-after-click – viddzy

Répondre

0

Vous devez annuler votre notification manuellement dans votre activité. Passez l'ID de notification à votre PendingIntent pour le récupérer dans votre activité et annuler la notification correspondante via le NotificationManager comme:

NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 
manager.cancel(yourNotificationId); 

Un bon exemple et complet: How to dismiss notification after action has been clicked

+0

En utilisant le lien ci-dessus j'appelle la méthode présente dans mon activité et en annulant la notification .. je crée un objet d'activité résultante et appelle la méthode comme dialog.CancelNotification (NOTIFICATION_ID); où la méthode interne j'ai collé au-dessus du code donné par vous mais j'obtiens l'erreur ci-dessous java.lang.IllegalStateException: Les services de système non disponibles aux activités avant onCreate() – viddzy

+0

Où appelez-vous cette méthode? Si vous l'appelez dans la méthode onCreate(), appelez-vous super.onCreate() avant? – Bubu

0

Auparavant, je callied la NotificationManager en dehors de la méthode onCreate() , qui ne fonctionnait pas. Mais après le StackOverflow threadr suggested by @bubu je l'ai résolu en appelant NotificationManager à l'intérieur de onCreate();