This small Scala Library makes it easy to send a notification through the FCM HTTP API. It allows you to:
- Send a message to single/multiple devices
- Handle updated tokens
- Handle deleted/invalid tokens
This library makes use of the STTP HTTP Client and the Circe JSON library.
- Scala 2.12.x
Either construct an instance of DefaultFcmSender and supply the necessary parameters or use your favorite DI framework.
Bind the DefaultFcmSender to use it in your application:
bind(classOf[FcmSender]).to(classOf[DefaultFcmSender])Bind the DefaultFirebaseAuthenticator:
bind(classOf[FirebaseAuthenticator]).to(classOf[DefaultFirebaseAuthenticator])The FcmSender needs configuration, easiest is to use a Typesafe Config file to configure FCM. In your application.conf add the following configuration:
fcm {
endpoint = "<Google endpoint e.g.: https://fcm.googleapis.com/v1/{parent=projects/*}/messages:send"
key-file = "Path to the JSON key file"
validate-only = true # If true FCM will only validate your notifications but not send them!
token-endpoint = "<Google token endpoint, optional, defaults to: https://www.googleapis.com/oauth2/v4/token>"
}
Bind the DefaultFcmConfigProvider dependency to read the configuration from the application.conf, in case of Google Guice:
bind(classOf[FcmConfigProvider]).to(classOf[DefaultFcmConfigProvider])You can also provide your own implementation of the FcmConfigProvider trait to get needed configuration.
By default the FcmConfigProvider defaults to an async based STTP client; this can be overriden in your own FcmConfigProvider implementation.
Implement the trait io.ceratech.fcm.TokenRepository in your codebase to handle updated/deleted tokens from the FCM server. The library expects an instance of the trait to be injectable. E.g. add the binding in code:
bind(classOf[TokenRepository]).to(classOf[MyTokenRepository])Inject an instance of io.ceratech.fcm.FcmSender into your desired class and call sendMessage(FcmNotification, token). The FcmMessage case class contains the fields you can supply to FCM.