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

Mastering Firebase Cloud Messaging

Mastering Firebase Cloud Messaging

Firebase Notifications can be confusing, there are different ways to implement them and not all behave the same. If you want to bring a great user experience to Android, iOS and browsers the one-size-fits-all solution from the tutorials won’t be enough for you, instead, you will need to dig deeper into the API to get the best of it. We went through that learning process and in this talk you will learn our tips and tricks.

72ffb135de71bef2c4a11961634edc6a?s=128

Miguel Beltran

November 04, 2017
Tweet

Transcript

  1. MASTERING FIREBASE CLOUD MESSAGING @Miqubel by Miquel Beltran Android Developer

    @ Nebenan.de
  2. “cross-platform messaging solution that lets you reliably deliver messages at

    no cost.” What is Firebase Cloud Messaging? @Miqubel
  3. Key capabilities: • Notifications or Data messages (4KB) • Versatile

    targeting • Upstream messages @Miqubel
  4. @Miqubel Notification vs. Message • Push Notification? • Notification? •

    Cloud Message?
  5. The idea @Miqubel

  6. The reality @Miqubel

  7. Notifications console @Miqubel

  8. A/B Testing with the console • Presented at Firebase Dev

    Submit 2017 • Multiple audience target • Different content / parameters • Check results per audience @Miqubel
  9. @Miqubel

  10. Pushing manually curl https://fcm.googleapis.com/fcm/send -X POST \ --header "Authorization: key=long

    auth key" \ --Header "Content-Type: application/json" \ -d ' { "to": "the device token" "notification":{ "title":"New Notification!", "body":"Test" }, "priority":10 }' val token = FirebaseInstanceId.getInstance().token Log.d("Firebase", "My token: $token") @Miqubel
  11. Token problem • Where to store the token? • More

    than one token per user? • What happens when the user logs out? • Keep sending emails? @Miqubel
  12. Notification vs. Data @Miqubel

  13. Notifications in foreground class NotificationService : FirebaseMessagingService() { override fun

    onMessageReceived(remoteMessage: RemoteMessage) { val notification = NotificationCompat.Builder(this,”cid") .setContentTitle(remoteMessage.notification.title) .setContentText(remoteMessage.notification.body) .setSmallIcon(R.mipmap.ic_launcher) .build() val manager = NotificationManager… manager.notify(123, notification) } } @Miqubel
  14. Notification Payload • “icon”: Android & iOS • “sound”: only

    Android • “badge”: only iOS • “click_action”: Android & iOS "notification":{ "title": "New Notification!", "body": "Test" }, @Miqubel
  15. Notifications & Click Actions "notification":{ "title": "New Notification!", "body": "Test",

    "click_action": "OPEN_FRIEND_LIST" }, @Miqubel <intent-filter> <action android:name=“OPEN_FRIEND_LIST” /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter>
  16. Data Payload “notification":{ "title": "New Notification!", "body": “Test" }, @Miqubel

  17. Data Payload "data":{ "title": "New Notification!", "body": “Test" }, @Miqubel

  18. Data Payload "data":{ "title": "New Notification!", "body": "Test", "user_id": "1234",

    "user_avatar": "http://example.com/users/1234/avatar.jpg", "notification_type": "private_message" }, @Miqubel
  19. Notifications with data class NotificationService : FirebaseMessagingService() { override fun

    onMessageReceived(remoteMessage: RemoteMessage) { val notification = NotificationCompat.Builder(this,”cid") .setContentTitle(remoteMessage.data["title"]) .setContentText(remoteMessage.data["body"]) .setSmallIcon(R.mipmap.ic_launcher) .build() val manager = NotificationManager… manager.notify(123, notification) } } @Miqubel
  20. Missing Notifications? “The reason is that phone manufacturers like Oppo,

    Xiaomi, and One Plus use a stock ROM that disables the re-starting of background services for most apps.” Neil Mathew: Why your Push Notifications never see the light of day @Miqubel
  21. Web: Asking permission messaging.requestPermission() @Miqubel

  22. @Miqubel

  23. Web: Notification vs Data @Miqubel

  24. Web: Service Worker? @Miqubel

  25. Web Alternative • Fallback that supports all browsers • Not

    free • No background work • Focused on data transport, not notifications • No need to request permissions @Miqubel
  26. iOS: all Apple • Setup APNs (Apple Push Notification service)

    • No difference between pure data vs. notifications • Specific parameters just for iOS too • “Just works”™ @Miqubel
  27. Upstream messages • Android & iOS: send “upstream” messages to

    your server • API missing on web? • Asynchronous and battery efficient @Miqubel
  28. “One size fits no one” - probably me @Miqubel

  29. Thank you! - also me @Miqubel