如何使用FCM通知修复sender_id_mismatch

时间:2019-01-08 11:45:49

标签: java firebase firebase-cloud-messaging

我在Java服务器上工作,该服务器正在使用OAUTH 2.0身份验证向设备发送Firebase Cloud Messaging通知。

根据指南(https://firebase.google.com/docs/cloud-messaging/auth-server?authuser=0),我已经从具有编辑者角色的帐户服务生成了密钥,但是我一直收到 403 SENDER_ID_MISMATCH 错误。

有什么建议吗?

public class FcmManager {
     private static final String DEVICE_NOTIFICATION_URL =   "https://fcm.googleapis.com/v1/projects/myprojectid/messages:send";
     private static final String JSON_STR_DEVICE_NOTIFICATION = "{\"message\":{\"token\" : " +"\"%s\"" +  ",\"notification\" : " +
            "{\"body\" : \"%s\",\"title\" : \"%s\"}}}";
public static boolean sendNotificationDevice(DeviceNotification deviceNotification, String msgTitle,
                                             String msgBody) throws Exception{
    String accessToken = getAccessToken();

    System.out.println(msgBody);
    String jsonStr = String.format(JSON_STR_DEVICE_NOTIFICATION, deviceNotification.getToken()
    ,msgBody,msgTitle);
    System.out.println(jsonStr);
    F.Promise<String> response = WS.url(DEVICE_NOTIFICATION_URL)
            .setContentType("application/json").setHeader("Authorization","Bearer " +
                    accessToken).post(jsonStr).map(
                    new F.Function<WS.Response, String>() {
                        public String apply(WS.Response response) {
                            String result = response.getBody();
                            System.out.println(result);
                            return result;
                        }
                    }
            );
    return true;
}

private static String getAccessToken() throws IOException {
    GoogleCredential googleCredential = GoogleCredential
            .fromStream(new FileInputStream("mygeneratedkeyfromaccountservice.json"))
            .createScoped(Arrays.asList("https://www.googleapis.com/auth/firebase.messaging"));
    googleCredential.refreshToken();
    return googleCredential.getAccessToken();
}

}

0 个答案:

没有答案