How can I attach multiple extras to a single Intent in the Bundle object?

40
February 12, 2019, at 05:10 AM

I'm receiving several notifications each with its own unique id.

The desired scenario is for each notification to have its own extra.

But when I store/stash the notifications in a Bundle object all notifications are assigned the same extra-(i.e. the extra that belongs to the last notification).

I have confirmed this because when I click on any notification, I'm sent to the last post I received.

While I want to be sent to the post corresponding to the notification I just clicked.

Below is the relevant code snippet:-

 public void createNotif(Poste poste, Comment comment, Boolean checkGroupSon) {
    if (comment == null) {
        emeteur = poste.getEmmet();
        message = poste.getTx();
        pathImg = poste.getImg();
    } else {
        emeteur = comment.getEmmet();
        message = comment.getComment();
        pathImg = comment.getImage();
    }
    //onDismiss Intent
    Intent intent = new Intent(mContext, NotificationControllerReceiver.class);
    PendingIntent mBroadcastIntentController = PendingIntent.getBroadcast(mContext, 0, intent, 0);

    Intent notificationIntent = new Intent(mContext, CommentPhotoActivity.class);
    notificationIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // Intent.FLAG_ACTIVITY_CLEAR_TASK|
    notificationIntent.putExtra("pid", poste.getId());

    PendingIntent contentIntent = PendingIntent.getActivity(mContext, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    //TEST    notification.setLatestEventInfo(getApplicationContext(), "YuYu", "Vous avez re├žu un nouveau poste", contentIntent);
    //TEST  notification.flags = Notification.FLAG_AUTO_CANCEL;
    //TEST
    //    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(getApplicationContext()).setContentTitle("Poste: "+emeteur).setSmallIcon(R.drawable.ic_launcher).setContentIntent(contentIntent).setContentText(message).setDeleteIntent(mBroadcastIntentController).setPriority(Notification.PRIORITY_MAX);
    NotificationCompat.Builder notificationBuilder = null;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        notificationBuilder = new NotificationCompat.Builder(mContext, "poste");

            if (pathImg == null)
                notificationBuilder.setContentTitle(emeteur).setSmallIcon(R.drawable.ic_launcher).setContentIntent(contentIntent).setContentText(emeteur + ": " + message).setDeleteIntent(mBroadcastIntentController).setPriority(NotificationCompat.PRIORITY_MAX);
            else
                notificationBuilder.setContentTitle(emeteur).setSmallIcon(R.drawable.ic_launcher).setContentIntent(contentIntent).setContentText(emeteur + ": Photo \uD83D\uDCF7 " + message).setDeleteIntent(mBroadcastIntentController).setPriority(NotificationCompat.PRIORITY_MAX);

        }
        //  notificationBuilder.setContentTitle("Poste: "+emeteur).setSmallIcon(R.drawable.ic_launcher).setContentIntent(contentIntent).setContentText(message).setDeleteIntent(mBroadcastIntentController).setPriority(Notification.PRIORITY_MAX);
    } 
    //  long[] v = {500,1000};
    // notificationBuilder.setVibrate(v);
    notifications = null;
    boolean b = notifHashMap.containsKey(poste.getId());
    if (b) {
        notifications = notifHashMap.get(poste.getId());
        // Add your All messages here or use Loop to generate messages
        if (pathImg == null)
            notifications.add(emeteur + ":" + message);
        else
            notifications.add(emeteur + ": Photo \uD83D\uDCF7 " + message);
        notifHashMap.put(poste.getId(), notifications);
    } else {
        notifications = new ArrayList<String>();
        // Add your All messages here or use Loop to generate messages
        if (pathImg == null)
            notifications.add(emeteur + ":" + message);
        else
            notifications.add(emeteur + ": Photo \uD83D\uDCF7 " + message);
        notifHashMap.put(poste.getId(), notifications);
    }

    //  if (inboxStyle == null)
    //    inboxStyle = new NotificationCompat.InboxStyle();
    //else
    inboxStyle = new NotificationCompat.InboxStyle(notificationBuilder);
    if (notifications.size() > 1) {
        if (poste.getImg() == null)
            inboxStyle.setBigContentTitle("Poste:" + poste.getTx());
        else
            inboxStyle.setBigContentTitle("Poste: \uD83D\uDCF7 " + poste.getTx());
        inboxStyle.setSummaryText("Vous avez " + notifications.size() + " notifications.");
        for (int i = 0; i < notifications.size(); i++) {
            inboxStyle.addLine(notifications.get(i));
        }
    } else if (notifications.size() == 1) {
        inboxStyle.addLine(notifications.get(0));
    }
    notificationBuilder.setStyle(inboxStyle);
    notificationBuilder.setVibrate(new long[]{0L});
    //notificationBuilder.setNumber(value++);
    if (!checkGroupSon) {
        Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        notificationBuilder.setSound(alarmSound);
    }

    NotifManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                                             /* Create or update. */
        mChannel = new NotificationChannel("poste", "YuYu", NotificationManager.IMPORTANCE_LOW);
        mChannel.enableLights(true);
        mChannel.setLightColor(Color.RED);
        mChannel.canShowBadge();
        mChannel.setVibrationPattern(new long[]{ 0 });
        mChannel.enableVibration(true);
      /*  if (!checkGroupSon) {
            mChannel.enableVibration(true);
            mChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
        } */
        assert NotifManager != null;
        notificationBuilder.setChannelId("poste");
       // NotifManager.deleteNotificationChannel("poste");
        NotifManager.createNotificationChannel(mChannel);
    }
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        summaryNotification = new NotificationCompat.Builder(mContext, "poste")
                .setSmallIcon(R.drawable.ic_launcher)
                .setStyle(new NotificationCompat.InboxStyle()
                        .addLine("1")
                        .addLine("2")
                        .setBigContentTitle("nouveau messages")
                        .setSummaryText("Nouveau message"))
                .setPriority(NotificationCompat.PRIORITY_LOW)
                .setGroup("example_group")
                .setGroupAlertBehavior(NotificationCompat.GROUP_ALERT_CHILDREN)
                .setGroupSummary(true).build();
    }
    Notification notification = notificationBuilder.setGroup("example_group").build();
    notification.flags = Notification.FLAG_AUTO_CANCEL;
    // assert  NotifPostMsgManager != null;
    //TEST   manager.notify(ID_NOTIFICATION, notification);
    NotifManager.notify(poste.getId(), notification);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
        NotifManager.notify(1, summaryNotification);
}
READ ALSO
React Native TextField component in external library on focus

React Native TextField component in external library on focus

I have a Register Component that has PhoneInput Component from the external library https://githubcom/thegamenicorus/react-native-phone-input The problem is I want to execute some code as soon as TextField inside the PhoneInput is Focused

69
Installing Flutter on Mac OS

Installing Flutter on Mac OS

on running flutter doctor i get the below dependency lacking :

65
Fingerprint after inactivity

Fingerprint after inactivity

I am implementing fingerprint support to protect sensitive areas of my app and access to some web APIs (using a signature)I am initializing the key generator like this:

46
Gradle Task from Plugin Doesn&#39;t Run on &ldquo;Build&rdquo;, But Does Run on &ldquo;Clean&rdquo;

Gradle Task from Plugin Doesn't Run on “Build”, But Does Run on “Clean”

We have an Android project which requires a certain Gradle Plugin Task to run before we build the APK(The plugin is written by us)

62