0

I have an unusual problem where my push notifications are using data from older push notifications, I think. My app has a list of companies and I use push notifications with data so that when the user clicks on the notification it takes them to that company in my app. While testing, the first message I send works fine and takes me to the right company. When I send another message that is supposed to go to a different company, it goes to the first one. I know that the app is receiving the correct information, because when I log the company ID it matches what its supposed to be. But when the app is opened from by clicking the notification, the company ID that shows up in the log is not the right one.

here is the code that handles the notification

private void sendNotification(String title, String msg, String cat, String comp) {
    mNotificationManager = (NotificationManager)
            this.getSystemService(Context.NOTIFICATION_SERVICE);

    Log.i("Send notif",""+comp);

    Intent launch = new Intent(this, AccountSetup.class);
    launch.putExtra("cat", cat).putExtra("comp", comp);
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
            launch, 0);

    NotificationCompat.Builder mBuilder =
            new NotificationCompat.Builder(this)
    .setSmallIcon(R.drawable.ic_launcher)
    .setContentTitle(title)
    .setStyle(new NotificationCompat.BigTextStyle()
    .bigText(msg))
    .setContentText(msg);

    mBuilder.setContentIntent(contentIntent);
    mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
}

this is working as expected, here is the code i have for pulling the information out onCreate()

    Intent thispage = getIntent();
    if(thispage.getStringExtra("comp") != null ) {
        Log.i("mainactivity extras","received cat " + thispage.getIntExtra("cat", 11) + " comp " + thispage.getStringExtra("comp"));
        mainPage.putExtra("currentLocation", user._Location)
            .putExtra("selectedCategory", thispage.getIntExtra("cat", 11))
            .putExtra("selectedCompany", -1)
            .putExtra("companyID",  thispage.getStringExtra("comp"));
    }

this is where im having the problem, when the activity launches, it still has the old company ID, regardless of what push notification message I clicked.

Am I missing something? the title and body of the message are correct, and the log shows that the company id is right too, just when I click it the intent extras are only showing whatever was sent in the first message.

JStephen
  • 1,059
  • 3
  • 13
  • 28

2 Answers2

2

Well I was facing a similar issue too. I solved it by using PendingIntent.Flagoneshot. Here's the line from my code:

PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
            intent, PendingIntent.FLAG_ONE_SHOT);

There are 5 types of pendingintent flags one of them might work for you if not this one.

Illegal Argument
  • 10,090
  • 2
  • 44
  • 61
  • 1
    Thanks this worked. My notification was still there afterwards so later clicks don't do anything (as the flag implies). In case anyone needs to know how to remove the notification [check this answer here](http://stackoverflow.com/a/15664841/2593088) – JStephen Jun 20 '14 at 14:45
  • For what it's worth, years later, I had the same problem but I needed to use the FLAG_UPDATE_CURRENT as otherwise the pending intent always had the extras that I put on the first notification. – Timbo Mar 30 '17 at 13:49
0

Note that the activity will be started outside of the context of an existing activity, so you must use the Intent.FLAG_ACTIVITY_NEW_TASK launch flag in the Intent. Try it.

Mehul Shah
  • 391
  • 3
  • 7
  • I tried this and It didn't work. However I kept the flag when testing Illegal Argument's answer and it worked, I'm not sure if it helped or not. – JStephen Jun 20 '14 at 14:49