2017-08-11 3 views
0

Je travaille sur le projet où nous montrons des notifications pour chaque mise à jour de l'album photo. Mais en appuyant sur une notification, il supprime toutes les notifications de la barre de notification.En tapant une notification, en supprimant toutes les notifications

J'ai essayé le code suivant:

private void handleNotification(NotificationModel notificationModel) { 
    // display the notification in the notification bar 
    int i=0; 
    if(!TextUtils.isEmpty(notificationModel.getAlbum_id())) 
     i=Integer.parseInt(notificationModel.getAlbum_id()); 

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

    //icon appears in device notification bar and right hand corner of notification 
    builder.setSmallIcon(R.mipmap.ic_launcher); 

    Intent intent=new Intent(this,MainActivity.class); 
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); 
    intent.putExtra(MainActivity.BUNDLE_ALBUM_ID_KEY, notificationModel.getAlbum_id()); 
    intent.putExtra(MainActivity.BUNDLE_ALBUM_NAME_KEY, notificationModel.getAlbum_title()); 

    /*intent = new Intent(this, NotificationActivity.class);*/ 
    PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(),i,intent,PendingIntent.FLAG_UPDATE_CURRENT); 
    // Set the intent that will fire when the user taps the notification. 
    builder.setContentIntent(pendingIntent); 
    /*builder.setAutoCancel(true);*/ 
    // Large icon appears on the left of the notification 
    builder.setLargeIcon(BitmapFactory.decodeResource(getApplicationContext().getResources(), R.mipmap.ic_launcher)); 

    // Content title, which appears in large type at the top of the notification 
    builder.setContentTitle(notificationModel.getTitle()); 
    builder.setContentText(notificationModel.getMessage()); 
    builder.setAutoCancel(true); 
    NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 
    Notification notification=builder.build(); 

    notification.defaults = Notification.DEFAULT_SOUND 
      | Notification.DEFAULT_VIBRATE; 
    notification.flags |= Notification.FLAG_AUTO_CANCEL; 

    notificationManager.notify(i,notification); 
} 

Qu'est-ce que je fais mal?

+0

ou de vérifier la valeur de je reste même chaque fois que cela pourrait être un problème ... –

+0

@MohdSaquib essayé. mais ne fonctionne pas. vérifié la valeur i aussi. c'est unique. – Subhra

+0

@Subhra Votre code fonctionne absolument bien. Vérifiez le même projet dans un autre appareil ou vérifiez les paramètres de notification de votre appareil. – Nikhil

Répondre

0

S'il vous plaît vérifier la valeur de « i » qui fonctionne comme identifiant de notification reste identique à chaque fois que cela pourrait être un problème ... Ici

int i=0; 
    if(!TextUtils.isEmpty(notificationModel.getAlbum_id())) 
     i=Integer.parseInt(notificationModel.getAlbum_id()); 

S'il vous plaît vérifier la valeur de « i », je pense est toujours même dans votre cas que vous utilisez avec le code ci-dessous

PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(),**i**,intent,PendingIntent.FLAG_UPDATE_CURRENT); 

Et

notificationManager.notify(i,notification); 
+0

Vérifié la valeur «i». C'est unique. Si 'i' est identique, les notifications séparées n'auraient pas été créées pour chaque album. Des notifications séparées sont affichées pour chaque album. – Subhra

+0

Oui bro qui est la valeur de problème principal de je ne devrais pas être unique voir que j'ai donné la solution son essayer de comprendre la valeur logique de notification id c'est-à-dire je dans votre code devrait être différent chaque fois. Lorsque la valeur de i sera différente de ce que vous pouvez réaliser ce que vous voulez. –