Slide 1

Slide 1 text

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

Slide 2

Slide 2 text

“cross-platform messaging solution that lets you reliably deliver messages at no cost.” What is Firebase Cloud Messaging? @Miqubel

Slide 3

Slide 3 text

Key capabilities: • Notifications or Data messages (4KB) • Versatile targeting • Upstream messages @Miqubel

Slide 4

Slide 4 text

@Miqubel Notification vs. Message • Push Notification? • Notification? • Cloud Message?

Slide 5

Slide 5 text

The idea @Miqubel

Slide 6

Slide 6 text

The reality @Miqubel

Slide 7

Slide 7 text

Notifications console @Miqubel

Slide 8

Slide 8 text

A/B Testing with the console • Presented at Firebase Dev Submit 2017 • Multiple audience target • Different content / parameters • Check results per audience @Miqubel

Slide 9

Slide 9 text

@Miqubel

Slide 10

Slide 10 text

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

Slide 11

Slide 11 text

Token problem • Where to store the token? • More than one token per user? • What happens when the user logs out? • Keep sending emails? @Miqubel

Slide 12

Slide 12 text

Notification vs. Data @Miqubel

Slide 13

Slide 13 text

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

Slide 14

Slide 14 text

Notification Payload • “icon”: Android & iOS • “sound”: only Android • “badge”: only iOS • “click_action”: Android & iOS "notification":{ "title": "New Notification!", "body": "Test" }, @Miqubel

Slide 15

Slide 15 text

Notifications & Click Actions "notification":{ "title": "New Notification!", "body": "Test", "click_action": "OPEN_FRIEND_LIST" }, @Miqubel

Slide 16

Slide 16 text

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

Slide 17

Slide 17 text

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

Slide 18

Slide 18 text

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

Slide 19

Slide 19 text

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

Slide 20

Slide 20 text

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

Slide 21

Slide 21 text

Web: Asking permission messaging.requestPermission() @Miqubel

Slide 22

Slide 22 text

@Miqubel

Slide 23

Slide 23 text

Web: Notification vs Data @Miqubel

Slide 24

Slide 24 text

Web: Service Worker? @Miqubel

Slide 25

Slide 25 text

Web Alternative • Fallback that supports all browsers • Not free • No background work • Focused on data transport, not notifications • No need to request permissions @Miqubel

Slide 26

Slide 26 text

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

Slide 27

Slide 27 text

Upstream messages • Android & iOS: send “upstream” messages to your server • API missing on web? • Asynchronous and battery efficient @Miqubel

Slide 28

Slide 28 text

“One size fits no one” - probably me @Miqubel

Slide 29

Slide 29 text

Thank you! - also me @Miqubel