|
| 1 | +/* |
| 2 | + * Copyright 2018 Google Inc. All Rights Reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.google.firebase.example; |
| 18 | + |
| 19 | +import com.google.firebase.messaging.AndroidConfig; |
| 20 | +import com.google.firebase.messaging.AndroidNotification; |
| 21 | +import com.google.firebase.messaging.ApnsConfig; |
| 22 | +import com.google.firebase.messaging.Aps; |
| 23 | +import com.google.firebase.messaging.ApsAlert; |
| 24 | +import com.google.firebase.messaging.FirebaseMessaging; |
| 25 | +import com.google.firebase.messaging.Message; |
| 26 | +import com.google.firebase.messaging.Notification; |
| 27 | +import com.google.firebase.messaging.TopicManagementResponse; |
| 28 | +import com.google.firebase.messaging.WebpushConfig; |
| 29 | +import com.google.firebase.messaging.WebpushNotification; |
| 30 | +import java.util.Arrays; |
| 31 | +import java.util.List; |
| 32 | + |
| 33 | +public class FirebaseMessagingSnippets { |
| 34 | + |
| 35 | + public void sendToToken() throws Exception { |
| 36 | + // [START send_to_token] |
| 37 | + // This registration token comes from the client FCM SDKs. |
| 38 | + String registrationToken = "YOUR_REGISTRATION_TOKEN"; |
| 39 | + |
| 40 | + // See documentation on defining a message payload. |
| 41 | + Message message = Message.builder() |
| 42 | + .putData("score", "850") |
| 43 | + .putData("time", "2:45") |
| 44 | + .setToken(registrationToken) |
| 45 | + .build(); |
| 46 | + |
| 47 | + // Send a message to the device corresponding to the provided |
| 48 | + // registration token. |
| 49 | + String response = FirebaseMessaging.getInstance().sendAsync(message).get(); |
| 50 | + // Response is a message ID string. |
| 51 | + System.out.println("Successfully sent message: " + response); |
| 52 | + // [END send_to_token] |
| 53 | + } |
| 54 | + |
| 55 | + public void sendToTopic() throws Exception { |
| 56 | + // [START send_to_topic] |
| 57 | + // The topic name can be optionally prefixed with "/topics/". |
| 58 | + String topic = "highScores"; |
| 59 | + |
| 60 | + // See documentation on defining a message payload. |
| 61 | + Message message = Message.builder() |
| 62 | + .putData("score", "850") |
| 63 | + .putData("time", "2:45") |
| 64 | + .setTopic(topic) |
| 65 | + .build(); |
| 66 | + |
| 67 | + // Send a message to the devices subscribed to the provided topic. |
| 68 | + String response = FirebaseMessaging.getInstance().sendAsync(message).get(); |
| 69 | + // Response is a message ID string. |
| 70 | + System.out.println("Successfully sent message: " + response); |
| 71 | + // [END send_to_topic] |
| 72 | + } |
| 73 | + |
| 74 | + public void sendToCondition() throws Exception { |
| 75 | + // [START send_to_condition] |
| 76 | + // Define a condition which will send to devices which are subscribed |
| 77 | + // to either the Google stock or the tech industry topics. |
| 78 | + String condition = "'stock-GOOG' in topics || 'industry-tech' in topics"; |
| 79 | + |
| 80 | + // See documentation on defining a message payload. |
| 81 | + Message message = Message.builder() |
| 82 | + .setNotification(new Notification( |
| 83 | + "$GOOG up 1.43% on the day", |
| 84 | + "$GOOG gained 11.80 points to close at 835.67, up 1.43% on the day.")) |
| 85 | + .setCondition(condition) |
| 86 | + .build(); |
| 87 | + |
| 88 | + // Send a message to devices subscribed to the combination of topics |
| 89 | + // specified by the provided condition. |
| 90 | + String response = FirebaseMessaging.getInstance().sendAsync(message).get(); |
| 91 | + // Response is a message ID string. |
| 92 | + System.out.println("Successfully sent message: " + response); |
| 93 | + // [END send_to_condition] |
| 94 | + } |
| 95 | + |
| 96 | + public void sendDryRun() throws Exception { |
| 97 | + Message message = Message.builder() |
| 98 | + .putData("score", "850") |
| 99 | + .putData("time", "2:45") |
| 100 | + .setToken("token") |
| 101 | + .build(); |
| 102 | + |
| 103 | + // [START send_dry_run] |
| 104 | + // Send a message in the dry run mode. |
| 105 | + boolean dryRun = true; |
| 106 | + String response = FirebaseMessaging.getInstance().sendAsync(message, dryRun).get(); |
| 107 | + // Response is a message ID string. |
| 108 | + System.out.println("Dry run successful: " + response); |
| 109 | + // [END send_dry_run] |
| 110 | + } |
| 111 | + |
| 112 | + public Message androidMessage() { |
| 113 | + // [START android_message] |
| 114 | + Message message = Message.builder() |
| 115 | + .setAndroidConfig(AndroidConfig.builder() |
| 116 | + .setTtl(3600 * 1000) // 1 hour in milliseconds |
| 117 | + .setPriority(AndroidConfig.Priority.NORMAL) |
| 118 | + .setNotification(AndroidNotification.builder() |
| 119 | + .setTitle("$GOOG up 1.43% on the day") |
| 120 | + .setBody("$GOOG gained 11.80 points to close at 835.67, up 1.43% on the day.") |
| 121 | + .setIcon("stock_ticker_update") |
| 122 | + .setColor("#f45342") |
| 123 | + .build()) |
| 124 | + .build()) |
| 125 | + .setTopic("industry-tech") |
| 126 | + .build(); |
| 127 | + // [END android_message] |
| 128 | + return message; |
| 129 | + } |
| 130 | + |
| 131 | + public Message apnsMessage() { |
| 132 | + // [START apns_message] |
| 133 | + Message message = Message.builder() |
| 134 | + .setApnsConfig(ApnsConfig.builder() |
| 135 | + .putHeader("apns-priority", "10") |
| 136 | + .setAps(Aps.builder() |
| 137 | + .setAlert(ApsAlert.builder() |
| 138 | + .setTitle("$GOOG up 1.43% on the day") |
| 139 | + .setBody("$GOOG gained 11.80 points to close at 835.67, up 1.43% on the day.") |
| 140 | + .build()) |
| 141 | + .setBadge(42) |
| 142 | + .build()) |
| 143 | + .build()) |
| 144 | + .setTopic("industry-tech") |
| 145 | + .build(); |
| 146 | + // [END apns_message] |
| 147 | + return message; |
| 148 | + } |
| 149 | + |
| 150 | + public Message webpushMessage() { |
| 151 | + // [START webpush_message] |
| 152 | + Message message = Message.builder() |
| 153 | + .setWebpushConfig(WebpushConfig.builder() |
| 154 | + .setNotification(new WebpushNotification( |
| 155 | + "$GOOG up 1.43% on the day", |
| 156 | + "$GOOG gained 11.80 points to close at 835.67, up 1.43% on the day.", |
| 157 | + "https://my-server/icon.png")) |
| 158 | + .build()) |
| 159 | + .setTopic("industry-tech") |
| 160 | + .build(); |
| 161 | + // [END webpush_message] |
| 162 | + return message; |
| 163 | + } |
| 164 | + |
| 165 | + public Message allPlatformsMessage() { |
| 166 | + // [START multi_platforms_message] |
| 167 | + Message message = Message.builder() |
| 168 | + .setNotification(new Notification( |
| 169 | + "$GOOG up 1.43% on the day", |
| 170 | + "$GOOG gained 11.80 points to close at 835.67, up 1.43% on the day.")) |
| 171 | + .setAndroidConfig(AndroidConfig.builder() |
| 172 | + .setTtl(3600 * 1000) |
| 173 | + .setNotification(AndroidNotification.builder() |
| 174 | + .setIcon("stock_ticker_update") |
| 175 | + .setColor("#f45342") |
| 176 | + .build()) |
| 177 | + .build()) |
| 178 | + .setApnsConfig(ApnsConfig.builder() |
| 179 | + .setAps(Aps.builder() |
| 180 | + .setBadge(42) |
| 181 | + .build()) |
| 182 | + .build()) |
| 183 | + .setTopic("industry-tech") |
| 184 | + .build(); |
| 185 | + // [END multi_platforms_message] |
| 186 | + return message; |
| 187 | + } |
| 188 | + |
| 189 | + public void subscribeToTopic() throws Exception { |
| 190 | + String topic = "highScores"; |
| 191 | + // [START subscribe] |
| 192 | + // These registration tokens come from the client FCM SDKs. |
| 193 | + List<String> registrationTokens = Arrays.asList( |
| 194 | + "YOUR_REGISTRATION_TOKEN_1", |
| 195 | + // ... |
| 196 | + "YOUR_REGISTRATION_TOKEN_n" |
| 197 | + ); |
| 198 | + |
| 199 | + // Subscribe the devices corresponding to the registration tokens to the |
| 200 | + // topic. |
| 201 | + TopicManagementResponse response = FirebaseMessaging.getInstance().subscribeToTopicAsync( |
| 202 | + registrationTokens, topic).get(); |
| 203 | + // See the TopicManagementResponse reference documentation |
| 204 | + // for the contents of response. |
| 205 | + System.out.println(response.getSuccessCount() + " tokens were subscribed successfully"); |
| 206 | + // [END subscribe] |
| 207 | + } |
| 208 | + |
| 209 | + public void unsubscribeFromTopic() throws Exception { |
| 210 | + String topic = "highScores"; |
| 211 | + // [START unsubscribe] |
| 212 | + // These registration tokens come from the client FCM SDKs. |
| 213 | + List<String> registrationTokens = Arrays.asList( |
| 214 | + "YOUR_REGISTRATION_TOKEN_1", |
| 215 | + // ... |
| 216 | + "YOUR_REGISTRATION_TOKEN_n" |
| 217 | + ); |
| 218 | + |
| 219 | + // Unsubscribe the devices corresponding to the registration tokens from |
| 220 | + // the topic. |
| 221 | + TopicManagementResponse response = FirebaseMessaging.getInstance().unsubscribeFromTopicAsync( |
| 222 | + registrationTokens, topic).get(); |
| 223 | + // See the TopicManagementResponse reference documentation |
| 224 | + // for the contents of response. |
| 225 | + System.out.println(response.getSuccessCount() + " tokens were unsubscribed successfully"); |
| 226 | + // [END unsubscribe] |
| 227 | + } |
| 228 | +} |
0 commit comments