I'm working on a backend I'd like to use to send / Push notifications to android app using Firebase Cloud Messaging (FCM).
I've read the documentation ( FCM a few times), but I still have many questions.
I know you can use the Admin SDK or some other server protocol, but I decided to use the Admin SDK.
I have already registered in the Firebase Console, generated the server key and other necessary keys.
Here is an example of a message:
POST https://fcm.googleapis.com/v1/projects/nome_projeto/messages:send HTTP/1.1
Content-Type: application/json
Authorization: Bearer ya29.ElqKBGN2Ri_Uz...HnS_uNreA
{
"message":{
"token":"bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
"notification":{
"body" : "Primeira Mensagem FCM!",
"title" : "Mensagem FCM"
}
}
Follow part of my source:
<dependency>
<groupId>com.google.firebase</groupId>
<artifactId>firebase-admin</artifactId>
<version>5.8.0</version>
</dependency>
private Resource resource = new ClassPathResource("./service-account.json");
private InputStream resourceInputStream;
public String obterAccessToken() throws IOException {
resourceInputStream = resource.getInputStream();
GoogleCredential googleCredential = GoogleCredential
.fromStream(resourceInputStream)
.createScoped(Collections.singletonList("https://www.googleapis.com/auth/firebase.messaging"));
googleCredential.refreshToken();
return googleCredential.getAccessToken();
}
public void iniciarFirebase() throws IOException {
resourceInputStream = resource.getInputStream();
FirebaseOptions options = new FirebaseOptions.Builder()
.setCredentials(GoogleCredentials.fromStream(resourceInputStream))
.build();
FirebaseApp.initializeApp(options);
}
What now? How do I send the message to an App?
I need to generate a Json with the information of these two methods?