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.
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
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
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.
When I open the app and send notification from the Firebase console , the onMessageReceived works fine. The app has its small icon , Notification sound ,etc.. But when the app is killed or is closed , no notification sound or a small icon appears.
After GCM the google changed the GCM as FCM, And FCM are containing two type of payload one is Notification and another One is Data payload when you are posting notification with Notification Payload then app will be in background then onMessageReceived not called but when in foreground onMessageReceived called, But if you put Data payload then all-time onMessageReceived is called.
Using Notification Payload
onMessageReceived called in the only foreground not in background
Using Data Payload
onMessageReceived called every time
I'm just going through the concept options of the Google Cloud Messaging System https://developers.google.com/cloud-messaging/concept-options and I saw this:
App behavior when receiving messages that include both notification and data payloads
depends on whether the app is in the background, or the foreground —essentially,
whether or not it is active at the time of receipt.
- When in the background, apps receive the notification payload in the notification tray,
and only handle the data payload when the user taps on the notification.
- When in the foreground, your app receives a bundle with both payloads available.
Is it possible to send a push message to specific android device and then verify its content by the app's logic?
And then if the content is verified by the app and it's correct - notify the user, in other case stay silent, dismiss the push msg and don't even show the notification icon?
Thanks!
You have to track the gcm type, for what such gcm gets called, depend on that you can show & hide notifications.
To show notification to user for push the control is in your hand only. Just apply the check that you want to and if the condition satisfies only then generate the notification.