Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/366.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何使用fcm通知修复发件人id不匹配_Java_Firebase_Firebase Cloud Messaging - Fatal编程技术网

Java 如何使用fcm通知修复发件人id不匹配

Java 如何使用fcm通知修复发件人id不匹配,java,firebase,firebase-cloud-messaging,Java,Firebase,Firebase Cloud Messaging,我正在一个java服务器上工作,该服务器使用OAUTH 2.0身份验证向设备发送Firebase云消息通知 根据guide(),我已经从一个具有编辑器角色的帐户服务中生成了密钥,但是我不断得到403发送者ID\u不匹配的错误 有什么建议吗 public class FcmManager { private static final String DEVICE_NOTIFICATION_URL = "https://fcm.googleapis.com/v1/projects/myp

我正在一个java服务器上工作,该服务器使用OAUTH 2.0身份验证向设备发送Firebase云消息通知

根据guide(),我已经从一个具有编辑器角色的帐户服务中生成了密钥,但是我不断得到403发送者ID\u不匹配的错误

有什么建议吗

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();
}
公共类FcmManager{
专用静态最终字符串设备\u通知\u URL=”https://fcm.googleapis.com/v1/projects/myprojectid/messages:send";
私有静态最终字符串JSON\u STR\u DEVICE\u NOTIFICATION=“{\”message\”:{\“token\”:“+”\%s\”,“NOTIFICATION\”:“+
“{\'body\':\%s\',\'title\':\%s\'}}”;
公共静态布尔sendNotificationDevice(DeviceNotification DeviceNotification,字符串msgTitle,
字符串(msgBody)引发异常{
字符串accessToken=getAccessToken();
系统输出打印LN(msgBody);
String jsonStr=String.format(JSON_STR_DEVICE_通知,devicentification.getToken()
,msgBody,msgTitle);
系统输出打印LN(jsonStr);
F.承诺响应=WS.url(设备通知url)
.setContentType(“应用程序/json”).setHeader(“授权”、“承载人”+
accessToken.post(jsonStr.map)(
新F.函数(){
应用公共字符串(WS.Response){
字符串结果=response.getBody();
系统输出打印项次(结果);
返回结果;
}
}
);
返回true;
}
私有静态字符串getAccessToken()引发IOException{
GoogleCredential GoogleCredential=GoogleCredential
.fromStream(新文件输入流(“mygeneratedkeyfromaccountservice.json”))
.createScope(Arrays.asList(“https://www.googleapis.com/auth/firebase.messaging"));
googleCredential.refreshToken();
返回googleCredential.getAccessToken();
}

}

发件人\u ID\u不匹配
表示您使用的项目ID与您发送到的令牌的项目ID不同。感谢您的重播,。然而,情况显然并非如此。设备通知URL(myprojectid)中的项目id与所用服务帐户密钥上的项目id匹配。