diff --git a/notificationworkerlambda/src/main/scala/com/gu/notifications/worker/AndroidSender.scala b/notificationworkerlambda/src/main/scala/com/gu/notifications/worker/AndroidSender.scala index b128caa11..555387f3f 100644 --- a/notificationworkerlambda/src/main/scala/com/gu/notifications/worker/AndroidSender.scala +++ b/notificationworkerlambda/src/main/scala/com/gu/notifications/worker/AndroidSender.scala @@ -16,6 +16,8 @@ import java.time.Instant import java.util.UUID import java.util.concurrent.Executors import scala.concurrent.{ExecutionContext, ExecutionContextExecutor} +import com.gu.notifications.worker.delivery.fcm.FcmFirebase +import scala.util.Try class AndroidSender(val config: FcmWorkerConfiguration, val firebaseAppName: Option[String], val metricNs: String) extends SenderRequestHandler[FcmClient] { @@ -37,8 +39,10 @@ class AndroidSender(val config: FcmWorkerConfiguration, val firebaseAppName: Opt override implicit val ioContextShift: ContextShift[IO] = IO.contextShift(ec) override implicit val timer: Timer[IO] = IO.timer(ec) - override val deliveryService: IO[Fcm[IO]] = - FcmClient(config.fcmConfig, firebaseAppName).fold(e => IO.raiseError(e), c => IO.delay(new Fcm(c))) + val fcmFirebase: Try[FcmFirebase] = FcmFirebase(config.fcmConfig, firebaseAppName) + override val deliveryService: IO[Fcm[IO]] = + fcmFirebase.fold(e => IO.raiseError(e), c => IO.delay(new Fcm(FcmClient(c)))) + override val maxConcurrency = config.concurrencyForIndividualSend override val batchConcurrency = 100 diff --git a/notificationworkerlambda/src/main/scala/com/gu/notifications/worker/delivery/fcm/FcmClient.scala b/notificationworkerlambda/src/main/scala/com/gu/notifications/worker/delivery/fcm/FcmClient.scala index 4da807cf8..2fd3df29d 100644 --- a/notificationworkerlambda/src/main/scala/com/gu/notifications/worker/delivery/fcm/FcmClient.scala +++ b/notificationworkerlambda/src/main/scala/com/gu/notifications/worker/delivery/fcm/FcmClient.scala @@ -176,8 +176,10 @@ class FcmClient (firebaseMessaging: FirebaseMessaging, firebaseApp: FirebaseApp, } -object FcmClient { - def apply(config: FcmConfig, firebaseAppName: Option[String]): Try[FcmClient] = +case class FcmFirebase(firebaseMessaging: FirebaseMessaging, firebaseApp: FirebaseApp, config: FcmConfig, projectId: String, credential: GoogleCredentials, jsonFactory: JsonFactory) + +object FcmFirebase { + def apply(config: FcmConfig, firebaseAppName: Option[String]): Try[FcmFirebase] = Try { val credential = GoogleCredentials.fromStream(new ByteArrayInputStream(config.serviceAccountKey.getBytes)) val firebaseOptions: FirebaseOptions = FirebaseOptions.builder() @@ -193,10 +195,15 @@ object FcmClient { case s: ServiceAccountCredentials => s.getProjectId() case _ => "" } - new FcmClient(FirebaseMessaging.getInstance(firebaseApp), firebaseApp, config, projectId, credential, firebaseOptions.getJsonFactory()) + new FcmFirebase(FirebaseMessaging.getInstance(firebaseApp), firebaseApp, config, projectId, credential, firebaseOptions.getJsonFactory()) } } +object FcmClient { + def apply(firebase: FcmFirebase): FcmClient = + new FcmClient(firebase.firebaseMessaging, firebase.firebaseApp, firebase.config, firebase.projectId, firebase.credential, firebase.jsonFactory) +} + object FirebaseHelpers { implicit class RichApiFuture[T](val af: ApiFuture[T]) extends AnyVal {