问题描述
我正在使用端点https://fcm.googleapis.com/v1/projects/{PROJECT_NAME}/messages:send向Firebase Cloud Messaging服务器发送POST请求,以便向我的应用发送通知。问题是我收到错误401,验证错误。我已经遍历了他们的文档https://firebase.google.com/docs/reference/fcm/rest/v1/projects.messages/send?apix=true,但似乎无法理解为什么会收到此错误。我尝试使用页面上的Google Api资源管理器测试端点,并且工作正常。通知已发送到我的应用程序。但是,在我的代码中使用volley发送相同的POST请求失败,并在JSON中出现以下错误:
{
"error": {
"code": 401,"message": "Request is missing required authentication credential. Expected OAuth 2 access token,login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project.","status": "UNAUTHENTICATED"
}
}
这就是我所做的:
val FCM_POST = "https://fcm.googleapis.com/v1/projects/${Constants.PROJECT_NAME}/messages:send"
val map = mapOf("message" to
mapOf("notification" to
mapOf("title" to "test title","body" to "test body")),"topic" to "testTopic")
val request = object : JsonObjectRequest(
Method.POST,FCM_POST,JSONObject(map),{
showDialog.log(TAG,"sendMedicalTipsNotification response: $it")
},{
val networkResponse = it.networkResponse
if (networkResponse?.data != null) {
val jsonError = String(networkResponse.data)
try {
val jsonObject = JSONObject(jsonError)
showDialog.log(TAG,"sendMedicalTipsNotification error: $jsonObject")
} catch (e: JSONException) {
showDialog.log(TAG,"sendMedicalTipsNotification error: $e")
}
}
}) {
override fun getHeaders(): MutableMap<String,String> {
return mutableMapOf("Authorization" to "Bearer ${Constants.MY_PROJECT_CLOUD_CONSOLE_CLIENT_ID}","Content-Type" to "application/json")
}
}
Volley.newRequestQueue(ctx).add(request)
}
如您所见,我正在请求标头中发送我的CLIENT_ID和内容类型。我真的不知道为什么会收到这个错误
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)