1
I am working on a backend that I would like to use to send / Push notifications to android app using the Firebase Cloud Messaging (FCM).
I’ve read the documentation a few times (WCF) but I still have many doubts.
I know it is possible to use the Admin SDK or some other server protocol, but what I decided was to use the Admin SDK.
I have already registered in the Firebase Console, generated the server key and other necessary keys.
Below 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?
firebase Adminsdk does not support sending notifications even under the documentation: https://firebase.google.com/docs/admin/setup
– Weslley Barbosa
It is possible to send the notification with an HTTP call on the FCM server: https://firebase.google.com/docs/cloud-messaging/http-server-ref
– Weslley Barbosa
Very useful both tips. I’m also trying to make work the example you posted. I never used and I’m trying to learn Retrofit2 (Call).
– Mamga
It’s very easy, I did it in a project where the banckend had to notify users, and in the backend and easier still to use retrofit, because it does not need to make call asincrona, you can run the direct call that the main thread supports, in case you do not want to treat everything as Entity, vc can use Okhttp lib to handle everything directly from the network layer
– Weslley Barbosa
In the example, sendPush(Map<String, Object> param) is not accepting the return of Return Response.body(). I still don’t understand pq...
– Mamga
In vdd, the syntax is incorrect, because the return of the method is VOID and does not accept Return, because I did many sending methods and some of them I did asynchronous execution, because I did not need to know the result
– Weslley Barbosa
I edited and switched to the correct syntax
– Weslley Barbosa
If I use it as it is now (public Firebasepushresponse sendPush(Map<String, Object> param) ) it still continues with the following error : The Return type is incompatible with Firebasecall.sendPush(Map<String,Object>). I’m trying to make a simpler example here to try to understand.
– Mamga
I omitted the converter, because you can choose the lib of your preference, here in this example it shows how to add a converter https://futurestud.io/tutorials/retrofit-2-adding-customizing-the-gson-converter
– Weslley Barbosa
The Converter is for you to treat the answers, as a REST framework vc treats the answers directly as an entity and not as Json, but for that you need to map the answers with a JSON library, in the case of the example you use Gson (google library)
– Weslley Barbosa
It’s about to get... Now the following error happens: Exception in thread com.fasterxml.Jackson.databind.exc.Invaliddefinitionexception: Cannot Construct instance of instance of
br.com.teste.FirebasePushResponse
(no Creators, like default Construct, exist): cannot deserialize from Object value (no delegate- or Property-based Creator) at [Source: (okhttp3.Responsebody$Bomawarereader); line: 1, column: 2]... Thank you– Mamga
That’s why I like to use GSON, it doesn’t need many treatments, but tbm is limited to how to map, but you can try this solution to your error with Jackson: http://www.baeldung.com/jackson-exception
– Weslley Barbosa
I switched to GSON friend... It was no mistake. Firebasepush: sucess. Ufaaaaaaa. Now to test if you are sending the push correctly I need to install an app on a mobile phone? Can you get any I can test? Or do I need to "do" one?
– Mamga
Rlx, some topics may take days to be synchronized Android/ IOS, but if you send the call to the topic default, it will work for sure, but it is very easy to push notification on android, already have some templates ready in the own android studio
– Weslley Barbosa