Volley BasicNetwork.performRequest: Неожиданный код ответа 401 для https://fcm.googleapis.com/fcm/send ошибка
Я не уверен, что является ошибкой, пожалуйста, помогите.
Я пытаюсь получить уведомление. Все еще новичок в отношении сообщений и залпов. Пожалуйста, помогите, я могу получить уведомление в чате, но не с этим
(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);
}
}