Firebase display automatic notification even when all overrided - android

I use Firebase Cloud Messaging in my app, and I'm able to receive remote messages, with the data and everything I need from it. But even when I do nothing with the remote message, a notification appears automatically, with the content of the message.
public class MyFirebaseMessagingService extends FirebaseMessagingService {
#Override
public void onMessageReceived(RemoteMessage remoteMessage) {
// This line is executed, with the full message
}
}
With this code, the Android notification is still triggered, and I don't want it. I did not setup a default channel, and even on Android 8 (where I need to create a channel to send notification), there is one by default.
How can I remove this notification?
I want to display a notification only on my own terms, not on Firebase's ones

Don't use Notification message use data message instead.
in the Notification message, firebase will always show a notification when your app is in the background and there is no way to prevent it.
so the only way is to use data message and handle the notification by yourself.
from: https://firebase.google.com/docs/cloud-messaging/concept-options#notifications_and_data_messages
Notification message: 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 and an optional data
payload of custom key-value pairs.
Data message: Client app is responsible for processing data messages.
Data messages have only custom key-value pairs.
in all of my apps, I only use Notification message for very simple promotions because of lack of control and customization.

Related

FCM, how to allow messages from API's and avoid from Console

FCM message can be pushed from console and using api
from console (https://console.firebase.google.com)
using api (https://fcm.googleapis.com/fcm/send)
In both case when app is in foreground, message is received in service that extends FirebaseMessagingService, in onMessageReceived we can filter requests by custom param in bundle, but when app is in background and message is sent from console, receiver is not called, and the push message is somehow added.
Is possible to handle this request?
When app is in background and you suppose to fire notification via console at that time notification will come in notification tray rather than going to your service.
That will mentioned in guidelines.
https://firebase.google.com/docs/cloud-messaging/android/receive
So if you want to still handle notifications then, you have to set proper payload for same. So that whenever user click on notification then it will react accordingly.
For disabling notification try following,
FirebaseInstanceId.getInstance().deleteInstanceId();
Firebase Cloud Messaging support two types of messages:
Notification messages, sometimes thought of as "display messages." [When the app is not active], these are handled by the FCM SDK automatically.
Data messages, which are handled by the client app.
The Firebase console always sends notification messages, which are (when the app is not active) handled by the system. There is no way to intercept these messages in that case.
If you want to always receive the message in your application code, you should use a data message, which can only be sent using the API.
Yes, it is possible to handle messages when app is in foreground/background. In both cases you can handle it by implementing onMessageReceived. The only difference is, You have to put data in your JSON Message. Here you can find more.
#Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Map<String, String> data = remoteMessage.getData();
String myKey = data.get("myKey");
}

How to make a GCM / FCM notification-type message non-collapsible

Google Cloud Messaging (GCM) support two types of push messages: "notification" messages and "data" messages. According to the documentation, notification messages are collapsible by default, while data messages are non-collapsible by default.
In order to make a data message collapsible, you need to specifiy a collapseKey. My question is: How can you make a notification message non-collapsible?
Note: The question applies to Firebase Cloud Messaging (FCM) as well.
The message concepts and options documentation states:
messages are non-collapsible by default except for notification messages, which are always collapsible
But then later on the same page, it goes on to say:
except for notification messages, all messages are non-collapsible by default
Which is somewhat ambiguous. However, in the payload section, it states:
[notification messages] may have optional data payload. Always collapsible
Therefore, it doesn't seem possible to make notification messages non-collapsible.
I'd suggest that this is by design, because when creating notifications in Android, they are automatically replaced when another notification with the same ID is posted (similarly to how collapsing messages works). If I remember correctly, FCM/GCM uses the same ID for all notification messages.
Possible solution
If you do want a non-collapsible notification message, I'd suggest sending a data-only payload (with no notification or collapseKey), and then overriding the onMessageReceived() from the FirebaseMessagingService to create your own notification.
There is an example of this available on the Android quickstart sample:
#Override
public void onMessageReceived(RemoteMessage remoteMessage) {
// ...
// Check if message contains a data payload.
if (remoteMessage.getData().size() > 0) {
Log.d(TAG, "Message data payload: " + remoteMessage.getData());
// ...
}
// ...
// Also if you intend on generating your own notifications as a result of a received FCM
// message, here is where that should be initiated. See sendNotification method below.
}
The last comment there points you to the example sendNotification() method.
For your scenario, you'll need to pass a unique ID to the notificationManager.notify() call so that Android creates a new notification and does not replace any existing notifications - therefore, making the message non-collapsible.
First off, just going to put the FCM docs reference here:
A non-collapsible message denotes that each individual message is delivered to the device. A non-collapsible message delivers some useful content, as opposed to a collapsible message like a content-free "ping" to the mobile app to contact the server to fetch data.
As per your question -- "How can you make a notification message non-collapsible?" -- have you specifying a different collapse_key for each notification message you send?
A collapsible message works as if each of the message has the same collapse_key. But if you specify a different one for each message, then it won't be able to replace the previous one. This would make a notification message behave like a non-collapsible message.
However, since you are using a collapse_key, it is technically still treated as a collapsible message. Which means it is still a subject to the limit of four collapse keys at a time:
FCM allows a maximum of four different collapse keys per Android device to be used by the app server at any given time. In other words, the FCM server can simultaneously store four different collapsible messages per device, each with a different collapse key. If you exceed this number, FCM only keeps four collapse keys, with no guarantees about which ones are kept.
With all that said, any reason why you're not just using a data message payload? We use it on our app and I find it more flexible than a notification message.

Unable to send data message using firebase console

I'm using firebase console and can send only Notification messages using it.
Is there a way to send data messages using the same?
The Firebase Notifications Console can only be used to send notification messages. It cannot be used to send data messages.
See the table in message types in the Firebase documentation:
Notification message
Use scenario: 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.
How to send:
Use your app server and FCM server API: Set the notification key. May have optional data payload. Always collapsible.
Use the Notifications console: Enter the Message Text, Title, etc., and send. Add optional data payload by providing Custom data in the Notifications console. Always collapsible.
Data message
Use scenario: Client app is responsible for processing data messages. Data messages have only custom key-value pairs.
How to send:
Use your app server and FCM server API: Set the data key only. Can be either collapsible or non-collapsible.
You can test both notification message and data message using Postman(rest client for testing http request).See screen shots:
In header pass:
key:Content-Type, value:application/json
key:Authorization:key=<Server key>
Please look here: Firebase push notifications update DB, my post from June.
In conclusion, you need send HTTP POST request to https://fcm.googleapis.com/fcm/send
https://fcm.googleapis.com/fcm/send
Content-Type:application/json
Authorization:key=AIzaSyZ-1u...0GBYzPu7Udno5aA
{ "data": {
"score": "5x1",
"time": "15:10"
},
"to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1..."
}
You can now send notification message via the console. Note that it is different from data messages; notification messages only trigger the onMessageReceived callback when the app is in the foreground.
They are inside the advanced options tab on the compose message screen.
Just expand it and type your key/value map.
These will be included into the data field of the notification.

Android silent push notifications

We think that Android’s push notifications by default are silent notifications and must be the developer who programmatically raise the notification on the screen. Or the push notifications aren’t silent by default?
Today we work using this JSON structure which our app receives from the GCM server.
{
"data":
{
“Type” : “2”,
“_dId” : “3718829”,
“_mId” : “9924012”,
“_msg” : “HOLA JVE”,
“collapse_key” : “9924012”
},
"to" : "xxxxxxxxxxxxxxxxxxxxxxxxxxx"
}
Is this structure valid for silent notifications or there is a different one ?
Truly we are pretty lost on this topic. Following this question, anyone have a json structure of an Android push notification with message and title tags ?
Other question : When a silent notification is shown to the user ? When the user open the app or just when the user unlock the phone ?
Or the push notifications aren’t silent by default?
There are two ways that a notification is received in Android, either it's Notification Tray, or you handle it yourself in onMessageReceived() depending on which payload you use (Notification or Data).
Basing from your inquiry (silent push notification), I'm guessing you'd prefer the latter, since you'll be able to handle it yourself. As per the GCM Payload docs:
Use notifications when you want GCM to handle displaying a notification on your client app’s behalf. Use data messages when you want your app to handle the display or process the messages on your Android client app,..
So what you are currently using right now, (a data payload) should be fine. However, if you have both notification and data in your payload, you will have to consider your app's status. Referring to this FCM docs, for data payload:
App state Notification Data Both
Foreground onMessageReceived onMessageReceived onMessageReceived
Background System tray onMessageReceived Notification: system tray
Data: in extras of the intent.
..anyone have a json structure of an Android push notification with message and title tags ?
As also mentioned in the docs I linked above:
Data messages have only custom key/value pairs.
So I think it's safe for you to just use keys so long as it's not a reserved word, as mentioned in this docs:
The key should not be a reserved word ("from" or any word starting with "google" or "gcm"). Do not use any of the words defined in this table (such as collapse_key).
When a silent notification is shown to the user ? When the user open the app or just when the user unlock the phone ?
I think what I mentioned above pretty much covers this part (see the table).
On Android, differently from iOS, the app is responsible for creating and showing the push notification. So yes, you can think of them as silent by default, although you don't exactly have this concept on Android.
What I normally do is, if the notification should not be shown, add a silent field. For example:
{
"to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
"notification" : {
"body" : "Will not show this message",
"title" : "Portugal vs. Denmark",
"icon" : "myicon"
"silent": "true"
}
}
And then you can use the silent field to show or not show the notification according to an if statement.

Android gcm notification payload vs data payload?

According to
https://developers.google.com/cloud-messaging/server-ref
gcm message can have notification payload or/and data payload
What is the difference between these two?
Reading on the link you send it is explained under Payload section
Payload
Optional. If you are including a payload in the message, you use the
data parameter to include your custom key/value pairs. The client app
handles the data payload for display or other processing purposes.
The notification parameter with predefined options indicates that GCM
will display the message on the client app’s behalf if the client app
implements GCMListenerService on Android, or if the notification
message is sent to an iOS device. This applies for both HTTP and XMPP.
The app server can send a message including both notifications and
data payloads. In such cases, GCM handles displaying the notification
payload and the client app handles the data payload.
See the Server Reference for details on sending and receiving
messages.
You may find this explanation more helpful
Use scenario
Notification: GCM automatically displays the message to end user devices on behalf of the client app. Notifications have a pre-defined set of user-visible keys.
Data: Client app is responsible for processing data messages. Data messages have only custom key/value pairs.
How to send
Notification: Set notification payload. May have optional data payload. Always collapsible.
Data: Set data payload only. Can be either collapsible or non-collapsible.
https://developers.google.com/cloud-messaging/concept-options?hl=en

Categories

Resources