Search code examples
androidandroid-intentandroid-serviceandroid-push-notification

I don't get my bundle's data in onNewIntent method of MainActivity


I get a push notification in my FireBaseService and I want to send it's data to MainActivity, below is my code:

        Intent intent = new Intent("PUSH_NOTIFICATION");
    intent.setClassName(this, MainActivity.class.getName());
    Bundle bundle = new Bundle();
    bundle.putParcelable("data", data);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    intent.putExtra("notification bundle", bundle);
    startActivity(intent);

onNewIntent() gets called but when I parse it, it gives me null data. Did I do anything wrong?

Thanks in advance.

Edit: My MainActivity launch mode is singleTop (to get same instance of it)


Solution

  • I found the answer, I read somewhere on Stackoverflow that Bundles have problem with Parcelable Objects, and it's better to convert them to Byte arrays and then bind them to Bundles, I didn't test it, but I bind JSON string itself and send it with Bundle and then parse it in onNewIntent() method, instead of parse it and send parcelable object, and it worked