Created
October 23, 2020 06:26
-
-
Save sergiocasero/a3fa03bb5c78a57e4e30becd0ac25050 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.sergiocasero.backend.push | |
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential | |
import com.sergiocasero.jvm.network.createService | |
import com.sergiocasero.model.PushTopic | |
import com.sergiocasero.properties.Property | |
import kotlinx.coroutines.Deferred | |
import kotlinx.coroutines.runBlocking | |
import okhttp3.ResponseBody | |
import retrofit2.http.Body | |
import retrofit2.http.POST | |
import java.io.FileInputStream | |
import java.util.* | |
interface FcmApiService { | |
companion object { | |
const val ENDPOINT = "https://fcm.googleapis.com/v1/projects/fibraclim-223015/" | |
} | |
@POST("./messages:send") | |
fun sendPush(@Body push: Push): Deferred<ResponseBody> | |
} | |
fun sendPush(topic: PushTopic, title: String, body: String) { | |
val push = Push(Message(topic = topic.label, notification = Notification(body = body, title = title))) | |
val googleCredential = GoogleCredential | |
.fromStream(FileInputStream(Property.Backend.pushFile)) | |
.createScoped(Arrays.asList("https://www.googleapis.com/auth/firebase.messaging")) | |
googleCredential.refreshToken() | |
val service = createService<FcmApiService>(FcmApiService.ENDPOINT, "Bearer " + googleCredential.accessToken) | |
runBlocking { | |
val response = service.sendPush(push).await() | |
println(response.string()) | |
} | |
} | |
data class Push(val message: Message) | |
data class Message(val topic: String, val notification: Notification) | |
data class Notification(val body: String, val title: String) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment