Kotlin Firebase Cloud Messaging – How to Receive Messages | Android

Tags: kotlin, android, firebase, cloud
Published: 2021-05-22 05:34:33

https://grokonez.com/android/kotlin-firebase-cloud-messaging-receive-messages-android

Kotlin Firebase Cloud Messaging – How to Receive Messages | Android


Firebase Cloud Messaging (FCM) is a cross-platform messaging solution that help us deliver messages reliably at no cost. In this tutorial, we're gonna look at way to receive Messages in an Android App using FCM.

More Practice: <a href="https://grokonez.com/android/kotlin-firebase-cloud-messaging-subscribe-topic-receive-messages-android">Kotlin Firebase Cloud Messaging – How to Subscribe TOPIC & Receive Messages | Android</a>

<!--more-->
<h2>I. Firebase Cloud Messaging</h2>
<h3>1. Usage</h3>
With FCM, we can:
- notify a client that new data is available to sync.
- send notification messages to drive user re-engagement and retention.
- create instant messaging with a payload of up to 4KB to a client app.
<h3>2. How to receive Messages</h3>
<h4>2.1 Add Firebase to Android Project</h4>
- Create new Android App including Kotlin support.
- From menu bar, <strong>Tool</strong> -> <strong>Firebase</strong>:
<img src="https://grokonez.com/wp-content/uploads/2017/12/kotlin-firebase-cloud-messaging-setup-tool.png" alt="kotlin-firebase-cloud-messaging-setup-tool" width="211" height="199" class="alignnone size-full wp-image-9865" />

- Assistant window will be shown. Choose <strong>Cloud Messaging</strong>, then click on <strong>Setup Firebase Cloud Messaging</strong>:
<img src="https://grokonez.com/wp-content/uploads/2017/12/kotlin-firebase-cloud-messaging-setup-tool-2.png" alt="kotlin-firebase-cloud-messaging-setup-tool-2" width="417" height="237" class="alignnone size-full wp-image-9867" />

- At the step <strong>1</strong>, click on <strong>Connect to Firebase</strong>:
<img src="https://grokonez.com/wp-content/uploads/2017/12/kotlin-firebase-cloud-messaging-setup-tool-3.png" alt="kotlin-firebase-cloud-messaging-setup-tool-3" width="405" height="372" class="alignnone size-full wp-image-9868" />

- In new Window, choose <strong>Create new Project</strong> and fill in the name of Firebase Project:
<img src="https://grokonez.com/wp-content/uploads/2017/12/kotlin-firebase-cloud-messaging-setup-tool-4.png" alt="kotlin-firebase-cloud-messaging-setup-tool-4" width="677" height="515" class="alignnone size-full wp-image-9870" />

- Click on <strong>Connect to Firebase</strong>, the Browser will show new Firebase Project that has just been created:
<img src="https://grokonez.com/wp-content/uploads/2017/12/kotlin-firebase-cloud-messaging-setup-tool-console.png" alt="kotlin-firebase-cloud-messaging-setup-tool-console" width="700" height="482" class="alignnone size-full wp-image-9871" />

- Come back to Android Studio, at the step <strong>2</strong>, click on <strong>Add FCM to your app</strong>:
<img src="https://grokonez.com/wp-content/uploads/2017/12/kotlin-firebase-cloud-messaging-setup-tool-5.png" alt="kotlin-firebase-cloud-messaging-setup-tool-5" width="372" height="335" class="alignnone size-full wp-image-9872" />

- You will see a new window that shows the code will be add to <strong>build.gradle</strong> files. Click on <strong>Accept Changes</strong>:
<img src="https://grokonez.com/wp-content/uploads/2017/12/kotlin-firebase-cloud-messaging-setup-tool-6.png" alt="kotlin-firebase-cloud-messaging-setup-tool-6" width="507" height="443" class="alignnone size-full wp-image-9873" />

- Now view your Project under Project type, you can see <strong>google-services.json</strong> file:
<img src="https://grokonez.com/wp-content/uploads/2017/12/kotlin-firebase-cloud-messaging-setup-tool-7.png" alt="kotlin-firebase-cloud-messaging-setup-tool-7" width="257" height="495" class="alignnone size-full wp-image-9874" />
<h4>2.2 Check dependencies</h4>
Open Gradle file to check what happens, we can see that some code has been added:
- <strong>build.gradle</strong> (project-level):
<pre><code class="language-javascript">
buildscript {
    // ...
    dependencies {
        // ...
        classpath 'com.google.gms:google-services:3.1.0'
    }
}
</code></pre>
- <strong>build.gradle</strong> (App-level):

More at:

https://grokonez.com/android/kotlin-firebase-cloud-messaging-receive-messages-android

Kotlin Firebase Cloud Messaging – How to Receive Messages | Android