Skip to content

Commit b96b220

Browse files
authored
Accept prefixed topic names (#136)
* Accept prefixed topic names * Parsing topic name in a helper method * Updated javadoc for setTopic()
1 parent 854c6df commit b96b220

File tree

2 files changed

+36
-9
lines changed

2 files changed

+36
-9
lines changed

src/main/java/com/google/firebase/messaging/Message.java

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -74,16 +74,23 @@ private Message(Builder builder) {
7474
!Strings.isNullOrEmpty(builder.condition)
7575
);
7676
checkArgument(count == 1, "Exactly one of token, topic or condition must be specified");
77-
if (builder.topic != null) {
78-
checkArgument(!builder.topic.startsWith("/topics/"),
79-
"Topic name must not contain the /topics/ prefix");
80-
checkArgument(builder.topic.matches("[a-zA-Z0-9-_.~%]+"), "Malformed topic name");
81-
}
8277
this.token = builder.token;
83-
this.topic = builder.topic;
78+
this.topic = stripPrefix(builder.topic);
8479
this.condition = builder.condition;
8580
}
8681

82+
private static String stripPrefix(String topic) {
83+
if (Strings.isNullOrEmpty(topic)) {
84+
return null;
85+
}
86+
if (topic.startsWith("/topics/")) {
87+
topic = topic.replaceFirst("^/topics/", "");
88+
}
89+
// Checks for illegal characters and empty string.
90+
checkArgument(topic.matches("[a-zA-Z0-9-_.~%]+"), "Malformed topic name");
91+
return topic;
92+
}
93+
8794
/**
8895
* Creates a new {@link Message.Builder}.
8996
*
@@ -162,10 +169,10 @@ public Builder setToken(String token) {
162169
}
163170

164171
/**
165-
* Sets the name of the FCM topic to which the message should be sent. Topic names must
166-
* not contain the {@code /topics/} prefix.
172+
* Sets the name of the FCM topic to which the message should be sent. Topic names may
173+
* contain the {@code /topics/} prefix.
167174
*
168-
* @param topic A valid topic name excluding the {@code /topics/} prefix.
175+
* @param topic A valid topic name.
169176
* @return This builder.
170177
*/
171178
public Builder setTopic(String topic) {

src/test/java/com/google/firebase/messaging/MessageTest.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,26 @@ public void testDataMessage() throws IOException {
6262
assertJsonEquals(ImmutableMap.of("topic", "test-topic", "data", data), message);
6363
}
6464

65+
@Test
66+
public void testInvalidTopicNames() {
67+
List<String> invalidTopicNames = ImmutableList.of("/topics/", "/foo/bar", "foo bar");
68+
for (String topicName : invalidTopicNames) {
69+
try {
70+
Message.builder().setTopic(topicName).build();
71+
} catch (IllegalArgumentException expected) {
72+
// expected
73+
}
74+
}
75+
}
76+
77+
@Test
78+
public void testPrefixedTopicName() throws IOException {
79+
Message message = Message.builder()
80+
.setTopic("/topics/test-topic")
81+
.build();
82+
assertJsonEquals(ImmutableMap.of("topic", "test-topic"), message);
83+
}
84+
6585
@Test
6686
public void testNotificationMessage() throws IOException {
6787
Message message = Message.builder()

0 commit comments

Comments
 (0)