How to get notification data when the app launched from launcher icon - android

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?

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.

Related

How to get Android push notification payload when background App is launched by user click on notification

I have an Android app that's in the background. I've registered it with Firebase and have been able to successfully retrieve it's token, as well as sending messages to it via the Firebase Console Manager (FCM).
When I send test messages via FCM, when the app is in the foreground the
onMessageReceived(remoteMessage:RemoteMessage)
call back API for my FirebaseMessagingService derived class gets triggered. I can then call remoteMessage.notification!!.body and obtain it's payload. This is the documented behavior so it's working as expected.
When the app is in the background though (say I press the home key), and I send the test message, that event doesn't get called but instead a notification shows up on the Android device, which is also the expected behavior. So far so good.
However, when I click on the notification in the notification channel, it brings the app to the foreground. However, when the app is launched like that - how does one retrieve the push notification payload? The onMessageReceived isn't triggered, and I'm having a hard time finding the answer to this in Android's documentation.
Any indication as to how to obtain that?
Thanks
Turns out this question provided the answer i was looking for. TL;DR - look in the extras field in the intent variable of the Activity
Firebase (FCM): open activity and pass data on notification click. android
Im not sure my solution below is best. But I think you can save the message to share preferences . And get it to another place it you want to you.
override fun onMessageReceived(p0: RemoteMessage) {
//TODO save it to share preferences
}
Give me another better solution if you find it. Thank you so much.

Display default notification in onMessageReceived

I'm using Firebase Cloud Messaging for Android. When my app is in the foreground, FCM will invoke onMessageReceived on my app's FirebaseMessagingService subclass.
When my app is in the background, the Android OS will create a default notification entry in the system tray. That notification entry looks pretty good to me; for the notifications I need to send, I don't particularly need to interrupt the user with the notification. The default notification in the system tray is just fine.
My question is, how do I make that "default" notification happen in onMessageReceived when my app is in the foreground? Is there a way to say, "I don't need to intercept this notification; please just do what you'd normally do if I were in the background"?
(Do I have to simulate it by hand with NotificationCompat.Builder? If so, which settings do I need to pass to get default behavior?)
When the app is in the background, your notifications are processed by the Google Services, which takes care of displaying your notifications as required, including the default click action (opening the app) and the notification icon.
When the app is in the foreground, the received messages are processed by the app.
Yes, you will have to mimic the NotificationCompat.Builder to look like default. There is no other way to do this without intercepting with onMessageReceived() callback.
Reference: https://firebase.google.com/docs/cloud-messaging/android/receive

Handling fcm notification when app not launched

I am using fcm push notification for my android app. I was able to display push notification on system tray when app is not launched. When I tap on the notification it opens the app launcher by default and I start an activity A from there. But the issue is, if I put the app to background and click on the app icon it again opens the app launcher rather than opening existing Activity A.
If the app process is killed, start the launcher activity. If the app is in the background, you can pass an intent to the notification which starts a DummyActivity that has no code on it, and immediatelly calls finish() on its onCreate() method. This will bring your app to the foreground.
Several things are not clear in your question. For example: How you send messages (from developer console or through rest api post requests to firebase backend)? What is your desired behaviour for app when push messages come? I will try to give you general answer that probably helps you to address issue and understand how to implement desired behaviour.
In any case, there are two types of Firebase push messages:
data messages
notification messages
more details about it check on Notification & data messages page
If you want to send additional details to activity that you are starting (something similar to bundle extras), you should use data messages and handle those in your service that extends FirebaseMessagingService by overriding onMessageReceived(RemoteMessage remoteMessage) method. This method is preferable for me because it is much more flexible. You can define all the details about showing notification based on received firebase message, including if notifications are bundled, what happens in details when user click notification and almost everything related to it.
If you don't need to start certain activity with some parameters, than you can use push messages and just define click_action. This method allows you to add define title, text and sound of notification (beside some other details) but it is not as flexible as if you send data messages
Here you can find detailed overview of possible parameters that you can use for different type of messages
Hope this helps

How to receive GCM message when app is closed or in background?

When the App is in foreground, the GcmListenerService will be called and I can do performance here. But what if the app is in background? When the app is in background, I can still receive the notification but it is just the notification part without data part. How can I customize the on receive event to receive a GCM message when my app is in background or even closed??
the onMessageReceived() method will not be called if the app is in background. Is there any other method works when app is in background?
https://github.com/google/gcm/issues/63
i have the same problem with him. And in this link there is a solution. When app is in the background, need server side to make a action. The data part will automatically saved in a intent and send to the activity contains that action in the content filter.

Handling Parse Push Notifications in Android

I am using Parse API in order to handle push notifications. In our Android application, I want to accomplish two things:
1) If we have received a Push Notification with the application is closed and the user clicks on the notification, I want to be able to understand that the application is being opened via a push notification.
2)If we receive a push notification while the application is open, I want to handle this and do some extra work.
In both cases, I want to be aware that the application has received a push notification in order to execute some special operations.
As far as I understand from Parse API documentations, it offers two methods of handling pushes: Responding with an Activity and Responding with an Intent. I am currently calling
PushService.setDefaultPushCallback(context, MainActivity.class);
in my Application class with needed changes in the AndroidManifest.xml file and already receive push notifications, this corresponds to Responding with an Activity method. But I don't know how to be aware of Push Notifications explicity with this method.
Thanks in advance.
When a push is received ,Check
1:Whether our application is in foreground or background.
If it is foreground, that means app is visible and do your stuff(show alerts or anything you want).
If app is in background,that means it is not visible and if you want to do any thing based on this.
i hope this helps..

Categories

Resources