Types of Message
Using Firebase Cloud Messaging, we can send three types of messages, i.e., Notification Message, Data Message, and the message with both Notification & Data Payload.
1) Notification Message
Firebase SDK has handled notification messages itself. Typically, the notification message includes the title, icon, message, etc. These messages can also be sent from the Firebase console UI. By sending this type of message, we will not have much control over the information. The notification will appear automatically when the app is in the background.
A notification is a message which Android shows outside of our app's UI for providing users with reminders, communication with other people, or other timely information about our app. Users can tap on the notification to open our app or take action directly from the notification.
The notification design is determined by the system template - the content for each part of the template is defined by our app. Some information on the notification only appears in the expanded view. The most common parts of notification are as follows:
2) Data Message
Data messages are handled by the Android app. If we want to send some additional data along with the information, then we can add such messages. But, it is not possible to send these messages through the Firebase console. To send notifications using the Firebase API, we must have server-side logic. We must use the data key while sending this message.
We can use data messages to send custom data elements to a client application. However, FCM places a 4KB limit on these data messages, so if our payload is greater than 4KB, we must obtain additional data using the WorkManager or JobScheduler API.
3) Messages with both Notification and Data payload
Both Notification and Data payload can also be contained in a message. Sending of these types of messages is handled in two scenarios depending upon the app state, i.e., background and foreground. We can use both the notification and data keys for these messages.
When the app state is in the background, the apps receive the notification payload when the user taps on the notification, and when in the foreground, the app receives a message object with both payloads available.
Now, we will create an Android project and add it with the Firebase either from Google Assistant or manually. After that, we will move to the FCM console (Firebase->Grow->Cloud Messaging).
Click on Send your first message.
After clicking on Send your first message, it will ask to fill a few of the fields such as notification title, notification text, notification image that is optional, and notification name, which is also optional.
After filling all the fields, click on Next.
After clicking on Next, it will ask for the Target either User segment or Topic. We will select the app for the target user and click on Next.
When we click on the Next, it will ask for the Scheduling and click on Next again.
After that, it will ask for the Conversion events. We can do this, but it is optional. So, we can ignore it and proceed further.
After that, it will ask for some additional options, which are also optional. These are notification channels, custom data, sound, and expires, etc. After that, we will click on the Review.
After publishing, we can see the notification in the console.
We can duplicate notification if we want to get insight on message delivery to our app. We can go to the reporting dashboard, which records a number of messages sent in the device. It also has some data for impressions notifications which are seen by users, but we will go through more of the notification composer.