Skip to content

Commit fdfe8ea

Browse files
committed
feat(messaging): topic subscription changes
1 parent 33da83f commit fdfe8ea

6 files changed

Lines changed: 521 additions & 15 deletions

File tree

‎src/main/java/com/google/firebase/messaging/FirebaseMessaging.java‎

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -557,6 +557,42 @@ private CallableOperation<TopicManagementResponse, FirebaseMessagingException> s
557557
final List<String> registrationTokens, final String topic) {
558558
checkRegistrationTokens(registrationTokens);
559559
checkTopic(topic);
560+
final FirebaseMessagingClient messagingClient = getMessagingClient();
561+
return new CallableOperation<TopicManagementResponse, FirebaseMessagingException>() {
562+
@Override
563+
protected TopicManagementResponse execute() throws FirebaseMessagingException {
564+
return messagingClient.subscribeToTopic(topic, registrationTokens);
565+
}
566+
};
567+
}
568+
569+
/**
570+
* Subscribes a list of registration tokens to a topic using the legacy Instance ID API.
571+
*
572+
* @deprecated Use {@link #subscribeToTopic(List, String)} instead.
573+
*/
574+
@Deprecated
575+
public TopicManagementResponse subscribeToTopicLegacy(@NonNull List<String> registrationTokens,
576+
@NonNull String topic) throws FirebaseMessagingException {
577+
return subscribeLegacyOp(registrationTokens, topic).call();
578+
}
579+
580+
/**
581+
* Similar to {@link #subscribeToTopicLegacy(List, String)} but performs the operation
582+
* asynchronously.
583+
*
584+
* @deprecated Use {@link #subscribeToTopicAsync(List, String)} instead.
585+
*/
586+
@Deprecated
587+
public ApiFuture<TopicManagementResponse> subscribeToTopicLegacyAsync(
588+
@NonNull List<String> registrationTokens, @NonNull String topic) {
589+
return subscribeLegacyOp(registrationTokens, topic).callAsync(app);
590+
}
591+
592+
private CallableOperation<TopicManagementResponse, FirebaseMessagingException> subscribeLegacyOp(
593+
final List<String> registrationTokens, final String topic) {
594+
checkRegistrationTokens(registrationTokens);
595+
checkTopic(topic);
560596
final InstanceIdClient instanceIdClient = getInstanceIdClient();
561597
return new CallableOperation<TopicManagementResponse, FirebaseMessagingException>() {
562598
@Override
@@ -597,6 +633,43 @@ private CallableOperation<TopicManagementResponse, FirebaseMessagingException> u
597633
final List<String> registrationTokens, final String topic) {
598634
checkRegistrationTokens(registrationTokens);
599635
checkTopic(topic);
636+
final FirebaseMessagingClient messagingClient = getMessagingClient();
637+
return new CallableOperation<TopicManagementResponse, FirebaseMessagingException>() {
638+
@Override
639+
protected TopicManagementResponse execute() throws FirebaseMessagingException {
640+
return messagingClient.unsubscribeFromTopic(topic, registrationTokens);
641+
}
642+
};
643+
}
644+
645+
/**
646+
* Unsubscribes a list of registration tokens from a topic using the legacy Instance ID API.
647+
*
648+
* @deprecated Use {@link #unsubscribeFromTopic(List, String)} instead.
649+
*/
650+
@Deprecated
651+
public TopicManagementResponse unsubscribeFromTopicLegacy(
652+
@NonNull List<String> registrationTokens,
653+
@NonNull String topic) throws FirebaseMessagingException {
654+
return unsubscribeLegacyOp(registrationTokens, topic).call();
655+
}
656+
657+
/**
658+
* Similar to {@link #unsubscribeFromTopicLegacy(List, String)} but performs the operation
659+
* asynchronously.
660+
*
661+
* @deprecated Use {@link #unsubscribeFromTopicAsync(List, String)} instead.
662+
*/
663+
@Deprecated
664+
public ApiFuture<TopicManagementResponse> unsubscribeFromTopicLegacyAsync(
665+
@NonNull List<String> registrationTokens, @NonNull String topic) {
666+
return unsubscribeLegacyOp(registrationTokens, topic).callAsync(app);
667+
}
668+
669+
private CallableOperation<TopicManagementResponse, FirebaseMessagingException>
670+
unsubscribeLegacyOp(final List<String> registrationTokens, final String topic) {
671+
checkRegistrationTokens(registrationTokens);
672+
checkTopic(topic);
600673
final InstanceIdClient instanceIdClient = getInstanceIdClient();
601674
return new CallableOperation<TopicManagementResponse, FirebaseMessagingException>() {
602675
@Override

‎src/main/java/com/google/firebase/messaging/FirebaseMessagingClient.java‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,25 @@ interface FirebaseMessagingClient {
2929
*/
3030
BatchResponse sendAll(List<Message> messages, boolean dryRun) throws FirebaseMessagingException;
3131

32+
/**
33+
* Subscribes a list of registration tokens to a topic via the FCM v1 API.
34+
*
35+
* @param topic Name of the topic.
36+
* @param registrationTokens A list of registration tokens.
37+
* @return A {@link TopicManagementResponse}.
38+
* @throws FirebaseMessagingException If an error occurs.
39+
*/
40+
TopicManagementResponse subscribeToTopic(
41+
String topic, List<String> registrationTokens) throws FirebaseMessagingException;
42+
43+
/**
44+
* Unsubscribes a list of registration tokens from a topic via the FCM v1 API.
45+
*
46+
* @param topic Name of the topic.
47+
* @param registrationTokens A list of registration tokens.
48+
* @return A {@link TopicManagementResponse}.
49+
* @throws FirebaseMessagingException If an error occurs.
50+
*/
51+
TopicManagementResponse unsubscribeFromTopic(
52+
String topic, List<String> registrationTokens) throws FirebaseMessagingException;
3253
}

0 commit comments

Comments
 (0)