我想使用OkHttp 3将设备发送到设备Firebase通知,但在发布JSON时收到以下错误:
cannot resolve method create 'com.google.common.net.MediaType,java.lang.String)
这是我的代码:
final String legacyServerKey = "";
final MediaType JSON = MediaType.parse("application/json; charset=utf-8");
OkHttpClient client = new OkHttpClient();
JSONObject json = new JSONObject();
JSONObject dataJson = new JSONObject();
dataJson.put("body", "Hi this is sent from device to device");
dataJson.put("title", "dummy title");
json.put("notification", dataJson);
json.put("to", reg_token);
RequestBody body = RequestBody.create(JSON, json.toString());
Request request = new Request.Builder()
.header("Authorization", "key=" + legacyServerKey)
.url("https://fcm.googleapis.com/fcm/send")
.post(body)
.build();
try {
Response response = client.newCall(request).execute();
String finalResponse = response.body().string();
} catch (IOException e) {
e.printStackTrace();
}
答案 0 :(得分:0)
看起来您导入了com.google.common.net.MediaType
。您需要okhttp3.MediaType