@@ -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 ) {
0 commit comments