Yuki Goto
Yuki Goto

Reputation: 51

I get the same extra data from notifications even if I create some intents with different data

I copied and paste from this document and tried to PutExtra.

I tapped button1, button2 and button3, and then tapped a notification from button1, but ResultActivity was started with button3, why?

I want to be displayed as button1. Do you know a solution?

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findViewById(R.id.button).setOnClickListener(listener);
        findViewById(R.id.button2).setOnClickListener(listener);
        findViewById(R.id.button3).setOnClickListener(listener);
    }

    private View.OnClickListener listener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.button:
                    notifyNotification("button1");
                    break;
                case R.id.button2:
                    notifyNotification("button2");
                    break;
                case R.id.button3:
                    notifyNotification("button3");
                    break;
            }

        }
    };

    private int mId = 0;

    private void notifyNotification(String value) {
        NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder(this)
                        .setSmallIcon(android.R.drawable.stat_notify_chat)
                        .setContentTitle("My notification")
                        .setContentText(value);
        // Creates an explicit intent for an Activity in your app
        Intent resultIntent = new Intent(this, ResultActivity.class);
        resultIntent.putExtra("value", value); // ***** I added this code *****

        // The stack builder object will contain an artificial back stack for the
        // started Activity.
        // This ensures that navigating backward from the Activity leads out of
        // your application to the Home screen.
        TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
        // Adds the back stack for the Intent (but not the Intent itself)
        stackBuilder.addParentStack(ResultActivity.class);
        // Adds the Intent that starts the Activity to the top of the stack
        stackBuilder.addNextIntent(resultIntent);
        PendingIntent resultPendingIntent =
                stackBuilder.getPendingIntent(
                        0,
                        PendingIntent.FLAG_UPDATE_CURRENT
                );
        mBuilder.setContentIntent(resultPendingIntent);
        NotificationManager mNotificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        // mId allows you to update the notification later on.
        mNotificationManager.notify(mId, mBuilder.build());

        mId++;
    }
}

public class ResultActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_result);

        Intent intent = getIntent();
        String value = intent.getStringExtra("value");

        ((TextView)findViewById(R.id.textView)).setText(value);
    }
}

Upvotes: 3

Views: 1306

Answers (2)

Yogendra
Yogendra

Reputation: 5258

long mId = System.currentTimemiles();

Upvotes: 0

Andrew T.
Andrew T.

Reputation: 4707

The problem lies on PendingIntent.FLAG_UPDATE_CURRENT and using same PendingIntent's request code, which is 0. That way, the all notifications' Intent will be updated to the last Button pressed.

Try to also use different request code for each PendingIntent.

PendingIntent resultPendingIntent =
    stackBuilder.getPendingIntent(
        mId, // update to use same ID with notification's ID
        PendingIntent.FLAG_UPDATE_CURRENT
    );

Upvotes: 8

Related Questions