1

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)

boltup_im_coding
  • 6,345
  • 6
  • 40
  • 52
MehDi
  • 504
  • 6
  • 20

2 Answers2

1

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

MehDi
  • 504
  • 6
  • 20
  • Can you explain this a little further with an example? – Christopher Smit Mar 19 '18 at 06:53
  • It's really simple, just add your string which you got from firebase, add it to a Bundle, then send this bundle via Intent. Just search for "pass an string via bundle to another activity", you will find so many sample. Only wanna remind that you should make your target activity android:launchMode="singleTask" in manifest (to avoid lunch it again). @ChristopherSmit – MehDi Apr 15 '18 at 13:06
  • Thank you! this really help me solve my problem!! – raquezha Aug 22 '21 at 22:03
0

I have recently implemented Firebase notifications in my app and I faced the same issue. The problem was when notification sent through Firebase console and app was in background the onMessageReceived method doesn't get called and onNewIntent() method returns null.

Try sending a push notification with the same payload through PostMan(RestApi Client). You will then be able to receive and fetch data in onMessageRecieved method of your receiver.

Karl Gjertsen
  • 4,690
  • 8
  • 41
  • 64
Aditi
  • 389
  • 4
  • 13
  • thanks Aditi, but I receive data through onMessageReceived and no problem(whether app is background or foreground) , if you have problem with it I can help you later, but this is not my problem – MehDi Apr 25 '17 at 08:36
  • Are you sending data through firebase Console? And if your onMessageReceieved is called , you can further define the flow of the app through this method. There is no need for onNewIntent – Aditi Apr 25 '17 at 08:47