Search code examples
androidandroid-intentandroid-activitybundleandroid-pendingintent

Intent returns WRONG data


I have this function to create a notification:

   NotificationCompat.Builder mBuilder =
                    new NotificationCompat.Builder(context)
                            .setSmallIcon(R.drawable.notification_icon)
                            .setContentTitle(title)
                            .setSound(alarmSound)
                            .setContentText(subtitle);
            if(haveIntent) {
                Intent intent = new Intent(context, PSBeaconMainActivity.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
                intent.putExtra("idToPass", beaconSettingID);
                PendingIntent pIntent = PendingIntent.getActivity(context, 0, intent, 0);
                mBuilder.setContentIntent(pIntent);
            }
            int mNotificationId = 001;
            NotificationManager mNotifyMgr =
                    (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
            mNotifyMgr.notify(mNotificationId, mBuilder.build());

I send for now a hardcoded value for the beaconSettingID such as this: https://scontent-amt2-1.xx.fbcdn.net/v/t35.0-12/28822214_1907084479363661_711965391_o.png?oh=b4c247836bcbd572d27f53738d2bdf6c&oe=5AA0C75E

This is my getIntent.getBundle:

   if (bundle != null && bundle.containsKey("idToPass")) {
        beaconUUID = bundle.getString("idToPass");
    }

But I get back this: https://scontent-amt2-1.xx.fbcdn.net/v/t35.0-12/28768405_1907084916030284_1955200830_o.png?oh=bcccf27352c90b248e82e08d902b8c15&oe=5AA1BA02

I don't understand? beaconID was the name of the "extra" key before. Since then I deleted the app, and changed it to idToPass. But I still get the old key, and more than that, a wrong value for it. Why is this happening?


Solution

  • Deleting an app doesn't mean that you deleted the previous notification. Try to change

    PendingIntent pIntent = PendingIntent.getActivity(context, 0, intent, 0);
    

    to:

    PendingIntent pIntent = PendingIntent.getActivity(context, SOME_UNIQUE_ID, intent, 0);
    

    In order to create new

    Or to:

    PendingIntent contentIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    

    To update previous notification

    Hope it helps