如何使用expo推送令牌发送FCM通知?

问题描述 投票:0回答:1

我正在使用博览会通知来处理我的通知。但我的 api 没有使用 expo 令牌发送通知。当我通过世博会现场发送它时,效果很好。有什么办法可以将代币转换为 FCM 或与 expo 代币一起发送吗?

这是我获得的代币

token = await Notification.getExpoPushTokenAsync({
                projectId: Constants.expoConfig?.extra?.eas.projectId,
            })

{"data": "ExponentPushToken[zVrYVeGa5RO_Rov5RnKh38]", "type": "expo"}

我的API代码看起来像这样

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();
        }
    }
react-native push-notification expo firebase-cloud-messaging
1个回答
0
投票

我刚刚发现以下方法获取的是 FCM Token 而不是 expo Push Token。

const token = await Notification.getDevicePushTokenAsync()

但是也许使用expo推送通知api更好,因为它与fcm,apn一起工作,并且可以处理前台通知,但FCM本身不能

© www.soinside.com 2019 - 2024. All rights reserved.