Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Firebase Notifications

Andrew Kelly
November 15, 2016

Firebase Notifications

An overview of how to setup FIrebase Notifications on Android.

Andrew Kelly

November 15, 2016

Transcript

14 slides
  1. GCM & FCM • At Google I/O 2016 Google Cloud

    Messaging (GCM) became Firebase Cloud Messaging (FCM). • If you’ve used GCM before FCM will be very familiar to you, it’s very easy to get setup to add push notifications to your app. • Firebase Notifications sit on top of FCM to provide even easier push- setup. • Firebase Notification are part of the “Grow" section of the Firebase suite, and are ideally used for marketing purposes.
  2. Firebase Notifications • Notifications operate in 2 modes, that are

    handed differently depending on if your app is in the foreground or background. - Notification - Data
  3. Notification vs Data • Notification payloads "to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...", "notification"

    : { "body" : "great match!", "title" : "Portugal vs. Denmark" } • Data payloads "to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...", "data" : { "name" : "Mario", "comment" : "great match!” } • You can send Notification and Data payloads in the same message.
  4. onMessageReceived() • This callback is defined in a Service that

    you extend and add to your manifest. • It allows you to process a notification when your app is in the foreground (or background if sending Data payloads). • This callback won’t be called if your app is in the background and you send a Notification payload. • How do I know if my app is in the foreground? - Count the number of onStart/onStop calls. - Any number of other ways!
  5. AndroidManifest.xml <service
 android:name=".services.MyFirebaseTokenService"
 android:exported="false">
 <intent-filter>
 <action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
 </intent-filter>
 </service>
 


    <service
 android:name=".services.MyFirebaseMessagingService"
 android:exported="false">
 <intent-filter>
 <action android:name="com.google.firebase.MESSAGING_EVENT"/>
 </intent-filter>
 </service> <meta-data android:name="com.google.firebase.messaging.default_notification_icon" android:resource="@drawable/ic_notification" /> <meta-data android:name="com.google.firebase.messaging.default_notification_color" android:resource="@color/colorAccent" />
  6. MyFirebaseTokenService public class MyFirebaseTokenService extends FirebaseInstanceIdService {
 
 @Override
 public

    void onTokenRefresh() {
 // Get updated InstanceID token.
 String token = FirebaseInstanceId.getInstance().getToken();
 // Send this to the back end for use later.
 }
 
 }
  7. MyFirebaseMessagingService public class MyFirebaseMessagingService extends FirebaseMessagingService {
 
 @Override
 public

    void onMessageReceived(RemoteMessage remoteMessage) {
 // Check if message contains a data payload.
 if (remoteMessage.getData().size() > 0) {
 Log.d(TAG, "Data: " + remoteMessage.getData());
 }
 
 // Check if message contains a notification payload.
 if (remoteMessage.getNotification() != null) {
 Log.d(TAG, "Notification: " + remoteMessage.getNotification().getBody());
 } }
 }
  8. Testing • You can send test Notification payloads from the

    Firebase Console. • You can send test Notification + Data payloads from the Firebase Console • You CAN’T send Data only payloads from the Firebase Console. - Use the command line instead, if your app server isn’t ready yet. curl --header "Authorization: key=<insert your server key here>" --header Content-Type:"application/json" https://fcm.googleapis.com/fcm/send -d "{\"data\": {\"title\": \"Push it\",\"message\": \"Push it real good\"}, \"to\":\"<insert device token here>\"}"
  9. Recipients • Since Firebase Notifications are based upon FCM, at

    the end of the day you’re just sending out push notifications to a list of devices based on the firebase device token, or to devices registered against a topic. • The Firebase Console allows you to choose topic subscribers, or particular users as defined in Firebase Analytics, or an individual device. • Given the marketing idea behind Firebase Notifications, the analytics setup is the one Google is pushing. • As with GCM, you can also choose to send the Firebase registration token to your own backend server if you wish to send push messages programatically. • You can see how many people Firebase thinks will receive the notification in the console. • You can also see how users engaged with the notifications that were sent, received, viewed etc.

More Decks by Andrew Kelly

Other Decks in Technology