How to get notification data with onNewIntent

Asked

Viewed 275 times

1

I have an application where when I click on the notification I would like to get the values set in the notification putExtra within onNewIntent, follow my code to see if anyone can help me.

Intent i = new Intent(this,MainActivity.class);
    i.putExtra("id",id);
    i.putExtra("plan",plan);
    i.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(this,0,i,PendingIntent.FLAG_UPDATE_CURRENT);

Now on the Mainactivity

@Override
protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    setIntent(intent);

    String id;
    String plan;
    Bundle extras = intent.getExtras();
    if(extras == null) {
        Log.w("extras", "false");
        id= null;
        plan= null;
    } else {
        if(extras.getString("id") != "" && extras.getString("plan") != "") {
            Log.w("extras", "true");
            id= extras.getString("id");
            plan= extras.getString("plan");
        }
    }
}
  • What’s the problem? Enter the method onNewIntent() and the Bundle is null? Or it doesn’t even enter the method?

  • The value was null, I was able to run by removing the line setIntent();

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.