|
24 | 24 | import com.google.firebase.messaging.FirebaseMessaging;
|
25 | 25 | import com.google.firebase.messaging.Message;
|
26 | 26 | import com.google.firebase.messaging.Notification;
|
| 27 | +import com.google.firebase.messaging.TopicManagementResponse; |
27 | 28 | import com.google.firebase.messaging.WebpushConfig;
|
28 | 29 | import com.google.firebase.messaging.WebpushNotification;
|
| 30 | +import java.util.Arrays; |
| 31 | +import java.util.List; |
29 | 32 |
|
30 | 33 | public class FirebaseMessagingSnippets {
|
31 | 34 |
|
@@ -146,4 +149,43 @@ public Message webpushMessage() {
|
146 | 149 | return message;
|
147 | 150 | }
|
148 | 151 |
|
| 152 | + public void subscribeToTopic() throws Exception { |
| 153 | + String topic = "highScores"; |
| 154 | + // [START subscribe] |
| 155 | + // These registration tokens come from the client FCM SDKs. |
| 156 | + List<String> registrationTokens = Arrays.asList( |
| 157 | + "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...", |
| 158 | + // ... |
| 159 | + "ecupwIfBy1w:APA91bFtuMY7MktgxA3Au_Qx7cKqnf..." |
| 160 | + ); |
| 161 | + |
| 162 | + // Subscribe the devices corresponding to the registration tokens to the |
| 163 | + // topic. |
| 164 | + TopicManagementResponse response = FirebaseMessaging.getInstance().subscribeToTopicAsync( |
| 165 | + registrationTokens, topic).get(); |
| 166 | + // See the TopicManagementResponse reference documentation |
| 167 | + // for the contents of response. |
| 168 | + System.out.println(response.getSuccessCount() + " tokens were subscribed successfully"); |
| 169 | + // [END subscribe] |
| 170 | + } |
| 171 | + |
| 172 | + public void unsubscribeFromTopic() throws Exception { |
| 173 | + String topic = "highScores"; |
| 174 | + // [START unsubscribe] |
| 175 | + // These registration tokens come from the client FCM SDKs. |
| 176 | + List<String> registrationTokens = Arrays.asList( |
| 177 | + "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...", |
| 178 | + // ... |
| 179 | + "ecupwIfBy1w:APA91bFtuMY7MktgxA3Au_Qx7cKqnf..." |
| 180 | + ); |
| 181 | + |
| 182 | + // Unsubscribe the devices corresponding to the registration tokens from |
| 183 | + // the topic. |
| 184 | + TopicManagementResponse response = FirebaseMessaging.getInstance().unsubscribeFromTopicAsync( |
| 185 | + registrationTokens, topic).get(); |
| 186 | + // See the TopicManagementResponse reference documentation |
| 187 | + // for the contents of response. |
| 188 | + System.out.println(response.getSuccessCount() + " tokens were unsubscribed successfully"); |
| 189 | + // [END unsubscribe] |
| 190 | + } |
149 | 191 | }
|
0 commit comments