|
| 1 | +/* |
| 2 | + * Copyright 2018 Google 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 | + |
| 17 | +package com.google.firebase.messaging; |
| 18 | + |
| 19 | +import static com.google.common.base.Preconditions.checkArgument; |
| 20 | + |
| 21 | +import com.google.api.client.util.Key; |
| 22 | +import com.google.common.collect.ImmutableMap; |
| 23 | +import com.google.firebase.internal.NonNull; |
| 24 | +import java.util.HashMap; |
| 25 | +import java.util.Map; |
| 26 | +import java.util.concurrent.TimeUnit; |
| 27 | + |
| 28 | +/** |
| 29 | + * Represents the Android-specific options that can be included in a {@link Message}. |
| 30 | + * Instances of this class are thread-safe and immutable. |
| 31 | + */ |
| 32 | +public class AndroidConfig { |
| 33 | + |
| 34 | + @Key("collapse_key") |
| 35 | + private final String collapseKey; |
| 36 | + |
| 37 | + @Key("priority") |
| 38 | + private final String priority; |
| 39 | + |
| 40 | + @Key("ttl") |
| 41 | + private final String ttl; |
| 42 | + |
| 43 | + @Key("restricted_package_name") |
| 44 | + private final String restrictedPackageName; |
| 45 | + |
| 46 | + @Key("data") |
| 47 | + private final Map<String, String> data; |
| 48 | + |
| 49 | + @Key("notification") |
| 50 | + private final AndroidNotification notification; |
| 51 | + |
| 52 | + private AndroidConfig(Builder builder) { |
| 53 | + this.collapseKey = builder.collapseKey; |
| 54 | + if (builder.priority != null) { |
| 55 | + this.priority = builder.priority.name().toLowerCase(); |
| 56 | + } else { |
| 57 | + this.priority = null; |
| 58 | + } |
| 59 | + if (builder.ttl != null) { |
| 60 | + checkArgument(builder.ttl >= 0, "ttl must not be negative"); |
| 61 | + long seconds = TimeUnit.MILLISECONDS.toSeconds(builder.ttl); |
| 62 | + long subsecondNanos = TimeUnit.MILLISECONDS.toNanos(builder.ttl - seconds * 1000L); |
| 63 | + if (subsecondNanos > 0) { |
| 64 | + this.ttl = String.format("%d.%09ds", seconds, subsecondNanos); |
| 65 | + } else { |
| 66 | + this.ttl = String.format("%ds", seconds); |
| 67 | + } |
| 68 | + } else { |
| 69 | + this.ttl = null; |
| 70 | + } |
| 71 | + this.restrictedPackageName = builder.restrictedPackageName; |
| 72 | + this.data = builder.data.isEmpty() ? null : ImmutableMap.copyOf(builder.data); |
| 73 | + this.notification = builder.notification; |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Priority levels that can be set on an {@link AndroidConfig}. |
| 78 | + */ |
| 79 | + public enum Priority { |
| 80 | + HIGH, |
| 81 | + NORMAL, |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * Creates a new {@link AndroidConfig.Builder}. |
| 86 | + * |
| 87 | + * @return A {@link AndroidConfig.Builder} instance. |
| 88 | + */ |
| 89 | + public static Builder builder() { |
| 90 | + return new Builder(); |
| 91 | + } |
| 92 | + |
| 93 | + public static class Builder { |
| 94 | + |
| 95 | + private String collapseKey; |
| 96 | + private Priority priority; |
| 97 | + private Long ttl; |
| 98 | + private String restrictedPackageName; |
| 99 | + private final Map<String, String> data = new HashMap<>(); |
| 100 | + private AndroidNotification notification; |
| 101 | + |
| 102 | + private Builder() {} |
| 103 | + |
| 104 | + /** |
| 105 | + * Sets a collapse key for the message. Collapse key serves as an identifier for a group of |
| 106 | + * messages that can be collapsed, so that only the last message gets sent when delivery can be |
| 107 | + * resumed. A maximum of 4 different collapse keys may be active at any given time. |
| 108 | + * |
| 109 | + * @param collapseKey A collapse key string. |
| 110 | + * @return This builder. |
| 111 | + */ |
| 112 | + public Builder setCollapseKey(String collapseKey) { |
| 113 | + this.collapseKey = collapseKey; |
| 114 | + return this; |
| 115 | + } |
| 116 | + |
| 117 | + /** |
| 118 | + * Sets the priority of the message. |
| 119 | + * |
| 120 | + * @param priority A value from the {@link Priority} enum. |
| 121 | + * @return This builder. |
| 122 | + */ |
| 123 | + public Builder setPriority(Priority priority) { |
| 124 | + this.priority = priority; |
| 125 | + return this; |
| 126 | + } |
| 127 | + |
| 128 | + /** |
| 129 | + * Sets the time-to-live duration of the message in milliseconds. |
| 130 | + * |
| 131 | + * @param ttl Time-to-live duration in milliseconds. |
| 132 | + * @return This builder. |
| 133 | + */ |
| 134 | + public Builder setTtl(long ttl) { |
| 135 | + this.ttl = ttl; |
| 136 | + return this; |
| 137 | + } |
| 138 | + |
| 139 | + /** |
| 140 | + * Sets the package name of the application where the registration tokens must match in order |
| 141 | + * to receive the message. |
| 142 | + * |
| 143 | + * @param restrictedPackageName A package name string. |
| 144 | + * @return This builder. |
| 145 | + */ |
| 146 | + public Builder setRestrictedPackageName(String restrictedPackageName) { |
| 147 | + this.restrictedPackageName = restrictedPackageName; |
| 148 | + return this; |
| 149 | + } |
| 150 | + |
| 151 | + /** |
| 152 | + * Adds the given key-value pair to the message as a data field. Key and the value may not be |
| 153 | + * null. When set, overrides any data fields set on the top-level {@link Message} via |
| 154 | + * {@link Message.Builder#putData(String, String)} and {@link Message.Builder#putAllData(Map)}. |
| 155 | + * |
| 156 | + * @param key Name of the data field. Must not be null. |
| 157 | + * @param value Value of the data field. Must not be null. |
| 158 | + * @return This builder. |
| 159 | + */ |
| 160 | + public Builder putData(@NonNull String key, @NonNull String value) { |
| 161 | + this.data.put(key, value); |
| 162 | + return this; |
| 163 | + } |
| 164 | + |
| 165 | + /** |
| 166 | + * Adds all the key-value pairs in the given map to the message as data fields. None of the |
| 167 | + * keys and values may be null. When set, overrides any data fields set on the top-level |
| 168 | + * {@link Message} via {@link Message.Builder#putData(String, String)} and |
| 169 | + * {@link Message.Builder#putAllData(Map)}. |
| 170 | + * |
| 171 | + * @param map A non-null map of data fields. Map must not contain null keys or values. |
| 172 | + * @return This builder. |
| 173 | + */ |
| 174 | + public Builder putAllData(@NonNull Map<String, String> map) { |
| 175 | + this.data.putAll(map); |
| 176 | + return this; |
| 177 | + } |
| 178 | + |
| 179 | + /** |
| 180 | + * Sets the Android notification to be included in the message. |
| 181 | + * |
| 182 | + * @param notification An {@link AndroidNotification} instance. |
| 183 | + * @return This builder. |
| 184 | + */ |
| 185 | + public Builder setNotification(AndroidNotification notification) { |
| 186 | + this.notification = notification; |
| 187 | + return this; |
| 188 | + } |
| 189 | + |
| 190 | + /** |
| 191 | + * Creates a new {@link AndroidConfig} instance from the parameters set on this builder. |
| 192 | + * |
| 193 | + * @return A new {@link AndroidConfig} instance. |
| 194 | + * @throws IllegalArgumentException If any of the parameters set on the builder are invalid. |
| 195 | + */ |
| 196 | + public AndroidConfig build() { |
| 197 | + return new AndroidConfig(this); |
| 198 | + } |
| 199 | + } |
| 200 | +} |
0 commit comments