7
7
*/
8
8
package firebase .cloudmessaging ;
9
9
10
+ import static firebase .cloudmessaging .Utils .getApplicationContext ;
11
+
10
12
import android .app .NotificationChannel ;
11
13
import android .app .NotificationManager ;
12
14
import android .content .Context ;
19
21
import android .os .Build ;
20
22
import android .os .Bundle ;
21
23
22
- import androidx .annotation .NonNull ;
23
24
import androidx .preference .PreferenceManager ;
24
25
25
- import com .google .android .gms .tasks .OnCompleteListener ;
26
- import com .google .android .gms .tasks .Task ;
27
26
import com .google .firebase .messaging .FirebaseMessaging ;
28
27
import com .google .firebase .messaging .RemoteMessage ;
29
28
32
31
import org .appcelerator .kroll .annotations .Kroll ;
33
32
import org .appcelerator .kroll .common .Log ;
34
33
import org .appcelerator .titanium .TiApplication ;
35
- import org .appcelerator .titanium .TiBaseActivity ;
36
34
import org .appcelerator .titanium .util .TiConvert ;
37
35
import org .json .JSONObject ;
38
36
@@ -55,11 +53,6 @@ public CloudMessagingModule() {
55
53
instance = this ;
56
54
}
57
55
58
- @ Kroll .onAppCreate
59
- public static void onAppCreate (TiApplication app ) {
60
- // put module init code that needs to run when the application is created
61
- }
62
-
63
56
public static CloudMessagingModule getInstance () {
64
57
return instance ;
65
58
}
@@ -78,28 +71,30 @@ private KrollDict getLastData()
78
71
79
72
if (extras != null ) {
80
73
for (String key : extras .keySet ()) {
81
- if (extras .get (key ) instanceof Bundle ) {
82
- Bundle bndl = (Bundle ) extras .get (key );
74
+ if (extras .get (key ) instanceof Bundle bndl ) {
83
75
for (String bdnlKey : bndl .keySet ()) {
84
76
data .put (key + "_" + bdnlKey , bndl .get (bdnlKey ));
85
77
}
86
78
} else {
87
- data .put (key , extras .get (key ).toString ());
79
+ Object value = extras .get (key );
80
+ if (value != null ) {
81
+ data .put (key , value .toString ());
82
+ }
88
83
}
89
84
}
90
85
91
86
data .put ("inBackground" , true );
92
87
} else {
93
88
Log .d (LCAT , "Empty extras in Intent" );
94
- if (!notificationData .equals ( "" )) {
89
+ if (!notificationData .isEmpty ( )) {
95
90
data = new KrollDict (new JSONObject (notificationData ));
96
91
data .put ("inBackground" , true );
97
92
}
98
93
}
99
94
100
95
if (data .get ("message" ) == null ) {
101
96
SharedPreferences preferences =
102
- PreferenceManager .getDefaultSharedPreferences (Utils . getApplicationContext ());
97
+ PreferenceManager .getDefaultSharedPreferences (getApplicationContext ());
103
98
String prefMessage = preferences .getString ("titanium.firebase.cloudmessaging.message" , null );
104
99
if (prefMessage != null ) {
105
100
data .put ("message" , new KrollDict (new JSONObject (prefMessage )));
@@ -117,7 +112,7 @@ private KrollDict getLastData()
117
112
@ Kroll .method
118
113
public void registerForPushNotifications () {
119
114
if (Build .VERSION .SDK_INT >= 33 ) {
120
- if (Utils . getApplicationContext ().checkSelfPermission ("android.permission.POST_NOTIFICATIONS" ) == PackageManager .PERMISSION_GRANTED ) {
115
+ if (getApplicationContext ().checkSelfPermission ("android.permission.POST_NOTIFICATIONS" ) == PackageManager .PERMISSION_GRANTED ) {
121
116
fireEvent ("success" , new KrollDict ());
122
117
getToken ();
123
118
} else {
@@ -132,34 +127,20 @@ public void registerForPushNotifications() {
132
127
133
128
@ Kroll .method
134
129
public void subscribeToTopic (String topic ) {
135
- FirebaseMessaging .getInstance ().subscribeToTopic (topic ).addOnCompleteListener (new OnCompleteListener <Void >() {
136
- @ Override
137
- public void onComplete (@ NonNull Task <Void > task ) {
138
- KrollDict data = new KrollDict ();
139
- if (!task .isSuccessful ()) {
140
- data .put ("success" , false );
141
- } else {
142
- data .put ("success" , true );
143
- }
144
- fireEvent ("subscribe" , data );
145
- }
130
+ FirebaseMessaging .getInstance ().subscribeToTopic (topic ).addOnCompleteListener (task -> {
131
+ KrollDict data = new KrollDict ();
132
+ data .put ("success" , task .isSuccessful ());
133
+ fireEvent ("subscribe" , data );
146
134
});
147
135
Log .d (LCAT , "subscribe to " + topic );
148
136
}
149
137
150
138
@ Kroll .method
151
139
public void unsubscribeFromTopic (String topic ) {
152
- FirebaseMessaging .getInstance ().unsubscribeFromTopic (topic ).addOnCompleteListener (new OnCompleteListener <Void >() {
153
- @ Override
154
- public void onComplete (@ NonNull Task <Void > task ) {
155
- KrollDict data = new KrollDict ();
156
- if (!task .isSuccessful ()) {
157
- data .put ("success" , false );
158
- } else {
159
- data .put ("success" , true );
160
- }
161
- fireEvent ("unsubscribe" , data );
162
- }
140
+ FirebaseMessaging .getInstance ().unsubscribeFromTopic (topic ).addOnCompleteListener (task -> {
141
+ KrollDict data = new KrollDict ();
142
+ data .put ("success" , task .isSuccessful ());
143
+ fireEvent ("unsubscribe" , data );
163
144
});
164
145
Log .d (LCAT , "unsubscribe from " + topic );
165
146
}
@@ -172,7 +153,7 @@ public void appDidReceiveMessage(KrollDict opt) {
172
153
@ Kroll .method
173
154
public void clearLastData () {
174
155
SharedPreferences preferences =
175
- PreferenceManager .getDefaultSharedPreferences (Utils . getApplicationContext ());
156
+ PreferenceManager .getDefaultSharedPreferences (getApplicationContext ());
176
157
preferences .edit ().remove ("titanium.firebase.cloudmessaging.message" ).apply ();
177
158
178
159
// remove intent value
@@ -186,34 +167,26 @@ public void clearLastData() {
186
167
@ Kroll .method
187
168
public void getToken () {
188
169
FirebaseMessaging fm = FirebaseMessaging .getInstance ();
189
- fm .getToken ().addOnCompleteListener (new OnCompleteListener <String >() {
190
- @ Override
191
- public void onComplete (@ NonNull Task <String > task ) {
192
- if (task .isSuccessful ()) {
193
- KrollDict data = new KrollDict ();
194
- fcmToken = task .getResult ();
195
- data .put ("fcmToken" , fcmToken );
196
- fireEvent ("didRefreshRegistrationToken" , data );
197
- }
170
+ fm .getToken ().addOnCompleteListener (task -> {
171
+ if (task .isSuccessful ()) {
172
+ KrollDict data = new KrollDict ();
173
+ fcmToken = task .getResult ();
174
+ data .put ("fcmToken" , fcmToken );
175
+ fireEvent ("didRefreshRegistrationToken" , data );
198
176
}
199
177
});
200
178
}
201
179
202
180
@ Kroll .method
203
181
public void deleteToken () {
204
182
FirebaseMessaging fm = FirebaseMessaging .getInstance ();
205
- fm .deleteToken ().addOnCompleteListener (new OnCompleteListener <Void >() {
206
- @ Override
207
- public void onComplete (@ NonNull Task <Void > task ) {
208
- KrollDict data = new KrollDict ();
209
- if (!task .isSuccessful ()) {
210
- data .put ("success" , false );
211
- } else {
212
- data .put ("success" , true );
213
- fcmToken = null ;
214
- }
215
- fireEvent ("tokenRemoved" , data );
183
+ fm .deleteToken ().addOnCompleteListener (task -> {
184
+ KrollDict data = new KrollDict ();
185
+ if (task .isSuccessful ()) {
186
+ fcmToken = null ;
216
187
}
188
+ data .put ("success" , task .isSuccessful ());
189
+ fireEvent ("tokenRemoved" , data );
217
190
});
218
191
}
219
192
@@ -232,11 +205,11 @@ public void sendMessage(KrollDict obj) {
232
205
// add custom data
233
206
Map <String , String > data = (HashMap ) obj .get ("data" );
234
207
assert data != null ;
235
- for (Object o : data .keySet ()) {
236
- rm .addData (( String ) o , data .get (o ));
208
+ for (String o : data .keySet ()) {
209
+ rm .addData (o , data .get (o ));
237
210
}
238
211
239
- if (!fireTo .equals ( "" ) && !fireMessageId .equals ( "" )) {
212
+ if (!fireTo .isEmpty ( ) && !fireMessageId .isEmpty ( )) {
240
213
fm .send (rm .build ());
241
214
} else {
242
215
Log .e (LCAT , "Please set 'to' and 'messageId'" );
@@ -256,7 +229,7 @@ public void onTokenRefresh(String token) {
256
229
}
257
230
}
258
231
259
- public void onMessageReceived (HashMap message ) {
232
+ public void onMessageReceived (HashMap < String , Object > message ) {
260
233
try {
261
234
if (hasListeners ("didReceiveMessage" )) {
262
235
KrollDict data = new KrollDict ();
@@ -274,14 +247,14 @@ public void createNotificationChannel(KrollDict options) {
274
247
return ;
275
248
}
276
249
Log .d (LCAT , "createNotificationChannel " + options .toString ());
277
- Context context = Utils . getApplicationContext ();
250
+ Context context = getApplicationContext ();
278
251
String sound = options .optString ("sound" , "default" );
279
252
String importance = options .optString ("importance" , sound .equals ("silent" ) ? "low" : "default" );
280
253
String channelId = options .optString ("channelId" , "default" );
281
254
String channelName = options .optString ("channelName" , channelId );
282
- Boolean vibration = ( Boolean ) options .optBoolean ("vibrate" , false );
283
- Boolean lights = ( Boolean ) options .optBoolean ("lights" , false );
284
- Boolean showBadge = ( Boolean ) options .optBoolean ("showBadge" , false );
255
+ boolean vibration = options .optBoolean ("vibrate" , false );
256
+ boolean lights = options .optBoolean ("lights" , false );
257
+ boolean showBadge = options .optBoolean ("showBadge" , false );
285
258
int importanceVal = NotificationManager .IMPORTANCE_DEFAULT ;
286
259
if (importance .equals ("low" )) {
287
260
importanceVal = NotificationManager .IMPORTANCE_LOW ;
@@ -290,7 +263,7 @@ public void createNotificationChannel(KrollDict options) {
290
263
}
291
264
292
265
Uri soundUri = null ;
293
- if (sound .equals ("default" ) || sound .equals ( "" )) {
266
+ if (sound .equals ("default" ) || sound .isEmpty ( )) {
294
267
soundUri = RingtoneManager .getDefaultUri (RingtoneManager .TYPE_NOTIFICATION );
295
268
} else if (!sound .equals ("silent" )) {
296
269
soundUri = Utils .getSoundUri (sound );
@@ -321,7 +294,7 @@ public void deleteNotificationChannel(String channelId) {
321
294
}
322
295
Log .d (LCAT , "deleteNotificationChannel " + channelId );
323
296
324
- Context context = Utils . getApplicationContext ();
297
+ Context context = getApplicationContext ();
325
298
NotificationManager notificationManager =
326
299
(NotificationManager ) context .getSystemService (Context .NOTIFICATION_SERVICE );
327
300
assert notificationManager != null ;
@@ -338,25 +311,22 @@ public String fcmToken() {
338
311
}
339
312
}
340
313
341
- @ Kroll .setProperty
342
- public void apnsToken (String str ) {
343
- // empty
344
- }
345
-
346
314
// clang-format off
347
315
@ Kroll .setProperty
348
316
@ Kroll .method
349
317
public void setNotificationChannel (Object channel )
350
318
// clang-format on
351
319
{
352
- if (!(channel instanceof NotificationChannelProxy )) {
320
+ if (Build .VERSION .SDK_INT < Build .VERSION_CODES .O ) {
321
+ return ;
322
+ }
323
+
324
+ if (!(channel instanceof NotificationChannelProxy channelProxy )) {
353
325
return ;
354
326
}
355
327
356
- Context context = Utils .getApplicationContext ();
357
- NotificationChannelProxy channelProxy = (NotificationChannelProxy ) channel ;
358
328
NotificationManager notificationManager =
359
- (NotificationManager ) context .getSystemService (Context .NOTIFICATION_SERVICE );
329
+ (NotificationManager ) getApplicationContext () .getSystemService (Context .NOTIFICATION_SERVICE );
360
330
assert notificationManager != null ;
361
331
notificationManager .createNotificationChannel (channelProxy .getNotificationChannel ());
362
332
}
@@ -367,8 +337,7 @@ public void setNotificationChannel(Object channel)
367
337
public void setForceShowInForeground (final Boolean showInForeground )
368
338
// clang-format on
369
339
{
370
- Context context = Utils .getApplicationContext ();
371
- SharedPreferences prefs = PreferenceManager .getDefaultSharedPreferences (context );
340
+ SharedPreferences prefs = PreferenceManager .getDefaultSharedPreferences (getApplicationContext ());
372
341
373
342
SharedPreferences .Editor editor = prefs .edit ();
374
343
editor .putBoolean (FORCE_SHOW_IN_FOREGROUND , showInForeground );
@@ -377,8 +346,7 @@ public void setForceShowInForeground(final Boolean showInForeground)
377
346
378
347
@ Kroll .getProperty
379
348
public Boolean forceShowInForeground () {
380
- Context context = Utils .getApplicationContext ();
381
- SharedPreferences prefs = PreferenceManager .getDefaultSharedPreferences (context );
349
+ SharedPreferences prefs = PreferenceManager .getDefaultSharedPreferences (getApplicationContext ());
382
350
return prefs .getBoolean (FORCE_SHOW_IN_FOREGROUND , false );
383
351
}
384
352
@@ -391,7 +359,7 @@ public void parseBootIntent() {
391
359
Intent intent = TiApplication .getAppRootOrCurrentActivity ().getIntent ();
392
360
String notification = intent .getStringExtra ("fcm_data" );
393
361
if (notification != null ) {
394
- HashMap <String , Object > msg = new HashMap <String , Object >();
362
+ HashMap <String , Object > msg = new HashMap <>();
395
363
msg .put ("data" , new KrollDict (new JSONObject (notification )));
396
364
onMessageReceived (msg );
397
365
intent .removeExtra ("fcm_data" );
@@ -402,7 +370,7 @@ public void parseBootIntent() {
402
370
Log .e (LCAT , "parseBootIntent" + ex );
403
371
}
404
372
405
- SharedPreferences preferences = PreferenceManager .getDefaultSharedPreferences (Utils . getApplicationContext ());
373
+ SharedPreferences preferences = PreferenceManager .getDefaultSharedPreferences (getApplicationContext ());
406
374
preferences .edit ().remove ("titanium.firebase.cloudmessaging.message" ).apply ();
407
375
}
408
376
}
0 commit comments