Prince
Prince

Reputation: 496

Intent not delivered correctly

I am sending an Intent from GCMIntentService through PendingIntent. I am inserting some strings inside intent and in PendingIntent, I call an activity which will receive intent. But the data is not what I am sending. Here is the screenshot. enter image description here

The first Intent shown is what I am sending, and the second is what I am receiving. Here is the code for sending:

Intent intent = new Intent(getBaseContext(),
                            Group_Chat_MainActivity.class);
                    intent.putExtra("group_id", group_id);
                    intent.putExtra("group_title", name);
                    intent.putExtra("from_push", true);
                    PendingIntent pendingIntent = PendingIntent.getActivity(
                            getBaseContext(), 100,
                            intent, 0);
Notification notification = new NotificationCompat.Builder(
                            getBaseContext()).setContentTitle("Paisa Swipe")
                            .setContentText(msg)
                            .setSmallIcon(R.drawable.home_logo)
                            .setTicker(ticker)
                            .setDefaults(Notification.DEFAULT_ALL)
                            .setWhen(System.currentTimeMillis())
                            .setContentIntent(pendingIntent)
                            .setAutoCancel(true).build();

                    notificationManager.notify(Integer.parseInt(group_id),
                            notification);

When I click notification, it sends to the Group_Chat_MainActivity class. In that, I use getIntent() to fetch the Intent data:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.chat_activity_main);
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    getIntent().getStringExtra("group_title")
    getIntent().getStringExtra("group_id")
    getIntent().getBooleanExtra("from_push",false)
    }

Here I am getting different strings then what I sent. Any ideas?

Upvotes: 1

Views: 235

Answers (2)

Mukesh Rana
Mukesh Rana

Reputation: 4091

Actually the problem is your PendingIntent requestCode is same all the time i.e 100. Keep it to be a unique for a group for eg. your group_id. Also your context here should be your GCMIntentService context.Try changing this to

PendingIntent pendingIntent = PendingIntent.getActivity(
                        getBaseContext(), 100,
                        intent, 0);

this

PendingIntent pendingIntent = PendingIntent.getActivity(
                        yourContextHere, Integer.parseInt(group_id),
                        intent, 0);

Upvotes: 1

Naruto Uzumaki
Naruto Uzumaki

Reputation: 2087

Make sure your getBaseContext() is not null.
If you use it in Fragmnet try:

inflatedview.getContext();

instead of:

getBaseContext()

and when you want get value from your intent in OnCreate method, try this:

getIntent().getExtras().getString();

instead of:

getIntent().getStringExtra();

hope this help you.

Upvotes: 0

Related Questions