Reputation: 69
I'm using expo notification to handle my notifications. But my api it's not sending the notifications with the expo token. When I send it by the expo site it works fine. Is there any way to convert the token to a FCM or send with the expo token??
This is the token I get with
token = await Notification.getExpoPushTokenAsync({
projectId: Constants.expoConfig?.extra?.eas.projectId,
})
{"data": "ExponentPushToken[zVrYVeGa5RO_Rov5RnKh38]", "type": "expo"}
My api code looks like this
public void enviarFcm(String token, String body) {
try {
InputStream inputStream = ChatService.class.getResourceAsStream("/firebase.json");
//FileInputStream serviceAccount = new FileInputStream("C:\\Projetos\\Rodovia\\rodoviaviam-5cf5a-firebase-adminsdk-odgju-47f821e743.json");
GoogleCredentials credentials = GoogleCredentials.fromStream(inputStream)
.createScoped(Arrays.asList(MESSAGING_SCOPE));
credentials.refreshIfExpired();
String accessToken = credentials.getAccessToken().getTokenValue();
HttpTransport httpTransport = new NetHttpTransport();
HttpRequestFactory requestFactory = httpTransport.createRequestFactory(
new HttpCredentialsAdapter(credentials));
JsonObject message = new JsonObject();
JsonObject messageContent = new JsonObject();
JsonObject notification = new JsonObject();
notification.addProperty("title", "Chat ViaM");
notification.addProperty("body", body);
messageContent.add("notification", notification);
messageContent.addProperty("token", token);
message.add("message", messageContent);
HttpRequest request = requestFactory.buildPostRequest(
new GenericUrl(FCM_URL),
ByteArrayContent.fromString("application/json", message.toString()));
request.getHeaders().setAuthorization("Bearer " + accessToken);
HttpResponse response = request.execute();
System.out.println("Response: " + response.parseAsString());
} catch (HttpResponseException e) {
System.err.println("Error response: " + e.getContent());
} catch (IOException e) {
e.printStackTrace();
}
}
Upvotes: 0
Views: 460
Reputation: 4258
Using @react-native-firebase/messaging
, you can directly interact with the firebase tokens (for both Android and iOS) instead of using the comparable expo-notifications
methods.
const fcmToken = await firebase.messaging().getToken();
if (fcmToken) {
// user has a device token
} else {
// user doesn't have a device token yet
}
Upvotes: 0
Reputation: 1
I think you need to pull the .data
property off the returned promise:
const token = (await Notification.getDeviceTokenPushAsync()).data;
On Android, that will return an FCM token that you can send notifications through FCM. HOwever, on iOS this returns the APN device token, which cannot be used with FCM.
Upvotes: 0
Reputation: 69
I just discovered that the following method get the FCM Token instead of expo push token.
const token = await Notification.getDevicePushTokenAsync()
But maybe use the expo push notification api is better, because it works with fcm, apn, and can handle foreground notifications, witch FCM itself can't
Upvotes: 0