5

I am currently developing an android app where I create a notification Intent which adds parameters to the bundle of the intent. When the notification is clicked it calls an activity and gets the data from the bundle. However, the first time the app is used it works fine, but when you click on a different item its supposed to pass different data onto the notification activity but for some reason its not replacing the old data with the new.

I have tried to call bundle.removeExtra("companyPassword") before I use the putExtra but it doesn't seem to make any difference. Below is the code for the notification

private void notification(String companyName, String companyURL, String companyUsername, String loginPassword)
{
    String ns = Context.NOTIFICATION_SERVICE;
    NotificationManager mNotificationManager = (NotificationManager)getSystemService(ns);

    int icon = R.drawable.icon;
    CharSequence tickerText = "Click notification to copy password";
    long when = System.currentTimeMillis();

    Notification notification = new Notification(icon, tickerText, when);

    Context context = getApplicationContext();
    CharSequence contentTitle = "PM - Login Management";
    CharSequence contentText = "Click here to copy password";
    Intent notificationIntent = new Intent(ShowLogins.this, DataManagement.class);
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
    notificationIntent.removeExtra("companyName");
    notificationIntent.removeExtra("companyURL");
    notificationIntent.removeExtra("companyUsername");
    notificationIntent.removeExtra("companyPassword");
    notificationIntent.putExtra("companyName", companyName);
    notificationIntent.putExtra("companyURL", companyURL);
    notificationIntent.putExtra("companyUsername", companyUsername);
    notificationIntent.putExtra("companyPassword", loginPassword);

    notification.flags |= Notification.FLAG_AUTO_CANCEL;
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
    notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);

    final int NOTIFICATION_ID = 1;

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

    mNotificationManager.notify(NOTIFICATION_ID, notification);
    finish();

And below is the code for the Notification activty where it retrieves the data that is passed into the bundle

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        try {
            // setContentView(R.layout.data_mangement);

            Bundle bundle = this.getIntent().getExtras();

            company = bundle.getString("companyName");
            companyURL = bundle.getString("companyURL");
            username = bundle.getString("companyUsername");
            password = bundle.getString("companyPassword");
            bundle.clear();

            ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
            Encryption encryption = new Encryption();
            String decryptedPassword = encryption.decrypt(password);
            clipboard.setText(decryptedPassword);
            toastNotification("Password Successfully Copied\n\nPaste into password field");
            bundle.clear();
            finish();
            moveTaskToBack(true);

        } catch (Exception ex) {
            Log.d("DataManagement Error", ex.toString());
        }
    }

For some reason when the activity for the notification is called it is only returning the data that was first sent when an item was first selected instead of retrieving the new data.

Thanks for any help you can provide.

Boardy
  • 35,417
  • 104
  • 256
  • 447
  • Just after doing some more debugging I looked what was actually inside the bundle when Eclipse stopped on the debug point and it looks as once the activity has been used once the next time it doesn't update the bundle – Boardy Feb 19 '11 at 15:44

1 Answers1

6

However, the first time the app is used it works fine, but when you click on a different item its supposed to pass different data onto the notification activity but for some reason its not replacing the old data with the new.

That is because you are not sufficiently changing your Intent and you are not using FLAG_UPDATE_CURRENT in your PendingIntent. Try the latter and see if it helps.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • what do you mean not sufficiently changing the Intent. I have done what you suggested creating a PendingIntent adding the flag but not it seems to not be passing anything on its just coming up with a NullPointerException – Boardy Feb 19 '11 at 19:55
  • @Boardy: If you call `getActivity()` on `PendingIntent` for an `Intent`, then later call `getActivity()` on `PendingIntent` for another `Intent` that is equivalent (same Intent action, data, categories, and components), you get *the same `PendingIntent`* back. Without `FLAG_UPDATE_CURRENT`, this means you get the extras from your first `Intent`, not the second. With respect to your exception, please configure the whole `Intent` first before creating the `PendingIntent`. – CommonsWare Feb 19 '11 at 20:01
  • This has a working solution http://stackoverflow.com/questions/3168484/pendingintent-works-correctly-for-the-first-notification-but-incorrectly-for-the set some dummy action on the Intent, otherwise extras are dropped. For example intent.setAction("foo") – Sam Jan 23 '12 at 10:24