Skip to content
This repository was archived by the owner on Dec 30, 2024. It is now read-only.

Commit 210c052

Browse files
committed
Added sub/unsub snippets
1 parent 844404d commit 210c052

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

admin/src/main/java/com/google/firebase/example/FirebaseMessagingSnippets.java

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,11 @@
2424
import com.google.firebase.messaging.FirebaseMessaging;
2525
import com.google.firebase.messaging.Message;
2626
import com.google.firebase.messaging.Notification;
27+
import com.google.firebase.messaging.TopicManagementResponse;
2728
import com.google.firebase.messaging.WebpushConfig;
2829
import com.google.firebase.messaging.WebpushNotification;
30+
import java.util.Arrays;
31+
import java.util.List;
2932

3033
public class FirebaseMessagingSnippets {
3134

@@ -146,4 +149,43 @@ public Message webpushMessage() {
146149
return message;
147150
}
148151

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+
}
149191
}

0 commit comments

Comments
 (0)