1

I've searched for about hour to find some solution how to send extras to activity when user clicks in notification box but everything I've found didn't work for me.

I need to pass event ID to activity where I show user info about this event.

I've used setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), setAction("Action"), PendingIntent.FLAG_UPDATE_CURRENT and combination of all of these solution but neither didn't work.

That's my code:

Notification.Builder notiBuilder = new Notification.Builder(context);
                notiBuilder.setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.drawable.logo));
                notiBuilder.setSmallIcon(R.drawable.logo);
                notiBuilder.setContentTitle(context.getString(R.string.eventNitificationTitle));
                notiBuilder.setContentText(obj.getString("nazwa") + " " + obj.getString("data"));

                Intent eventIntenet = new Intent(context, EventActivity.class);
                eventIntenet.setAction("Action_"+id);
                eventIntenet.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);

                eventIntenet.putExtra("eventID", id);

                PendingIntent pIntent = PendingIntent.getActivity(context, 0, eventIntenet, PendingIntent.FLAG_UPDATE_CURRENT);

                eventIntenet = null;

                notiBuilder.setContentIntent(pIntent);

                pIntent = null;

                NotificationManager notiManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                    notiManager.notify(Integer.parseInt(id), notiBuilder.build());
                } else {
                    notiManager.notify(Integer.parseInt(id), notiBuilder.getNotification());
                }

And way to get my Int:

this.eventID = getIntent().getIntExtra("eventID", -1);

Everytime when I click in notification, I'm moving to activity but getExtra returns -1.

  • Try getIntent().getExtra().getInt("eventID") and remove those you setting null to the Intents atleast don't set it until you finish notify. – Thilek Mar 11 '16 at 22:02
  • Thanks for reply. I've done it but still didn't works. I have one question more - setting variable's value to null when I don't need it anymore is good idea? – Wojciech Brzeziński Mar 11 '16 at 22:21

2 Answers2

0

You can try something along these lines:

                NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(ChatService.this).setSmallIcon(R.drawable.app_icon)
                        .setContentTitle(senderName)
                        .setContentText("Notification Text Here");
                mBuilder.setAutoCancel(true);

                Intent resultIntent = new Intent(MyService.this, TargetActivity.class);
                resultIntent.putExtra("eventID", id);

                TaskStackBuilder stackBuilder = TaskStackBuilder.create(MyService.this);
                stackBuilder.addParentStack(TargetActivity.class);
                stackBuilder.addNextIntent(resultIntent);

                PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);

                mBuilder.setContentIntent(resultPendingIntent);

                NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
                mNotificationManager.notify(id, mBuilder.build());
0

If your app is already running and your EventActivity is already on the top of the stack, this code:

this.eventID = getIntent().getIntExtra("eventID", -1);

will always use the Intent that EventActivity was started with the first time.

You should override onNewIntent() and get the "extra" from the Intent that is passed as an argument to that method.

David Wasser
  • 85,616
  • 15
  • 182
  • 239