/core/services/fcm/src/main/java/com/mongodb/stitch/core/services/fcm/FcmSendMessageRequest.java

https://github.com/mongodb/stitch-android-sdk · Java · 248 lines · 109 code · 23 blank · 116 comment · 2 complexity · da62de07fbef831b427e220dbcea832e MD5 · raw file

  1. /*
  2. * Copyright 2018-present MongoDB, Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.mongodb.stitch.core.services.fcm;
  17. import javax.annotation.Nonnull;
  18. import javax.annotation.Nullable;
  19. import org.bson.Document;
  20. /**
  21. * An FcmSendMessageRequest encapsulates the details of an FCM send message request.
  22. */
  23. public final class FcmSendMessageRequest {
  24. private final FcmSendMessagePriority priority;
  25. private final String collapseKey;
  26. private final Boolean contentAvailable;
  27. private final Boolean mutableContent;
  28. private final Long timeToLive;
  29. private final Document data;
  30. private final FcmSendMessageNotification notification;
  31. private FcmSendMessageRequest(
  32. final FcmSendMessagePriority priority,
  33. final String collapseKey,
  34. final Boolean contentAvailable,
  35. final Boolean mutableContent,
  36. final Long timeToLive,
  37. final Document data,
  38. final FcmSendMessageNotification notification
  39. ) {
  40. this.priority = priority;
  41. this.collapseKey = collapseKey;
  42. this.contentAvailable = contentAvailable;
  43. this.mutableContent = mutableContent;
  44. this.timeToLive = timeToLive;
  45. this.data = data;
  46. this.notification = notification;
  47. }
  48. /**
  49. * Returns the priority of the message.
  50. *
  51. * @return the priority of the message.
  52. */
  53. @Nonnull
  54. public FcmSendMessagePriority getPriority() {
  55. return priority;
  56. }
  57. /**
  58. * Returns the group of messages that can be collapsed.
  59. *
  60. * @return the group of messages that can be collapsed.
  61. */
  62. @Nullable
  63. public String getCollapseKey() {
  64. return collapseKey;
  65. }
  66. /**
  67. * Returns whether or not to indicate to the client that content is available in order
  68. * to wake the device. Note: for messages to iOS devices only.
  69. *
  70. * @return whether or not to indicate to the client that content is available in order
  71. * to wake the device.
  72. */
  73. @Nullable
  74. public Boolean getContentAvailable() {
  75. return contentAvailable;
  76. }
  77. /**
  78. * Returns whether or not the content in the message can be mutated. Note: for messages to
  79. * iOS devices only.
  80. *
  81. * @return whether or not the content in the message can be mutated.
  82. */
  83. @Nullable
  84. public Boolean getMutableContent() {
  85. return mutableContent;
  86. }
  87. /**
  88. * Returns how long (in seconds) the message should be kept in FCM storage if the device is
  89. * offline.
  90. *
  91. * @return how long (in seconds) the message should be kept in FCM storage if the device is
  92. * offline.
  93. */
  94. @Nullable
  95. public Long getTimeToLive() {
  96. return timeToLive;
  97. }
  98. /**
  99. * Returns the custom data to send in the payload.
  100. *
  101. * @return the custom data to send in the payload.
  102. */
  103. @Nullable
  104. public Document getData() {
  105. return data;
  106. }
  107. /**
  108. * Returns the predefined, user-visible key-value pairs of the notification payload.
  109. *
  110. * @return the predefined, user-visible key-value pairs of the notification payload.
  111. */
  112. @Nullable
  113. public FcmSendMessageNotification getNotification() {
  114. return notification;
  115. }
  116. /**
  117. * A builder that can build {@link FcmSendMessageRequest}s.
  118. */
  119. public static class Builder {
  120. private FcmSendMessagePriority priority;
  121. private String collapseKey;
  122. private Boolean contentAvailable;
  123. private Boolean mutableContent;
  124. private Long timeToLive;
  125. private Document data;
  126. private FcmSendMessageNotification notification;
  127. /**
  128. * Constructs a new builder for an FCM send message request.
  129. */
  130. public Builder() {}
  131. /**
  132. * Sets the priority of the message.
  133. *
  134. * @param priority the priority of the message.
  135. * @return the builder.
  136. */
  137. public Builder withPriority(@Nonnull final FcmSendMessagePriority priority) {
  138. this.priority = priority;
  139. return this;
  140. }
  141. /**
  142. * Sets the group of messages that can be collapsed.
  143. *
  144. * @param collapseKey the group of messages that can be collapsed.
  145. * @return the builder.
  146. */
  147. public Builder withCollapseKey(@Nonnull final String collapseKey) {
  148. this.collapseKey = collapseKey;
  149. return this;
  150. }
  151. /**
  152. * Sets whether or not to indicate to the client that content is available in order
  153. * to wake the device. Note: for messages to iOS devices only.
  154. *
  155. * @param contentAvailable whether or not to indicate to the client that content is available
  156. * in order to wake the device.
  157. * @return the builder.
  158. */
  159. public Builder withContentAvailable(final boolean contentAvailable) {
  160. this.contentAvailable = contentAvailable;
  161. return this;
  162. }
  163. /**
  164. * Sets whether or not the content in the message can be mutated. Note: for messages to
  165. * iOS devices only.
  166. *
  167. * @param mutableContent whether or not the content in the message can be mutated.
  168. * @return the builder.
  169. */
  170. public Builder withMutableContent(final boolean mutableContent) {
  171. this.mutableContent = mutableContent;
  172. return this;
  173. }
  174. /**
  175. * Sets how long (in seconds) the message should be kept in FCM storage if the device is
  176. * offline.
  177. *
  178. * @param timeToLive how long (in seconds) the message should be kept in FCM storage if the
  179. * device is offline.
  180. * @return the builder.
  181. */
  182. public Builder withTimeToLive(final long timeToLive) {
  183. this.timeToLive = timeToLive;
  184. return this;
  185. }
  186. /**
  187. * Sets the custom data to send in the payload.
  188. *
  189. * @param data the custom data to send in the payload.
  190. * @return the builder.
  191. */
  192. public Builder withData(@Nonnull final Document data) {
  193. this.data = data;
  194. return this;
  195. }
  196. /**
  197. * Sets the predefined, user-visible key-value pairs of the notification payload.
  198. *
  199. * @param notification the predefined, user-visible key-value pairs of the notification payload.
  200. * @return the builder.
  201. */
  202. public Builder withNotification(@Nonnull final FcmSendMessageNotification notification) {
  203. this.notification = notification;
  204. return this;
  205. }
  206. /**
  207. * Builds, validates, and returns the {@link FcmSendMessageRequest}.
  208. *
  209. * @return the built FCM send message request.
  210. */
  211. public FcmSendMessageRequest build() {
  212. if (priority == null) {
  213. priority = FcmSendMessagePriority.NORMAL;
  214. }
  215. return new FcmSendMessageRequest(
  216. priority,
  217. collapseKey,
  218. contentAvailable,
  219. mutableContent,
  220. timeToLive,
  221. data,
  222. notification);
  223. }
  224. }
  225. }