Search code examples
androidandroid-intentandroid-pendingintent

Pending intent seems to not pass extras


I use an Intent that start when a user click on a push notification. I have some problems passing extras through a pending intent. When I do this:

Intent i = new Intent(this, DashboardActivity.class);
i.putExtra("pending", true);
i.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);

PendingIntent pendingIntent = PendingIntent.getActivity(this,0,i,PendingIntent.FLAG_UPDATE_CURRENT);

I expect this behaviour:

  1. The pending intent start the DashboardActivity
  2. Inside the DashboardActivity I check for extras
  3. If I have extras, go to activity B

Instead, what happens is:

  1. The pending intent start the DashboardActivity
  2. Extras are null

This is the code to check for extras into DashboardActivity:

if (getIntent().getExtras() != null) {
    Intent i = new Intent(DashboardActivity.this, B.class);
    startActivity(i);
}

I don't realize how to quit this situation so if someone could help me I'll be grateful!

EDIT:

private void sendNotification(String messageBody){
    String id="",message="",title="";

    if(type.equals("json")) {
        try {
            JSONObject jsonObject = new JSONObject(messageBody);
            id = jsonObject.getString("id");
            message = jsonObject.getString("message");
            title = jsonObject.getString("title");

        } catch (JSONException e) {
            //            }
        }
    }
    else if(type.equals("message"))
    {
        message = messageBody;
    }

    Intent i = new Intent(this, DashboardActivity.class);
    i.putExtra("pending", true);
    i.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
    i.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);

    PendingIntent pendingIntent = PendingIntent.getActivity(this,0,i,PendingIntent.FLAG_UPDATE_CURRENT);

    NotificationCompat.Builder notificationBuilder=new NotificationCompat.Builder(this);
    notificationBuilder.setContentTitle(getString(R.string.app_name));
    notificationBuilder.setContentText(message);
    Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    notificationBuilder.setSound(soundUri);
    notificationBuilder.setSmallIcon(R.mipmap.ic_launcher);
    notificationBuilder.setLargeIcon(BitmapFactory.decodeResource(this.getResources(),R.mipmap.ic_launcher));
    notificationBuilder.setAutoCancel(true);
    Vibrator v = (Vibrator) this.getSystemService(Context.VIBRATOR_SERVICE);
    v.vibrate(1000);
    notificationBuilder.setContentIntent(pendingIntent);

    NotificationManager notificationManager=(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(0,notificationBuilder.build());
}

Solution

  • Try to use PendingIntent.FLAG_UPDATE_CURRENT.

    Also add Intent.FLAG_ACTIVITY_SINGLE_TOP so that if your activity is already opened, it is not recreated.

    Add the above 2 flags to your intent.

    Also in your DashboardActivity implement onNewIntent

    @Override
    public void onNewIntent(Intent intent){
        Bundle extras = intent.getExtras();
        if(extras != null){
            if(extras.containsKey("pending"))
         {
    
         }
      }