Repository navigation
Expand file tree
/
Copy pathApsAlert.java
More file actions
302 lines (265 loc) · 8.46 KB
/
Copy pathApsAlert.java
File metadata and controls
302 lines (265 loc) · 8.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
/*
* Copyright 2018 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.firebase.messaging;
import static com.google.common.base.Preconditions.checkArgument;
import com.google.api.client.util.Key;
import com.google.common.base.Strings;
import com.google.common.collect.ImmutableList;
import com.google.firebase.internal.NonNull;
import java.util.ArrayList;
import java.util.List;
/**
* Represents the <a href="https://developer.apple.com/library/content/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/PayloadKeyReference.html#//apple_ref/doc/uid/TP40008194-CH17-SW5">
* alert property</a> that can be included in the aps dictionary of an APNS payload.
*/
public class ApsAlert {
@Key("title")
private final String title;
@Key("subtitle")
private final String subtitle;
@Key("body")
private final String body;
@Key("loc-key")
private final String locKey;
@Key("loc-args")
private final List<String> locArgs;
@Key("title-loc-key")
private final String titleLocKey;
@Key("title-loc-args")
private final List<String> titleLocArgs;
@Key("subtitle-loc-key")
private final String subtitleLocKey;
@Key("subtitle-loc-args")
private final List<String> subtitleLocArgs;
@Key("action-loc-key")
private final String actionLocKey;
@Key("launch-image")
private final String launchImage;
private ApsAlert(Builder builder) {
this.title = builder.title;
this.subtitle = builder.subtitle;
this.body = builder.body;
this.actionLocKey = builder.actionLocKey;
this.locKey = builder.locKey;
if (!builder.locArgs.isEmpty()) {
checkArgument(!Strings.isNullOrEmpty(builder.locKey),
"locKey is required when specifying locArgs");
this.locArgs = ImmutableList.copyOf(builder.locArgs);
} else {
this.locArgs = null;
}
this.titleLocKey = builder.titleLocKey;
if (!builder.titleLocArgs.isEmpty()) {
checkArgument(!Strings.isNullOrEmpty(builder.titleLocKey),
"titleLocKey is required when specifying titleLocArgs");
this.titleLocArgs = ImmutableList.copyOf(builder.titleLocArgs);
} else {
this.titleLocArgs = null;
}
this.subtitleLocKey = builder.subtitleLocKey;
if (!builder.subtitleLocArgs.isEmpty()) {
checkArgument(!Strings.isNullOrEmpty(builder.subtitleLocKey),
"subtitleLocKey is required when specifying subtitleLocArgs");
this.subtitleLocArgs = ImmutableList.copyOf(builder.subtitleLocArgs);
} else {
this.subtitleLocArgs = null;
}
this.launchImage = builder.launchImage;
}
/**
* Creates a new {@link ApsAlert.Builder}.
*
* @return A {@link ApsAlert.Builder} instance.
*/
public static Builder builder() {
return new Builder();
}
public static class Builder {
private String title;
private String subtitle;
private String body;
private String locKey;
private List<String> locArgs = new ArrayList<>();
private String titleLocKey;
private List<String> titleLocArgs = new ArrayList<>();
private String subtitleLocKey;
private List<String> subtitleLocArgs = new ArrayList<>();
private String actionLocKey;
private String launchImage;
private Builder() {}
/**
* Sets the title of the alert. When provided, overrides the title sent
* via {@link Notification}.
*
* @param title Title of the notification.
* @return This builder.
*/
public Builder setTitle(String title) {
this.title = title;
return this;
}
/**
* Sets the subtitle of the alert.
*
* @param subtitle Subtitle of the notification.
* @return This builder.
*/
public Builder setSubtitle(String subtitle) {
this.subtitle = subtitle;
return this;
}
/**
* Sets the body of the alert. When provided, overrides the body sent
* via {@link Notification}.
*
* @param body Body of the notification.
* @return This builder.
*/
public Builder setBody(String body) {
this.body = body;
return this;
}
/**
* Sets the key of the text in the app's string resources to use to localize the action button
* text.
*
* @param actionLocKey Resource key string.
* @return This builder.
*/
public Builder setActionLocalizationKey(String actionLocKey) {
this.actionLocKey = actionLocKey;
return this;
}
/**
* Sets the key of the body string in the app's string resources to use to localize the body
* text.
*
* @param locKey Resource key string.
* @return This builder.
*/
public Builder setLocalizationKey(String locKey) {
this.locKey = locKey;
return this;
}
/**
* Adds a resource key string that will be used in place of the format specifiers in
* {@code bodyLocKey}.
*
* @param arg Resource key string.
* @return This builder.
*/
public Builder addLocalizationArg(@NonNull String arg) {
this.locArgs.add(arg);
return this;
}
/**
* Adds a list of resource keys that will be used in place of the format specifiers in
* {@code bodyLocKey}.
*
* @param args List of resource key strings.
* @return This builder.
*/
public Builder addAllLocalizationArgs(@NonNull List<String> args) {
this.locArgs.addAll(args);
return this;
}
/**
* Sets the key of the title string in the app's string resources to use to localize the title
* text.
*
* @param titleLocKey Resource key string.
* @return This builder.
*/
public Builder setTitleLocalizationKey(String titleLocKey) {
this.titleLocKey = titleLocKey;
return this;
}
/**
* Adds a resource key string that will be used in place of the format specifiers in
* {@code titleLocKey}.
*
* @param arg Resource key string.
* @return This builder.
*/
public Builder addTitleLocalizationArg(@NonNull String arg) {
this.titleLocArgs.add(arg);
return this;
}
/**
* Adds a list of resource keys that will be used in place of the format specifiers in
* {@code titleLocKey}.
*
* @param args List of resource key strings.
* @return This builder.
*/
public Builder addAllTitleLocArgs(@NonNull List<String> args) {
this.titleLocArgs.addAll(args);
return this;
}
/**
* Sets the key of the subtitle string in the app's string resources to use to localize
* the subtitle text.
*
* @param subtitleLocKey Resource key string.
* @return This builder.
*/
public Builder setSubtitleLocalizationKey(String subtitleLocKey) {
this.subtitleLocKey = subtitleLocKey;
return this;
}
/**
* Adds a resource key string that will be used in place of the format specifiers in
* {@code subtitleLocKey}.
*
* @param arg Resource key string.
* @return This builder.
*/
public Builder addSubtitleLocalizationArg(@NonNull String arg) {
this.subtitleLocArgs.add(arg);
return this;
}
/**
* Adds a list of resource keys that will be used in place of the format specifiers in
* {@code subtitleLocKey}.
*
* @param args List of resource key strings.
* @return This builder.
*/
public Builder addAllSubtitleLocArgs(@NonNull List<String> args) {
this.subtitleLocArgs.addAll(args);
return this;
}
/**
* Sets the launch image for the notification action.
*
* @param launchImage An image file name.
* @return This builder.
*/
public Builder setLaunchImage(String launchImage) {
this.launchImage = launchImage;
return this;
}
/**
* Creates a new {@link ApsAlert} instance from the parameters set on this builder.
*
* @return A new {@link ApsAlert} instance.
* @throws IllegalArgumentException If any of the parameters set on the builder are invalid.
*/
public ApsAlert build() {
return new ApsAlert(this);
}
}
}