Firebase Cloud Messaging

suggest change

First of all you need to setup your project adding Firebase to your Android project following the steps described in this topic.

Set up Firebase and the FCM SDK

Add the FCM dependency to your app-level build.gradle file

dependencies {
 compile 'com.google.firebase:firebase-messaging:11.0.4'
}

And at the very bottom (this is important) add:

// ADD THIS AT THE BOTTOM
apply plugin: 'com.google.gms.google-services'

Edit your app manifest

Add the following to your app’s manifest:

For example:

<service
    android:name=".MyInstanceIdListenerService">
    <intent-filter>
        <action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
    </intent-filter>
</service>
<service
    android:name=".MyFcmListenerService">
    <intent-filter>
        <action android:name="com.google.firebase.MESSAGING_EVENT" />
    </intent-filter>
</service>

Here are simple implementations of the 2 services.

To retrieve the current registration token extend the FirebaseInstanceIdService class and override the onTokenRefresh() method:

public class MyInstanceIdListenerService extends FirebaseInstanceIdService {

    // Called if InstanceID token is updated. Occurs if the security of the previous token had been
    // compromised. This call is initiated by the InstanceID provider.
    @Override
    public void onTokenRefresh() {
        // Get updated InstanceID token.
        String refreshedToken = FirebaseInstanceId.getInstance().getToken();

        // Send this token to your server or store it locally
    }
}

To receive messages, use a service that extends FirebaseMessagingService and override the onMessageReceived method.

public class MyFcmListenerService extends FirebaseMessagingService {
    
    /**
     * Called when message is received.
     *
     * @param remoteMessage Object representing the message received from Firebase Cloud Messaging.
     */
    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        String from = remoteMessage.getFrom();

        // Check if message contains a data payload.
        if (remoteMessage.getData().size() > 0) {
            Log.d(TAG, "Message data payload: " + remoteMessage.getData());
            Map<String, String> data = remoteMessage.getData();
        }

        // Check if message contains a notification payload.
        if (remoteMessage.getNotification() != null) {
            Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
        }

        // do whatever you want with this, post your own notification, or update local state
    }

in Firebase can grouped user by their behavior like “AppVersion,free user,purchase user,or any specific rules” and then send notification to specific group by send Topic Feature in fireBase.

to register user in topic use

FirebaseMessaging.getInstance().subscribeToTopic("Free");

then in fireBase console, send notification by topic name

More info in the dedicated topic Firebase Cloud Messaging.

Feedback about page:

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


Firebase:
* Firebase Cloud Messaging

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