rosu alin
rosu alin

Reputation: 5830

Activity does not get correct bundle

I am working with GCM, and this is my onMessage function:

@Override
protected void onMessage(Context context, Intent intent) {
    if (intent != null && intent.getExtras() != null) {
        try {
            String message = intent.getExtras().getString("message");
            String userId = intent.getExtras().getString("user_id");
            Log.i("","postda user id is:" + userId);
            Log.i("","will enter here FRIENDS: ");
            generateNotification(context, message, userId);
        } catch (Exception e) {
            Utils.appendLog("onMessage errror : " + e.getMessage());
            Log.i(TAG, e.getMessage(), e);
        }
    }
}

This is my CreateNotification function:

private static void generateNotification(Context context, String message, String userID) {
    Log.i("", "postda user message " + message + ".... userid: " + userID);
    String title = context.getString(R.string.passenger_name);
    Intent notificationIntent = new Intent(context, PSProfileActivity.class);
    notificationIntent.putExtra("id", userID);
    Log.i("", "postda ---------- userid: "+ userID);
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP| Intent.FLAG_ACTIVITY_NEW_TASK);
    PendingIntent intent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
    Log.i("", "postda message: " + message + "....userID " + userID );

    PSLocationCenter.getInstance().pref.setDataChanged(context, true);
    PSLocationCenter.getInstance().pref.setDataChangedProfile(context, true);

    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context);
    mBuilder.setContentTitle(title).setContentText(message).setSmallIcon(R.drawable.notification_icon);
    mBuilder.setContentIntent(intent);
    Notification notification = mBuilder.build();
    notification.flags |= Notification.FLAG_AUTO_CANCEL;

    NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(1, notification);

    playTone(context);
}

And this is the PSProfileActivity onCreate function:

@Override
protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_profile);
    ButterKnife.inject(this);

    mHeader = new ProfileHeader(this, backFromHeader);
    mData = new ProfileData(this);
    mButtons = new ProfileViewPagerButtons(PSProfileActivity.this, firstPage);

    Bundle bundle = getIntent().getExtras();
    if(bundle != null) id = bundle.getString("id");
    Log.i("", "postda in profile id is:" + id);
    if(!id.contentEquals(String.valueOf(PSLocationCenter.getInstance().pref.getUserId(PSProfileActivity.this)))){
        findViewById(R.id.action_settings).setVisibility(View.INVISIBLE);
    }
    Log.i("", "postda in profile id is 2:" + id);
}

And this is my Logcat response:

04-17 15:33:53.650   9699-11695/nl.hgrams.passenger I/﹕ postda user id is:23
04-17 15:33:53.651   9699-11695/nl.hgrams.passenger I/﹕ postda user message alin reddd accepted your friend request..... userid: 23
04-17 15:33:53.651   9699-11695/nl.hgrams.passenger I/﹕ postda ---------- userid: 23
04-17 15:33:53.652   9699-11695/nl.hgrams.passenger I/﹕ postda message: alin reddd accepted your friend request.....userID 23
04-17 15:33:58.812    9699-9699/nl.hgrams.passenger I/﹕ postda in profile id is:28
04-17 15:33:58.814    9699-9699/nl.hgrams.passenger I/﹕ postda in profile id is 2:28

As you can see, I'm sending via the bundle an ID, but the one that I get on the other page, is a totally different id (it's actually the last ID that should have been gotten here). This is really weird, does anyone know why this is happening?

Upvotes: 1

Views: 93

Answers (4)

lakshay
lakshay

Reputation: 1028

You are generating intent with the same id always. You need to pass the unique identifier as the second parameter and also need to add the Flag FLAG_UPDATE_CURRENT so that the extras get updated and you always get the latest extras that you passed .

So you have to generate intent like this :

PendingIntent intent = PendingIntent.getActivity(context, identifier , notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT );

identifier can be anything unique like auto increment variable or current timestamp

Upvotes: 1

Green goblin
Green goblin

Reputation: 9996

Use unique identitier in the PendingIntent:

int iUniqueId = (int) (System.currentTimeMillis() & 0xfffffff);      
PendingIntent.getActivity(context, iUniqueId, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

Upvotes: 2

AniV
AniV

Reputation: 4037

I think you have to use this flag, FLAG_UPDATE_CURRENT.

Like this: PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

This would refresh the old intents with the new extras that you are passing via bundle and get correct data that matches the ID.

Upvotes: 0

Ilan Klinghofer
Ilan Klinghofer

Reputation: 890

The problem may be that you are only using extras to modify the PendingIntent. Try embedding the id in the data field of notificationIntent instead.

From http://developer.android.com/reference/android/app/PendingIntent.html

A common mistake people make is to create multiple PendingIntent objects with Intents that only vary in their "extra" contents, expecting to get a different PendingIntent each time. This does not happen. The parts of the Intent that are used for matching are the same ones defined by Intent.filterEquals. If you use two Intent objects that are equivalent as per Intent.filterEquals, then you will get the same PendingIntent for both of them.

Upvotes: 0

Related Questions