com.google.firebase.messaging.FirebaseMessagingException Java Examples

The following examples show how to use com.google.firebase.messaging.FirebaseMessagingException. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: FirebaseMessagingSnippets.java    From firebase-admin-java with Apache License 2.0 6 votes vote down vote up
public void sendToTopic() throws FirebaseMessagingException {
  // [START send_to_topic]
  // The topic name can be optionally prefixed with "/topics/".
  String topic = "highScores";

  // See documentation on defining a message payload.
  Message message = Message.builder()
      .putData("score", "850")
      .putData("time", "2:45")
      .setTopic(topic)
      .build();

  // Send a message to the devices subscribed to the provided topic.
  String response = FirebaseMessaging.getInstance().send(message);
  // Response is a message ID string.
  System.out.println("Successfully sent message: " + response);
  // [END send_to_topic]
}
 
Example #2
Source File: FirebaseMessagingSnippets.java    From firebase-admin-java with Apache License 2.0 6 votes vote down vote up
public void sendToCondition() throws FirebaseMessagingException {
  // [START send_to_condition]
  // Define a condition which will send to devices which are subscribed
  // to either the Google stock or the tech industry topics.
  String condition = "'stock-GOOG' in topics || 'industry-tech' in topics";

  // See documentation on defining a message payload.
  Message message = Message.builder()
      .setNotification(new Notification(
          "$GOOG up 1.43% on the day",
          "$GOOG gained 11.80 points to close at 835.67, up 1.43% on the day."))
      .setCondition(condition)
      .build();

  // Send a message to devices subscribed to the combination of topics
  // specified by the provided condition.
  String response = FirebaseMessaging.getInstance().send(message);
  // Response is a message ID string.
  System.out.println("Successfully sent message: " + response);
  // [END send_to_condition]
}
 
Example #3
Source File: FirebaseMessagingSnippets.java    From firebase-admin-java with Apache License 2.0 6 votes vote down vote up
public void sendAll() throws FirebaseMessagingException {
  String registrationToken = "YOUR_REGISTRATION_TOKEN";

  // [START send_all]
  // Create a list containing up to 500 messages.
  List<Message> messages = Arrays.asList(
      Message.builder()
          .setNotification(new Notification("Price drop", "5% off all electronics"))
          .setToken(registrationToken)
          .build(),
      // ...
      Message.builder()
          .setNotification(new Notification("Price drop", "2% off all books"))
          .setTopic("readers-club")
          .build()
  );

  BatchResponse response = FirebaseMessaging.getInstance().sendAll(messages);
  // See the BatchResponse reference documentation
  // for the contents of response.
  System.out.println(response.getSuccessCount() + " messages were sent successfully");
  // [END send_all]
}
 
Example #4
Source File: FirebaseMessagingSnippets.java    From firebase-admin-java with Apache License 2.0 6 votes vote down vote up
public void sendMulticast() throws FirebaseMessagingException {
  // [START send_multicast]
  // Create a list containing up to 100 registration tokens.
  // These registration tokens come from the client FCM SDKs.
  List<String> registrationTokens = Arrays.asList(
      "YOUR_REGISTRATION_TOKEN_1",
      // ...
      "YOUR_REGISTRATION_TOKEN_n"
  );

  MulticastMessage message = MulticastMessage.builder()
      .putData("score", "850")
      .putData("time", "2:45")
      .addAllTokens(registrationTokens)
      .build();
  BatchResponse response = FirebaseMessaging.getInstance().sendMulticast(message);
  // See the BatchResponse reference documentation
  // for the contents of response.
  System.out.println(response.getSuccessCount() + " messages were sent successfully");
  // [END send_multicast]
}
 
Example #5
Source File: FirebaseMessagingSnippets.java    From firebase-admin-java with Apache License 2.0 6 votes vote down vote up
public void subscribeToTopic() throws FirebaseMessagingException {
  String topic = "highScores";
  // [START subscribe]
  // These registration tokens come from the client FCM SDKs.
  List<String> registrationTokens = Arrays.asList(
      "YOUR_REGISTRATION_TOKEN_1",
      // ...
      "YOUR_REGISTRATION_TOKEN_n"
  );

  // Subscribe the devices corresponding to the registration tokens to the
  // topic.
  TopicManagementResponse response = FirebaseMessaging.getInstance().subscribeToTopic(
      registrationTokens, topic);
  // See the TopicManagementResponse reference documentation
  // for the contents of response.
  System.out.println(response.getSuccessCount() + " tokens were subscribed successfully");
  // [END subscribe]
}
 
Example #6
Source File: FirebaseMessagingSnippets.java    From firebase-admin-java with Apache License 2.0 6 votes vote down vote up
public void unsubscribeFromTopic() throws FirebaseMessagingException {
  String topic = "highScores";
  // [START unsubscribe]
  // These registration tokens come from the client FCM SDKs.
  List<String> registrationTokens = Arrays.asList(
      "YOUR_REGISTRATION_TOKEN_1",
      // ...
      "YOUR_REGISTRATION_TOKEN_n"
  );

  // Unsubscribe the devices corresponding to the registration tokens from
  // the topic.
  TopicManagementResponse response = FirebaseMessaging.getInstance().unsubscribeFromTopic(
      registrationTokens, topic);
  // See the TopicManagementResponse reference documentation
  // for the contents of response.
  System.out.println(response.getSuccessCount() + " tokens were unsubscribed successfully");
  // [END unsubscribe]
}
 
Example #7
Source File: RelayService.java    From bisq with GNU Affero General Public License v3.0 6 votes vote down vote up
String sendAndroidMessage(String apsTokenHex, String encryptedMessage, boolean useSound) {
    Message.Builder messageBuilder = Message.builder();
    Notification notification = new Notification("Bisq", "Notification");
    messageBuilder.setNotification(notification);
    messageBuilder.putData("encrypted", encryptedMessage);
    messageBuilder.setToken(apsTokenHex);
    if (useSound)
        messageBuilder.putData("sound", "default");
    Message message = messageBuilder.build();
    try {
        FirebaseMessaging firebaseMessaging = FirebaseMessaging.getInstance();
        firebaseMessaging.send(message);
        return SUCCESS;
    } catch (FirebaseMessagingException e) {
        log.error(e.toString());
        e.printStackTrace();
        return "Error: " + e.toString();
    }
}
 
Example #8
Source File: FirebaseMessagingSnippets.java    From firebase-admin-java with Apache License 2.0 5 votes vote down vote up
public void sendToToken() throws FirebaseMessagingException {
  // [START send_to_token]
  // This registration token comes from the client FCM SDKs.
  String registrationToken = "YOUR_REGISTRATION_TOKEN";

  // See documentation on defining a message payload.
  Message message = Message.builder()
      .putData("score", "850")
      .putData("time", "2:45")
      .setToken(registrationToken)
      .build();

  // Send a message to the device corresponding to the provided
  // registration token.
  String response = FirebaseMessaging.getInstance().send(message);
  // Response is a message ID string.
  System.out.println("Successfully sent message: " + response);
  // [END send_to_token]
}
 
Example #9
Source File: FirebaseMessagingSnippets.java    From firebase-admin-java with Apache License 2.0 5 votes vote down vote up
public void sendDryRun() throws FirebaseMessagingException {
  Message message = Message.builder()
      .putData("score", "850")
      .putData("time", "2:45")
      .setToken("token")
      .build();

  // [START send_dry_run]
  // Send a message in the dry run mode.
  boolean dryRun = true;
  String response = FirebaseMessaging.getInstance().send(message, dryRun);
  // Response is a message ID string.
  System.out.println("Dry run successful: " + response);
  // [END send_dry_run]
}
 
Example #10
Source File: FirebaseMessagingSnippets.java    From firebase-admin-java with Apache License 2.0 5 votes vote down vote up
public void sendMulticastAndHandleErrors() throws FirebaseMessagingException {
  // [START send_multicast_error]
  // These registration tokens come from the client FCM SDKs.
  List<String> registrationTokens = Arrays.asList(
      "YOUR_REGISTRATION_TOKEN_1",
      // ...
      "YOUR_REGISTRATION_TOKEN_n"
  );

  MulticastMessage message = MulticastMessage.builder()
      .putData("score", "850")
      .putData("time", "2:45")
      .addAllTokens(registrationTokens)
      .build();
  BatchResponse response = FirebaseMessaging.getInstance().sendMulticast(message);
  if (response.getFailureCount() > 0) {
    List<SendResponse> responses = response.getResponses();
    List<String> failedTokens = new ArrayList<>();
    for (int i = 0; i < responses.size(); i++) {
      if (!responses.get(i).isSuccessful()) {
        // The order of responses corresponds to the order of the registration tokens.
        failedTokens.add(registrationTokens.get(i));
      }
    }

    System.out.println("List of tokens that caused failures: " + failedTokens);
  }
  // [END send_multicast_error]
}
 
Example #11
Source File: FirebaseErrorParsingUtils.java    From zhcet-web with Apache License 2.0 5 votes vote down vote up
/**
 * Checks if a firebase token is invalid by checking if it either expired or not forbidden
 * @param throwable Throwable of firebase execution error
 * @return TokenStatus denoting status of the token
 */
public static TokenStatus getTokenStatus(Throwable throwable) {
    Throwable current = throwable;
    while (!(current instanceof FirebaseMessagingException) && current != null) {
        current = current.getCause();
    }

    if (current == null)
        throw new InvalidThrowableException(throwable);

    // We have a FirebaseMessagingException

    FirebaseMessagingException firebaseMessagingException = (FirebaseMessagingException) current;

    while (!(current instanceof HttpResponseException) && current != null) {
        current = current.getCause();
    }

    if (current == null)
        throw new InvalidThrowableException(throwable);

    // We have a HttpResponseException

    HttpResponseException httpResponseException = (HttpResponseException) current;
    int statusCode = httpResponseException.getStatusCode();

    Reason reason = new Reason(statusCode, current.getMessage(), httpResponseException.getContent());

    boolean isTokenExpired = statusCode == 404 || statusCode == 400;
    boolean isTokenForbidden = statusCode == 403;

    if (isTokenExpired || isTokenForbidden) {
        return new TokenStatus(false, reason);
    } else {
        return new TokenStatus(true, reason);
    }
}