Good morning! I have an implementation of an API that when performing certain function, a call is sent to the Firebase and generated a notification in the application. Well, I researched and implemented based on this tutorial: Tutorial link
There's a part he says like this:
If you are a backend developer and push notification is used in your project then you can request your android developer to register your project with FCM and provide API key and device token respectively.
I imagine the token it refers to is the client token that will receive the notification. Let's say, the guy logged in to the app, the server generated the token and it was stored in the app's storage.
If this is what it is, how do I get the user token? kkk
I put a token generated by the spring service, but it gave an error.
package br.com.push.service;
import org.springframework.boot.configurationprocessor.json.JSONException;
import org.springframework.boot.configurationprocessor.json.JSONObject;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
@Service
public class PushService {
public void push() {
String deviceToken = "Token do serviço do spring";
try {
String androidFcmKey="Chave do projeto";
String androidFcmUrl="https://fcm.googleapis.com/fcm/send";
RestTemplate restTemplate = new RestTemplate();
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.set("Authorization", "key=" + androidFcmKey);
httpHeaders.set("Content-Type", "application/json");
JSONObject msg = new JSONObject();
JSONObject json = new JSONObject();
msg.put("title", "Title");
msg.put("body", "Message");
msg.put("notificationType", "Test");
json.put("data", msg);
json.put("to", deviceToken);
HttpEntity<String> httpEntity = new HttpEntity<String>(json.toString(),httpHeaders);
String response = restTemplate.postForObject(androidFcmUrl,httpEntity,String.class);
System.out.println(response);
} catch (JSONException e) {
e.printStackTrace();
}
}
}
And in the console you have returned the following message:
{"multicast_id":8855216073234680957,
"success":0,
"failure":1,
"canonical_ids":0,
"results":[
{"error":"InvalidRegistration"}
]}