PendingIntent not working when adding extras in intent

你说的曾经没有我的故事 提交于 2019-12-13 00:34:15

问题


I have GCMIntentService implemented and whenever i get the push notification i need to show the notification in notification menu and open an activity with some bundle values in the intent. I can see the notification in the notification menu but clicking on it just doesn't do anything. Following is the code which i am using :-

  mNotificationManager = (NotificationManager)
            this.getSystemService(Context.NOTIFICATION_SERVICE);

    Intent i = new Intent(this, ChatDetail.class);
    Bundle b = new Bundle();
    b.putString("my_id", "5356b178b130a74a57019fe9");
    b.putString("you_id", youId);
    i.putExtras(b);
   //       PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
   //               new Intent(this, MainActivity.class), 0);
            PendingIntent contentIntent = PendingIntent.getActivity(this, 0,      
                    i,PendingIntent.FLAG_CANCEL_CURRENT );

    NotificationCompat.Builder mBuilder =
            new NotificationCompat.Builder(this)
    .setSmallIcon(R.drawable.pool_my_ride_icon)
    .setContentTitle("GCM Notification")
    .setStyle(new NotificationCompat.BigTextStyle()
    .bigText(text))
    .setContentText(text);

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

I can see the notification but when i click on it the notification menu slides up and doesn't do anything doesn't opens any activity. If i don't send any bundle in the extras then i can open the activity but when i send bundle values then i can't open the activity.

Thanks in advance


回答1:


I'd try to add the extras to the intent one at a time :

i.putExtra("my_id", "5356b178b130a74a57019fe9");
i.putExtra("you_id", youId);



回答2:


hey try this it worked for me

Intent i = new Intent(this, ChatDetail.class);
Bundle b = new Bundle();
b.putString("my_id", "5356b178b130a74a57019fe9");
b.putString("you_id", youId);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
            Intent.FLAG_ACTIVITY_SINGLE_TOP);
i.putExtras(b);

the important point here is to send the additional flags to actually want your intent to be delivered

i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
            Intent.FLAG_ACTIVITY_SINGLE_TOP);

Just let me know if it worked for you!



来源:https://stackoverflow.com/questions/24766035/pendingintent-not-working-when-adding-extras-in-intent

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!