Pergunta

On my app, I post some notify the notify have a title and a text. When the user click on the notify i have to open an activity that show the title and the full text of the notify. It wotk fine if i have only one notification, but if there're 2 or more notify (with different id) on click, i open the activity ever with same title and text (of the first notify) how i can pass the correct value? sorry for bad english

public void sendSimpleNotification(String titolo, String testo, String imgUrl, int id) {

        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this.getApplicationContext());

        // Titolo e testo della notifica
        notificationBuilder.setContentTitle(titolo);
        notificationBuilder.setContentText(testo);

        // Testo che compare nella barra di stato non appena compare la notifica
        notificationBuilder.setTicker("Nuova offerta dall'Online app");

        // Data e ora della notifica
        notificationBuilder.setWhen(System.currentTimeMillis());

        // Icona della notifica
        notificationBuilder.setSmallIcon(R.drawable.ic_notifica);

        // Creiamo il pending intent che verrà lanciato quando la notifica
        // viene premuta
        //System.out.println("definisco l'intent per la notifica");
        Intent notificationIntent = new Intent(this.getApplicationContext(), MostraNotifica.class); //eventualmente qui devo collegare le info per l'intent da stampare
        notificationIntent.putExtra("titolo", titolo);
        notificationIntent.putExtra("testo", testo);
        imgUrl = imgUrl.replaceAll(" ", "%20");
        notificationIntent.putExtra("imgUrl", "http://www.ac-online.it/public/"+imgUrl);
        PendingIntent contentIntent = PendingIntent.getActivity(this.getApplicationContext(), 1,notificationIntent, 0);
       // System.out.println("ho finito di definire la notifica");
        notificationBuilder.setContentIntent(contentIntent);

        System.out.println("stamp " +titolo + testo + id); //here stamp the correct value

        // Impostiamo il suono, le luci e la vibrazione di default
        notificationBuilder.setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_LIGHTS | Notification.DEFAULT_VIBRATE);
       // System.out.println("sto per mostrare la notifica");

      mNotificationManager = (NotificationManager) this.getApplicationContext().getSystemService(this.getApplicationContext().NOTIFICATION_SERVICE);
      Notification notification = notificationBuilder.build();
      notification.flags = Notification.DEFAULT_LIGHTS | Notification.FLAG_AUTO_CANCEL;
      mNotificationManager.notify(id, notification);
     //   System.out.println("notifica stampata");


}
Foi útil?

Solução

Create pending intent like this

PendingIntent.getActivity(this.getApplicationContext(), (int)(Math.random() * 100), notificationIntent,PendingIntent.FLAG_UPDATE_CURRENT);
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top