A notorious common question is "how to send notifications from device to device", sadly the answer is: you can't. FCM needs to be triggered in order to send push notifications. That can be done in 3 different ways:
A push notification is an information payload that is sent from FCM. There are 3 types of push notifications: notification
, data
, notification and data
. This information can be represented as a JSON:
{
"to" : "APA91bHun4MxP5egoKMwt2KZFBaFUH-1RYqx...",
"notification" : {
"body" : "great match!",
"title" : "Portugal vs. Denmark",
"icon" : "myicon"
},
"data" : {
"Nick" : "Mario",
"Room" : "PortugalVSDenmark"
}
}
The above example is for the third type, notification
and data
combined. That is what will be asked to FCM to send.
notification
and notification
with data
but never only data
The importance of the notification
type is that allow applications to received default pushes empowering other teams such as marketing to increase application growth by simply using the web console without further coding needed beside adding the library to the project.
Please don't confuse the push notification, the notification
type and visual notification, this last correspond to an Android class (commonly NotificationCompat).
The behavior of the push is different according to the type and if the app is in the foreground or in background. Not on foreground means, minimized or closed.
notification
will trigger a default visual notification if the app is not in foreground, this notification can be customized in the manifest, please see documentation. If the app is in the foreground, we have to customize the behavior inside the onMessageReceived
method.data
type behavior must always be customized.notification
and data
if the app is not in the foreground will trigger default visual notification and data
payload will be available when the user click. Since the launcher Activity is triggered when the visual notification is clicked then you have to literally getIntent().getStringExtra("yourKey");
in that Activity to get the data. If the app is active (in the foreground), then you have to customize the behavior inside the onMessageReceived
method and you get access to the data
payload immediately.To get the information payload you have to do it inside the onMessageReceived
method, there the only available argument is the message:
notification
you have to remoteMessage.getNotification()
then you can get the body or title with the corresponding methodsdata
you have to remoteMessage.getData().get("yourKey")
.Is a good idea to add every not null verification, there will be several types of notifications arriving in advanced apps. A good strategy is to verify if each, the notification
and the data
are not null. A consequent usefull strategy will be to have always use a type
key in the data
notifications in order to do some flow control.
To send data
from the Firebase web console, the advanced options must be opened.
The notification
keys are limited, and indicated in the documentation. The values in any type can be only String.
If you have problems finding any documentation in Firebase, please go to the bottom of the page and change the language to "English", documentations are thinner in some other languages.
Firebase Cloud Messaging is the Firebase service that handles push notifications. You can add this service in any client: web, Android or IOS. The specific functioning for each must be read from the documentation.
For adding FCM in any type of project, is always adding a library.
Considering the special support for Android is worthy to take a few lines for it. Create a new project using Android Studio, in the menu go to Tools/Firebase, it will trigger the Firebase assistant. Select "Cloud Messaging" and follow steps one and two.
This is the basis for adding FCM in a project. From this point on, the client is already able to receive FCM push notifications that contain a "notification" payload as long as the app is not in foreground (more details in the remarks).
To further customize the FCM behavior in the client we need to add 2 services, this is well documented in the official site. Again we will take some consideration for Android:
FirebaseMessagingService
and override the onMessageReceived methodFirebaseInstanceIdService
and override the onTokenRefresh methodapplication
tag
</intent-filter>
</intent-filter>
You can get the notification
payload and the data
payload inside the onMessageReceived
method using the only argument there. The onTokenRefresh
method is called when the FCM token is assigned by FCM. An FCM token is a unique id for the app installation and the device and can be used as an address of the device to directly send push notifications.
Please read remarks for more information about the types of notification and the associated behavior.