When sending notification through Firebase API calls onMessageReceived() even when the app is not running or killed but when sending through Firebase Console does not invoke onMessageReceived() when app is killed ?
FCM provide two types of messages .
Notification messages -sometimes thought of as "display messages." These are handled by the FCM SDK automatically.
2.Data messages-which are handled by the client app.
Notification messages are delivered to the notification tray when the app is in the background. For apps in the foreground, messages are handled by onMessageReceived():
For more information read FCM messeges
You have to add some key-value pairs as a data payload from the Firebase Console. while composing message for notification you have to select Advanced options and then add some data as a key value.
Once the app will get data notification, then the notification will be displayed in both the modes(background/killed and foregroud).
Inside Firebase console(Compose message) :
Advances options --> Add custom data(with key and value) --> Send
Related
I have an android app which has a separate activity for notifications. I integrated firebase cloud messaging to my app for sending notifications to users. When I send a notification from the console, by default a notification pops up from the app. I want disable that notification and save the message from notification to a database. Later when user opens the app it gets the notifications data from the database and show them to the user. That means I don't want to show popup notifications to user, I want to show them only if user opens the app. My question is: How can I disable the default notification which pops up when a message is received?
If you want to send a message without a notification, you should not use the Firebase console to send that message. Instead, you will need to write some code on your desktop or backend, usually with the Firebase Admin SDK, to send a message with a data payload. The app will silently receive that message, and you can decide what to do with it from the callback you receive. Start with the instructions in the documentation to set up the client app. Be sure you are sending messages with only a data payload. It must not contain a notification payload.
I am trying to send push notification to both android and ios. On Android push notification is handled by data while on ios notification is required in payload to display the notification.
But if notification part is added and if user is subscribed to the channel on both the device. On Android it show 2 messages created by notification part of payload.
Any particular way to send the payload from firebase. So that it can run on both the devices.
Thanks in advance
Like Frank said, Firebase does support sending messages to both iOS and Android.
If you send a notification message with a data payload (so both notification and data) the Android client will either display the auto generated notification based on the notification payload if the app is in the background or be passed to the onMessageReceived callback where you have the option to generate a notification based on the data and/or notification payloads if you wish if the app is in the foreground. There will not be two notifications generated.
I've implemented Firebase Cloud Messaging in my android app.
Is it possible to turn notifications off from my application settings screen?
It was possible with old GCM as we have broadcast GcmReceiver and we could handle it ourselves.
Now we have only FirebaseInstanceIdService and could handle it only when app is in foreground.
How we can handle it when app is in background?
Is it possible only to disable Firebase Cloud Messaging only for some category of notification inside app or this should be done on the server side?
You can manage whether or not notifications are displayed by always sending data messages from your app server. The Firebase console always sends notification messages, which are the types of messages that generate notifications automatically when your app is in the background. So if you want full control of when notifications are displayed use data messages which are only available from your app server, not yet from the Firebase console. See more on message types here.
I came with the solution to send request to the server if I need to switch on/off some type of notifications. And if send request to switch off some notification server will no longer trigger FCM server to send notification to the device
I am trying to migrate to Firebase cloud messaging from GCM and I noticed that when the application is not running the notifications are coming in the notifications tray. With GCM this was not the case, it was up to the developer to show a notification or not. I want to have similar behavior with FCM where when the app is running I want to silently handle the push message instead of having user to click on the notification and start the app. How can I achieve that.
Thanks,
P
Firebase Cloud Messages has two types of messages:
Notification Message: this type of message has the same behavior as the GCM messages
FCM automatically displays the message to end-user devices on behalf of the client app. Notification messages have a predefined set of user-visible keys
Data Message: this type has the behavior you described above.
Client app is responsible for processing data messages.
Data messages have only custom key-value pairs.
To send data messages you need to use the HTTP API [for Data Message]. (quoted from Arthur)
Full documentation: https://firebase.google.com/docs/cloud-messaging/concept-options
Is it possible to send data payload through Firebase Console that we can receive in onMessageReceived() while our app is in background/killed?
It doesn't seem like the Firebase Console has the option to send "data" type messages. If you set the custom data fields in the Firebase Console, those will be delivered but only if your app is in the foreground. You need to implement your own FCM sender or use a 3rd party to send data type messages.
See this answer for more details about the two types of Firebase messages
Yes! but :)
When your application is in the background notification messages (which is what is sent from the console) are automatically displayed in the system tray. When the user taps on the notification the custom data is made available in the intent of the Activity that is launched.
Note that if the user never taps the notification there is no way to get at that data, so this data should not be critical for your application to function but should be used to enhance the user's experience once they have tapped the notification.
As you may have realised if your app is in the foreground, onMessageReceived is called and you can retrieve the custom data there.