0

I have implemented a service which extends FirebaseMessagingService and i am using it when the app is on the foreground to the received notification with my custom in-app notification view. And when the app is in background, as all other FCM releated topics and documentation here suggests, it is handled by the system tray, i only need to get the data from the bundle on the launched activity and make my redirections etc. on there.

What i also need to do is, when a notification is received and the app is launched by tapping on the launcher icon, not by notification. I need to get the notification data in that case and again make my redirections accordingly. But i couldn't able to get the attached data in that case. Any idea how to get the notification data on regular app launch?

caneru
  • 391
  • 1
  • 2
  • 15
  • You need just remote data when app closed, and not want to show notification at that time right?? – DeePanShu Nov 02 '21 at 06:41
  • Yes, i need data, i will show it later when the app is launched. I know i can override handleIntent method of FirebaseMessagingService to get data anytime, but i am not sure if that is the right way – caneru Nov 02 '21 at 06:53
  • There is a solution but it works always I can't say. Make a separate service that will ping after every 5 seconds and make a static object which will inform you that the application is open. If the application is open then onMessageReceived do whatever you want to do and if the application is closed means static instance killed, then store the data with timeinmillies and check stored data on launcher activity to perform your functions. – DeePanShu Nov 02 '21 at 07:01
  • nah, that is too much of work. I can use handleIntent instead to do something similar with less work, as every intent (dismiss, new token, receive) falls into there before anywhere else. But according to documentation, we should be using onMessageReceived, so i am not sure if that is the right way – caneru Nov 02 '21 at 07:51
  • ok, I just suggest the way, I will try this if I stucked there, hope you will find a better solution, If you not got the solution then try this one, thank you. – DeePanShu Nov 02 '21 at 07:55

1 Answers1

0

Okay, this is how i solved my issue for above case, it is more like a workaround than a solution, but maybe someone might find it useful for his/her own purposes:

  • If the app was in the background when the notification is received and it is launched from the app icon but not notification, then i catch it inside the handleIntent method of the service that extends FirebaseMessagingService to store the received data on my local.
  • If the app was in the background when the notification is received and it is launched from the notification but not from the app icon, then i get the notification data from the getIntent().getExtras() of SplasActivity (which is my LAUNCHER activity)
  • If the app was in the foreground when the notification is received, the i show it as an in-app notification without storing anything to my local

And i delete the stored data, after i show the incoming message as in-app notification on my next app launch either by notification or from app launcher icon.

There is definetely downsides of this implementation, but like i said it is more like workaround than a solution. I guess a proper solution should send the data as a data message as it is described in this post. But unfortunately that was not an option for me.

caneru
  • 391
  • 1
  • 2
  • 15