FCM & GCM
-
Upload
weihung-liu -
Category
Software
-
view
409 -
download
0
Transcript of FCM & GCM
FCM & GCM
Weihung 2016/06
About Name:Weihung Liu Android develop more than 5 years Innooz co, Ltd ( 英諾奧茲) FB:Android 開發者讀書會 板主 FB:Weihung.TW
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
What is FCMGoogle Cloud Message say: Firebase Cloud Message(FCM) is the new version of GCM.
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.
FCM & FN
https://firebase.google.com/support/faq/#messaging-difference
Android Dependencies Before
compile "com.google.android.gms:play-services-gcm:8.4.0”
Aftercompile "com.google.firebase:firebase-messaging:9.0.2”
Apply plugin Add in the end of build.gradleapply plugin: 'com.google.gms.google-services'
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
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
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
Android InstanceIDListenerService Before
public class MyInstanceIDListenerService extends InstanceIDListenerService { ... @Override public void onTokenRefresh() { Intent intent = new Intent(this, RegistrationIntentService.class); startService(intent); }}
Android InstanceIDListenerService After
public class MyInstanceIDListenerService extends FirebaseInstanceIdService { ... @Override public void onTokenRefresh() { String refreshedToken = FirebaseInstanceId.getInstance().getToken(); sendRegistrationToServer(refreshedToken); }}
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();
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>
Android Listener service Before
public class MyGcmListenerService extends GcmListenerService { @Override public void onMessageReceived(String from, Bundle data){ ... }}
Android Listener service After
public class MyFcmListenerService extends FirebaseMessagingService { @Override public void onMessageReceived(RemoteMessage message){ String from = message.getFrom(); Map data = message.getData(); } ...}
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");
Server endpoints Before
gcm-http.googleapis.com/gcm/ gcm-xmpp.googleapis.com
After fcm.googleapis.com/fcm/ fcm-xmpp.googleapis.com
Firebase notification
Firebase notification
Firebase notification
Firebase notification
Firebase notification
Q&A