com.google.firebase.messaging.Message Java Examples

The following examples show how to use com.google.firebase.messaging.Message. 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 Message androidMessage() {
  // [START android_message]
  Message message = Message.builder()
      .setAndroidConfig(AndroidConfig.builder()
          .setTtl(3600 * 1000) // 1 hour in milliseconds
          .setPriority(AndroidConfig.Priority.NORMAL)
          .setNotification(AndroidNotification.builder()
              .setTitle("$GOOG up 1.43% on the day")
              .setBody("$GOOG gained 11.80 points to close at 835.67, up 1.43% on the day.")
              .setIcon("stock_ticker_update")
              .setColor("#f45342")
              .build())
          .build())
      .setTopic("industry-tech")
      .build();
  // [END android_message]
  return message;
}
 
Example #5
Source File: FirebaseMessagingSnippets.java    From firebase-admin-java with Apache License 2.0 6 votes vote down vote up
public Message apnsMessage() {
  // [START apns_message]
  Message message = Message.builder()
      .setApnsConfig(ApnsConfig.builder()
          .putHeader("apns-priority", "10")
          .setAps(Aps.builder()
              .setAlert(ApsAlert.builder()
                  .setTitle("$GOOG up 1.43% on the day")
                  .setBody("$GOOG gained 11.80 points to close at 835.67, up 1.43% on the day.")
                  .build())
              .setBadge(42)
              .build())
          .build())
      .setTopic("industry-tech")
      .build();
  // [END apns_message]
  return message;
}
 
Example #6
Source File: FirebaseMessagingSnippets.java    From firebase-admin-java with Apache License 2.0 6 votes vote down vote up
public Message allPlatformsMessage() {
  // [START multi_platforms_message]
  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."))
      .setAndroidConfig(AndroidConfig.builder()
          .setTtl(3600 * 1000)
          .setNotification(AndroidNotification.builder()
              .setIcon("stock_ticker_update")
              .setColor("#f45342")
              .build())
          .build())
      .setApnsConfig(ApnsConfig.builder()
          .setAps(Aps.builder()
              .setBadge(42)
              .build())
          .build())
      .setTopic("industry-tech")
      .build();
  // [END multi_platforms_message]
  return message;
}
 
Example #7
Source File: FirebaseNotificationSender.java    From zhcet-web with Apache License 2.0 6 votes vote down vote up
private static Message createMessage(Notification notification, String fcmToken) {
    String title = notification.getSender().getName() + " : " + notification.getTitle();
    String message = notification.getMessage();
    String icon = getIcon(notification);

    Map<String, String> data = new HashMap<>();
    data.put("title", notification.getTitle());
    data.put("message", notification.getMessage());
    data.put("sender", notification.getSender().getName());
    data.put("sentTime", notification.getSentTime().toString());
    data.put("icon", getIcon(notification));

    return Message.builder()
            .setNotification(new com.google.firebase.messaging.Notification(title, message))
            .setWebpushConfig(WebpushConfig.builder()
                .setNotification(new WebpushNotification(title, message, icon))
                .putAllData(data)
                .build())
            .putAllData(data)
            .setToken(fcmToken)
            .build();
}
 
Example #8
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 #9
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 #10
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 #11
Source File: FirebaseMessagingSnippets.java    From firebase-admin-java with Apache License 2.0 5 votes vote down vote up
public Message webpushMessage() {
  // [START webpush_message]
  Message message = Message.builder()
      .setWebpushConfig(WebpushConfig.builder()
          .setNotification(new WebpushNotification(
              "$GOOG up 1.43% on the day",
              "$GOOG gained 11.80 points to close at 835.67, up 1.43% on the day.",
              "https://my-server/icon.png"))
          .setFcmOptions(WebpushFcmOptions.withLink("https://my-server/page-to-open-on-click"))
          .build())
      .setTopic("industry-tech")
      .build();
  // [END webpush_message]
  return message;
}