Demo of FCM based notifications

suggest change

This example shows how to use the Firebase Cloud Messaging(FCM) platform. FCM is a successor of Google Cloud Messaging(GCM). It does not require C2D_MESSAGE permissions from the app users.

Steps to integrate FCM are as follows.

  1. Create sample hello world project in Android Studio Your Android studio screen would look like the following picture.
  2. Next step is to set up firebase project. Visit https://console.firebase.google.com and create a project with an identical name, so that you can track it easily.
  3. Now it is time to add firebase to your sample android project you have just created. You will need package name of your project and Debug signing certificate SHA-1(optional).
a. Package name - It can be found from the android manifest XML file.

b. Debug signing SHA-1 certificate - It can be found by running following command in the terminal.

keytool -list -v -keystore ~/.android/debug.keystore -alias androiddebugkey -storepass android -keypass android

Enter this information in the firebase console and add the app to firebase project. Once you click on add app button, your browser would automatically download a JSON file named “google-services.json”.

  1. Now copy the google-services.json file you have just downloaded into your Android app module root directory.
  2. Follow the instructions given on the firebase console as you proceed ahead.
a. Add following code line to your project level build.gradle

`dependencies{
    classpath 'com.google.gms:google-services:3.1.0' .....`

b. Add following code line at the end of your app level build.gradle.
    
        //following are the dependencies to be added
        compile 'com.google.firebase:firebase-messaging:11.0.4'
        compile 'com.android.support:multidex:1.0.1'
    }
    // this line goes to the end of the file
    apply plugin: 'com.google.gms.google-services'

c. Android studio would ask you to sync project. Click on Sync now.
  1. Next task is to add two services.
a. One extending FirebaseMessagingService with intent-filter as following

        <intent-filter>
            <action android:name="com.google.firebase.MESSAGING_EVENT"/>
        </intent-filter>
b. One extending FirebaseInstanceIDService.
    
    <intent-filter>
        <action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
    </intent-filter>
  1. FirebaseMessagingService code should look like this.
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;

import com.google.firebase.messaging.FirebaseMessagingService;

public class MyFirebaseMessagingService extends FirebaseMessagingService {
    public MyFirebaseMessagingService() {
    }
}
  1. FirebaseInstanceIdService should look like this.
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;

import com.google.firebase.iid.FirebaseInstanceIdService;

public class MyFirebaseInstanceIDService extends FirebaseInstanceIdService {
    public MyFirebaseInstanceIDService() {
    }
}
  1. Now it is time to capture the device registration token. Add following line of code to MainActivity’s onCreate method.
String token = FirebaseInstanceId.getInstance().getToken();
Log.d("FCMAPP", "Token is "+token);
  1. Once we have the access token, we can use firebase console to send out the notification. Run the app on your android handset.

Click on Notification in Firebase console and UI will help you to send out your first message. Firebase offers functionality to send messages to single device(By using the device token id we captured) or all the users using our app or to specific group of users. Once you send your first message, your mobile screen should look like following.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:


Firebase:
* Demo of FCM based notifications

Table Of Contents
2 Gradle
5 Intent
17 Service
19 WebView
31 SQLite
35 Glide
37 Dialog
38 ACRA
44 Handler
53 Toast
63 Menu
65 Picasso
70 Volley
71 Widgets
78 Realm
90 Spinner
95 OkHttp
99 Firebase
108 TextView
109 ListView
111 Loader
118 Xposed
119 Security
121 ImageView
123 Doze Mode
130 Drawables
131 Colors
134 Fresco
139 AdMob
145 Keyboard
146 Button
150 EditText
155 Vk SDK
163 ExoPlayer
169 XMPP
175 OpenCV
177 Threads
184 ORMLite
186 TabLayout
190 LruCache
192 Zip files
194 Fastlane
199 FileIO
202 Moshi
210 VideoView
216 Paint
218 ProGuard
226 CleverTap
228 ADB shell
229 Ping ICMP
230 AIDL
234 Context
240 JCodec
242 Okio
249 FuseView
254 Looper
261 Fastjson
263 Jackson
267 Smartcard