Download - FCM & GCM
![Page 1: FCM & GCM](https://reader036.vdocuments.pub/reader036/viewer/2022062306/587201441a28ab176b8b51f5/html5/thumbnails/1.jpg)
FCM & GCM
Weihung 2016/06
![Page 2: FCM & GCM](https://reader036.vdocuments.pub/reader036/viewer/2022062306/587201441a28ab176b8b51f5/html5/thumbnails/2.jpg)
About Name:Weihung Liu Android develop more than 5 years Innooz co, Ltd ( 英諾奧茲) FB:Android 開發者讀書會 板主 FB:Weihung.TW
![Page 3: FCM & GCM](https://reader036.vdocuments.pub/reader036/viewer/2022062306/587201441a28ab176b8b51f5/html5/thumbnails/3.jpg)
Agenda What is FCM Firebase Notification FCM & FN Dependencies Apply plugin Permissions Receiver Listener service InstanceIDListenerService Remove registration Subscribe to topic Server endpoints Firebase notification
![Page 4: FCM & GCM](https://reader036.vdocuments.pub/reader036/viewer/2022062306/587201441a28ab176b8b51f5/html5/thumbnails/4.jpg)
What is FCMGoogle Cloud Message say: Firebase Cloud Message(FCM) is the new version of GCM.
![Page 5: FCM & GCM](https://reader036.vdocuments.pub/reader036/viewer/2022062306/587201441a28ab176b8b51f5/html5/thumbnails/5.jpg)
Firebase Notifications Firebase Notifications is a lightweight,
serverless messaging solution built on Firebase Cloud Messaging.
With a user-friendly graphical console and reduced coding requirements.
Lets users easily send messages to reengage and retain users.
![Page 6: FCM & GCM](https://reader036.vdocuments.pub/reader036/viewer/2022062306/587201441a28ab176b8b51f5/html5/thumbnails/6.jpg)
FCM & FN
https://firebase.google.com/support/faq/#messaging-difference
![Page 7: FCM & GCM](https://reader036.vdocuments.pub/reader036/viewer/2022062306/587201441a28ab176b8b51f5/html5/thumbnails/7.jpg)
Android Dependencies Before
compile "com.google.android.gms:play-services-gcm:8.4.0”
Aftercompile "com.google.firebase:firebase-messaging:9.0.2”
![Page 8: FCM & GCM](https://reader036.vdocuments.pub/reader036/viewer/2022062306/587201441a28ab176b8b51f5/html5/thumbnails/8.jpg)
Apply plugin Add in the end of build.gradleapply plugin: 'com.google.gms.google-services'
![Page 9: FCM & GCM](https://reader036.vdocuments.pub/reader036/viewer/2022062306/587201441a28ab176b8b51f5/html5/thumbnails/9.jpg)
Android Permissions Before
<uses-permission android:name="android.permission.WAKE_LOCK" /><permission android:name="<your-package-name>.permission.C2D_MESSAGE" android:protectionLevel="signature" /><uses-permission android:name="<your-package-name>.permission.C2D_MESSAGE" />
AfterNo permissions
![Page 10: FCM & GCM](https://reader036.vdocuments.pub/reader036/viewer/2022062306/587201441a28ab176b8b51f5/html5/thumbnails/10.jpg)
Android Receiver Before
<receiver android:name="com.google.android.gms.gcm.GcmReceiver" android:exported="true" android:permission="com.google.android.c2dm.permission.SEND" > <intent-filter> <action android:name="com.google.android.c2dm.intent.RECEIVE" /> <category android:name="com.example.gcm" /> </intent-filter></receiver>
AfterNo receiver
![Page 11: FCM & GCM](https://reader036.vdocuments.pub/reader036/viewer/2022062306/587201441a28ab176b8b51f5/html5/thumbnails/11.jpg)
Android Listener service A service extending
InstanceIDListenerService is now required only if you want to access the FCM token.
Manage device tokens to send a messages to single device directly
Send messages to device group Subscribe devices to topics with the
server
![Page 12: FCM & GCM](https://reader036.vdocuments.pub/reader036/viewer/2022062306/587201441a28ab176b8b51f5/html5/thumbnails/12.jpg)
Android InstanceIDListenerService Before
public class MyInstanceIDListenerService extends InstanceIDListenerService { ... @Override public void onTokenRefresh() { Intent intent = new Intent(this, RegistrationIntentService.class); startService(intent); }}
![Page 13: FCM & GCM](https://reader036.vdocuments.pub/reader036/viewer/2022062306/587201441a28ab176b8b51f5/html5/thumbnails/13.jpg)
Android InstanceIDListenerService After
public class MyInstanceIDListenerService extends FirebaseInstanceIdService { ... @Override public void onTokenRefresh() { String refreshedToken = FirebaseInstanceId.getInstance().getToken(); sendRegistrationToServer(refreshedToken); }}
![Page 14: FCM & GCM](https://reader036.vdocuments.pub/reader036/viewer/2022062306/587201441a28ab176b8b51f5/html5/thumbnails/14.jpg)
Android Remove registration
You no longer need to explicitly initiate the generation of a registration token — the library does this automatically
BeforeInstanceID instanceID = InstanceID.getInstance(this);String token = instanceID.getToken(getString(R.string.gcm_defaultSenderId), GoogleCloudMessaging.INSTANCE_ID_SCOPE, null);
AfterString token = FirebaseInstanceId.getInstance().getToken();
![Page 15: FCM & GCM](https://reader036.vdocuments.pub/reader036/viewer/2022062306/587201441a28ab176b8b51f5/html5/thumbnails/15.jpg)
Android Listener service Before
<service android:name=".MyInstanceIDListenerService" android:exported="false"> <intent-filter> <action android:name="com.google.android.gms.iid.InstanceID" /> </intent-filter></service>
After<service android:name=".MyInstanceIDListenerService"> <intent-filter> <action android:name="com.google.firebase.INSTANCE_ID_EVENT" /> </intent-filter></service>
![Page 16: FCM & GCM](https://reader036.vdocuments.pub/reader036/viewer/2022062306/587201441a28ab176b8b51f5/html5/thumbnails/16.jpg)
Android Listener service Before
public class MyGcmListenerService extends GcmListenerService { @Override public void onMessageReceived(String from, Bundle data){ ... }}
![Page 17: FCM & GCM](https://reader036.vdocuments.pub/reader036/viewer/2022062306/587201441a28ab176b8b51f5/html5/thumbnails/17.jpg)
Android Listener service After
public class MyFcmListenerService extends FirebaseMessagingService { @Override public void onMessageReceived(RemoteMessage message){ String from = message.getFrom(); Map data = message.getData(); } ...}
![Page 18: FCM & GCM](https://reader036.vdocuments.pub/reader036/viewer/2022062306/587201441a28ab176b8b51f5/html5/thumbnails/18.jpg)
Android Subscribe to topic Before
// Blocking methods. Execute them inside an AsyncTask or background thread.GcmPubSub.getInstance(context).subscribe("token", "/topics/mytopic", null /* extras bundle */);GcmPubSub.getInstance(context).unsubscribe("token", "/topics/mytopic");
After// Non-blocking methods. No need to use AsyncTask or background thread.FirebaseMessaging.getInstance().subscribeToTopic("mytopic");FirebaseMessaging.getInstance().unsubscribeToTopic("mytopic");
![Page 19: FCM & GCM](https://reader036.vdocuments.pub/reader036/viewer/2022062306/587201441a28ab176b8b51f5/html5/thumbnails/19.jpg)
Server endpoints Before
gcm-http.googleapis.com/gcm/ gcm-xmpp.googleapis.com
After fcm.googleapis.com/fcm/ fcm-xmpp.googleapis.com
![Page 20: FCM & GCM](https://reader036.vdocuments.pub/reader036/viewer/2022062306/587201441a28ab176b8b51f5/html5/thumbnails/20.jpg)
Firebase notification
![Page 21: FCM & GCM](https://reader036.vdocuments.pub/reader036/viewer/2022062306/587201441a28ab176b8b51f5/html5/thumbnails/21.jpg)
Firebase notification
![Page 22: FCM & GCM](https://reader036.vdocuments.pub/reader036/viewer/2022062306/587201441a28ab176b8b51f5/html5/thumbnails/22.jpg)
Firebase notification
![Page 23: FCM & GCM](https://reader036.vdocuments.pub/reader036/viewer/2022062306/587201441a28ab176b8b51f5/html5/thumbnails/23.jpg)
Firebase notification
![Page 24: FCM & GCM](https://reader036.vdocuments.pub/reader036/viewer/2022062306/587201441a28ab176b8b51f5/html5/thumbnails/24.jpg)
Firebase notification
![Page 25: FCM & GCM](https://reader036.vdocuments.pub/reader036/viewer/2022062306/587201441a28ab176b8b51f5/html5/thumbnails/25.jpg)
Q&A