Firebase_cloud_messaging_http_v1_api - Send Firebase Cloud Messaging Push Notifications via the FCM HTTP v1 API

I needed to send push notifications to a mobile app from my elixir server. I wanted to use Firebase Cloud Messaging’s “topics” so I didn’t have to keep track of every individiual device’s token, which requires using the newer FCM HTTP v1 API. All the other libraries I found on hex (like Pigeon) used the deprecated Legacy HTTP protocol. Versions of this code have been in production since April 2019.

From the README:

FirebaseCloudMessagingHttpV1Api

Send Firebase Cloud Messaging Push Notifications via the FCM HTTP v1 API.

Use goth to acquire authentication tokens.

Example

{:ok, %Goth.Token{token: token}} =
  Goth.Token.for_scope("https://www.googleapis.com/auth/firebase.messaging")

%Google.Firebase.FCM.V1.Message{
  notification: %Google.Firebase.FCM.V1.Notification{
    title: "notification title",
    body: "notification body"
  },
  android: %Google.Firebase.FCM.V1.AndroidConfig{
    priority: "HIGH",
    ttl: "86400s",
    notification: %Google.Firebase.FCM.V1.AndroidNotification{
      channel_id: "my-android-channel"
    }
  },
  topic: "my-topic"
}
|> Google.Firebase.FCM.V1.Message.send("projects/my-firebase-project", token)
3 Likes