private void handleTopicManagementHttpError( HttpResponseException e) throws FirebaseMessagingException { InstanceIdServiceErrorResponse response = new InstanceIdServiceErrorResponse(); try { JsonParser parser = jsonFactory.createJsonParser(e.getContent()); parser.parseAndClose(response); } catch (IOException ignored) { // ignored } // Infer error code from HTTP status String code = IID_ERROR_CODES.get(e.getStatusCode()); if (code == null) { code = UNKNOWN_ERROR; } String msg = response.error; if (Strings.isNullOrEmpty(msg)) { msg = String.format("Unexpected HTTP response with status: %d; body: %s", e.getStatusCode(), e.getContent()); } throw new FirebaseMessagingException(code, msg, e); }
private void sendNotification(DeviceInfo device, String notificationBase64) { Message msg = Message.builder() .putData("notification", notificationBase64) .setToken(device.fcm_device_info.token) .build(); try { String resp = FirebaseMessaging.getInstance().send(msg); log.info("Firebase message sent: %s", resp); } catch(FirebaseMessagingException e) { log.error("Error sending firebase notification: %s", e.getErrorCode(), e); } } }
private void handleSendHttpError(HttpResponseException e) throws FirebaseMessagingException { MessagingServiceErrorResponse response = new MessagingServiceErrorResponse(); if (e.getContent() != null) { try { JsonParser parser = jsonFactory.createJsonParser(e.getContent()); parser.parseAndClose(response); } catch (IOException ignored) { // ignored } } String code = FCM_ERROR_CODES.get(response.getErrorCode()); if (code == null) { code = UNKNOWN_ERROR; } String msg = response.getErrorMessage(); if (Strings.isNullOrEmpty(msg)) { msg = String.format("Unexpected HTTP response with status: %d; body: %s", e.getStatusCode(), e.getContent()); } throw new FirebaseMessagingException(code, msg, e); }
@Override protected String execute() throws FirebaseMessagingException { ImmutableMap.Builder<String, Object> payload = ImmutableMap.<String, Object>builder() .put("message", message); if (dryRun) { payload.put("validate_only", true); } HttpResponse response = null; try { HttpRequest request = requestFactory.buildPostRequest( new GenericUrl(url), new JsonHttpContent(jsonFactory, payload.build())); request.getHeaders().set("X-GOOG-API-FORMAT-VERSION", "2"); request.setParser(new JsonObjectParser(jsonFactory)); request.setResponseInterceptor(interceptor); response = request.execute(); MessagingServiceResponse parsed = new MessagingServiceResponse(); jsonFactory.createJsonParser(response.getContent()).parseAndClose(parsed); return parsed.name; } catch (HttpResponseException e) { handleSendHttpError(e); return null; } catch (IOException e) { throw new FirebaseMessagingException( INTERNAL_ERROR, "Error while calling FCM backend service", e); } finally { disconnectQuietly(response); } } };
return null; } catch (IOException e) { throw new FirebaseMessagingException( INTERNAL_ERROR, "Error while calling IID backend service", e); } finally {