Volley BasicNetwork.performRequest.BasicNetwork.PerformRequest: 预期的响应代码401 https:/fcm.googleapis.comfcmsend 错误
我不知道这是什么错误,请大家帮忙。
我想得到通知.还是新的向火基消息&volley。请帮助我能够得到聊天通知,但不能用这个。
(PS:原代码有授权与我的firebase密钥,在这里过滤掉了)
private void prepareNotification(String postID, String title, String description, String notificationType)
{
String NOTIFICATION_TITLE = title;
String NOTIFICATION_MESSAGE = description;
String NOTIFICATION_TYPE = notificationType;
JSONObject notificationJo = new JSONObject();
JSONObject notificationBodyJo = new JSONObject();
try
{
notificationBodyJo.put("notificationType", NOTIFICATION_TYPE);
notificationBodyJo.put("sender", hisId);
notificationBodyJo.put("postID", postID);
notificationBodyJo.put("pTitle", NOTIFICATION_TITLE);
notificationBodyJo.put("pDescription", NOTIFICATION_MESSAGE);
notificationJo.put("data", notificationBodyJo);
}
catch (JSONException e)
{
Toast.makeText(this, ""+e.getMessage(), Toast.LENGTH_SHORT).show();
}
sendPostNotification(notificationJo);
}
private void sendPostNotification(JSONObject notificationJo)
{
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest("https://fcm.googleapis.com/fcm/send", notificationJo,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.d("FCM_RESPONSE", "onResponse:" +response.toString());
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(CreatePostDetails.this, ""+error.toString(), Toast.LENGTH_SHORT).show();
}
})
{
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> headers = new HashMap<>();
headers.put("Content-Type", "application/json");
headers.put("Authorization", "key= "); //I already put API key inside in my code
return super.getHeaders();
}
};
Volley.newRequestQueue(this).add(jsonObjectRequest);
}
FirebaseMessagingService
String sender = remoteMessage.getData().get("sender");
String pId = remoteMessage.getData().get("pId");
String pTitle = remoteMessage.getData().get("pTitle");
String pDescription = remoteMessage.getData().get("pDescription");
if (!sender.equals(savedCurrentUser))
{
showPostNotification(""+pId, ""+pTitle, pDescription);
}
private void showPostNotification(String pId, String pTitle, String pDescription)
{
NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
int notificationID = new Random().nextInt(3000);
//APP > O
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
{
setupPostNotificationChannel(notificationManager);
}
Intent intent = new Intent(this, CreatePostDetails.class);
intent.putExtra("postID", pId);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this,0, intent, PendingIntent.FLAG_ONE_SHOT);
Bitmap largeIcon = BitmapFactory.decodeResource(getResources(), R.drawable.splash);
Uri notificationSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, "" + ADMIN_CHANNEL_ID)
.setSmallIcon(R.drawable.splash)
.setLargeIcon(largeIcon)
.setContentTitle(pTitle)
.setContentText(pDescription)
.setSound(notificationSoundUri)
.setContentIntent(pendingIntent);
notificationManager.notify(notificationID, notificationBuilder.build());
}
@RequiresApi(api = Build.VERSION_CODES.O)
private void setupPostNotificationChannel(NotificationManager notificationManager) {
CharSequence channelName = "New Notification";
String channelDescription = "Device to device post notification";
NotificationChannel adminChannel = new NotificationChannel(ADMIN_CHANNEL_ID, channelName, NotificationManager.IMPORTANCE_HIGH);
adminChannel.setDescription(channelDescription);
adminChannel.enableLights(true);
adminChannel.setLightColor(Color.RED);
adminChannel.enableVibration(true);
if (notificationManager!=null){
notificationManager.createNotificationChannel(adminChannel);
}
}
首先确保你有服务器密钥从firebase-console -> project-setting -> cloud messaging选项卡。
我有同样的问题,发送通知与 JsonObjectRequest尝试用StringRequest代替.它为我工作。
public void send(String topic,String mes){
JSONObject notification=new JSONObject();
JSONObject notificationBody=new JSONObject();
JSONObject data=new JSONObject();
try{
data.put("key","value");
notificationBody.put("title", "notification title");
notificationBody.put("body", mes);
notification.put("to", "/topics/"+topic);
notification.put("notification", notificationBody);
notification.put("data", data);
}
catch (Exception e){
e.printStackTrace();
}
StringRequest req=new StringRequest(Request.Method.POST, FCM_API, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
}
},null){
@Override
public byte[] getBody() throws AuthFailureError {
try{
return notification.toString().getBytes(StandardCharsets.UTF_8);
}
catch (Exception e){
return null;
}
}
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> params = new HashMap<>();
params.put("Authorization", "key="+serverKey);
params.put("Content-Type", contentType);
return params;
}
};
requestQueue.add(req);
}