I have a problem that to handle FCM notification message.
I want to handle notification message without data fields.
So I've implement firebasemessagingservice in my project, but onMessageReceived function did not triggered.
Is it possible? Can I handle none data fields notification message in background state?
No, onMessageReceived only gets triggered in the background when you have a data payload, see the documentation for details.
If you only have a notification field, it will land in the system tray and be handled by system, if you still want to handle the notification yourself, change the message payload to data and just include the related notification information and push out a notification yourself, for more details, see documentation.
The documentation says you can do this with setBackgroundMessageHandler() which goes in the firebase-messaging-sw.js file.
I have never done it, but that is what the documentations says.
Related
While sending the notification and when my application is in the foreground, i am getting the notification with the action buttons in the notification, while when my application is killed or in background the notification is coming but without the action button, i am getting just the title and content body.
I have tried many solution but it does work. Is android have new permission issues are introduced?
Any help and suggestion will be appreciated.Here is the Image
As there's no any code provided I can't say what's the specific issue here, butt keep in mind. In FCM there are 2 types of notifications:
Notification payload
Data payload
When you send a notification from FCM, it is a notification payload. In this case if your app is killed, the notification will go directly to the system tray and will be handled by the OS. The onMessageReceived() won't execute.
If you send the notification from the backend side, it's a data payload. In that case the onMessageReceived() will be executed.
Check more here FCM Message Types
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 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.
I'm currently implementing a Chat application using Firebase Cloud Messaging to send push notifications. Using the notification field in the API call, Firebase displays them automatically without having to manually create a service and listen for the messages.
The push notification is pretty generic, just says "You have a new message!", so it doesn't make sense to keep adding new pushes every new message, I need firebase to not show a new push if the message body contents the same of a previous one.
Is it posible to do without implementing the service and handling Notification show manually? Found no references of this use case in the documentation.
If your application is in Foreground then you receive the notification in onReceive of your FirebaseMessagingService. Else notification is delivered to system notification tray. Since you don't know the id of the notification so you may not retrieve it. It is also possible that your application is not running at the time you receive the notification. So logically it is NOT possible even by implementing the service. Well you can solve the problem by another approach. You can use Firebase Database in conjuction with Cloud Functions. Just have a look on developer guide of cloud functions and you will find that they can help yourself achieve you what you want. As a solution skeleton : Post messages to Firebase Database and Send notification using cloud functions. When the recipient reads the message update the database to reflect message has been read like having a variable seen. Design your cloud function such that it reads the value of seen value of last message and sends notification only if it was true. Hope this helps.
For manual handling you have to use the service. Go for handleIntent(intent) of FirebaseMessagingService.
This method gets called when application is in foreground, background and killed state. To avoid the duplication, do not call super.handleIntent(intent). This will prevent the automatic push notification when app in BG or killed state.
This worked for me.
Is it possible/is there a reliable way to get a call back when a notification is received on the android platform? i.e. I want to record when the notification is received (not when it is later opened).
No, It's your app which receives the message so if you need things logged, then log it. Simply handle it as you like once you receive it. Note that if you use FCM with Notification message message type, then you will not be directly notified about message arrival when app is in background (notification will be automatically posted by Firebase - your app will get message payload once use taps the notification). In such case consider switching to Data messages so you will get it no matter your app is in foreground or not.
See docs: https://firebase.google.com/docs/cloud-messaging/downstream