I have used this How to get custom data from android firebase notification? solution for getting custom data from Firebase and it work only if Application is open, when Application is closed the custom data return empty String ""
Another question related if remove the code for notification from:
public void onMessageReceived(RemoteMessage remoteMessage) {
and application is closed i recevived always a notification but if open not?
I think if Application is open use my code but if closed what code use?
The callback onMessageReceived is only called when the application is running ie its in foreground.
according to https://firebase.google.com/docs/cloud-messaging/android/receive
onMessageReceived is provided for most message types, with the following exceptions:
Notification messages delivered when your app is in the background.
In this case, the notification is delivered to the device’s system tray. A user tap on a notification opens the app launcher by default.
Messages with both notification and data payload, both background and
foreground:
In this case, the notification is delivered to the device’s system tray, and the data payload is delivered in the extras of the intent of your launcher Activity.
So the payload data you need to fetch using intent.getExtras()
Related
I have a scenario when i don't want android system to display push notification if app is in foreground.
Instead i want to display some view or launch activity in the app.
This is my code.
#Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
//...
//...
sendNotification(remoteMessage.getNotification(), subtitle);
}
If i comment out sendNotification()method, it does not display notification on both background & foreground cases.
There are two types of messages data messages and notification
messages. Data messages are handled here in onMessageReceived whether
the app is in the foreground or background. Data messages are the type
traditionally used with GCM. Notification messages are only received
here in onMessageReceived when the app is in the foreground. When the
app is in the background an automatically generated notification is
displayed. When the user taps on the notification they are returned to
the app. Messages containing both notification and data payloads are
treated as notification messages. The Firebase console always sends
notification messages.
Thought you can only handle notification when app is in foreground.
Solution:-
Tell your server that app is in foreground and background and they will send notification when app is in foreground only
Is it possible to read the push notification information (Both notification {} and data{} from FCM), without tapping/clicking on the notifications. ?
As for the the Documentation, Notification come your app, either it is in foreground or background, But in these case of background, a notification is delivered to the device's system tray, and the data payload is delivered in the extras of the intent of your launcher Activity.
But my requirement is to read the notification without clicking on it.
Is it possible..? if yes please give me some information / some links.
You can use FCM data message instead of notification, everytime you receive a new data message onMessageReceived will be called and you can choose either to show this notification or not, and will get all relevant data.
I have got notification from FCM. And i have opened the app from launcher without clicking the notification. So i dont get the notification data in activity. Is there a way to get notification data if user opens application from launcher without clicking notification?
It totally depends upon the app status to get notification data without clicking on Notification,
onMessageReceived is provided for most message types, with the following exceptions:
Notification messages delivered when your app is in the background.
In this case, the notification is delivered to the device’s system tray. A user tap on a notification opens the app launcher by default.
Messages with both notification and data payload, both background and foreground.
In this case, the notification is delivered to the device’s system tray, and the data payload is delivered in the extras of the intent of your launcher Activity.
I shared this data just to inform you that the answer provided by #Sagar Zala will not work when your app is in Background
FCM onMessageReceived Documentation
You can not directly get notification data if you opens app from launcher icon.
But in this case you can store notification data in SQLite or Shared Preference while notification arrive and use this data when you opens app from launcher icon.
Note: Clear notification data once app opens.
Thanks.
I have an android app connected to FCM(firebase cloud messaging) for sending notifications . However , when the app is in background , it doesn't apply
custom notifications. I think this is because it does not enter OnMessageReceived method . Whats the solution for this ? As i read somewhere it can be done using HTTP Api but i am not sure of how to go about it ? Please help.Thank you.
Whether the method onMessageReceived is called is dependent on the notification payload.
See 'Handling Messages' section in FCM docs here.
Handling messages
onMessageReceived is provided for most message types, with the following exceptions:
Notification messages delivered when your app is in the background. In this case, the notification is delivered to the device’s system tray. A user tap on a notification opens the app launcher by default.
Messages with both notification and data payload, both background and foreground. In this case, the notification is delivered to the device’s system tray, and the data payload is delivered in the extras of the intent of your launcher Activity.
In summary, if the app is in foreground, then onMessageReceived will always be called. If the app is in background, then onMessageReceived will only be called if the notification payload is data only.
So to answer your question, if you must have onMessageReceived every time regardless if the app is in foreground or background state, then set your notification payload to contain data only.
When i send a push notification while my app is running,my app crashes (via fire base console), but works just fine when my app is closed or running in the background.
Actually the problem is Getting in
class which is extends FirebaseMessagingService
FirebaseMessagingService this class is use to catch push notification.
so when your application is open you need to just check your code in
public void onMessageReceived(RemoteMessage remoteMessage)
Check your code in this method.
As per your second question when your application is closed or in background at the time OnMessageReceived() method is not handling a event of Push Notification
There are 2 main ways of getting a push notification from fire base
1) When application is in foreground (application is running or opened)
2) When application is in background (application is not running or closed)
In first case if you send push notification from fire base console the notification message and custom data payload is received in
public void onMessageReceived(RemoteMessage remoteMessage)
In short when application is in foreground onMessageReceived method will trigger always , notification data must be handled inside this method.
In second case if u send push notification from fire base console the notification message is received in system tray
and you can receive the data payload in the bundle extras of the launcher activity of your application.