77 */
88package firebase .cloudmessaging ;
99
10+ import static firebase .cloudmessaging .Utils .getApplicationContext ;
11+
1012import android .app .NotificationChannel ;
1113import android .app .NotificationManager ;
1214import android .content .Context ;
1921import android .os .Build ;
2022import android .os .Bundle ;
2123
22- import androidx .annotation .NonNull ;
2324import androidx .preference .PreferenceManager ;
2425
25- import com .google .android .gms .tasks .OnCompleteListener ;
26- import com .google .android .gms .tasks .Task ;
2726import com .google .firebase .messaging .FirebaseMessaging ;
2827import com .google .firebase .messaging .RemoteMessage ;
2928
3231import org .appcelerator .kroll .annotations .Kroll ;
3332import org .appcelerator .kroll .common .Log ;
3433import org .appcelerator .titanium .TiApplication ;
35- import org .appcelerator .titanium .TiBaseActivity ;
3634import org .appcelerator .titanium .util .TiConvert ;
3735import org .json .JSONObject ;
3836
@@ -55,11 +53,6 @@ public CloudMessagingModule() {
5553 instance = this ;
5654 }
5755
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-
6356 public static CloudMessagingModule getInstance () {
6457 return instance ;
6558 }
@@ -78,28 +71,30 @@ private KrollDict getLastData()
7871
7972 if (extras != null ) {
8073 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 ) {
8375 for (String bdnlKey : bndl .keySet ()) {
8476 data .put (key + "_" + bdnlKey , bndl .get (bdnlKey ));
8577 }
8678 } 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+ }
8883 }
8984 }
9085
9186 data .put ("inBackground" , true );
9287 } else {
9388 Log .d (LCAT , "Empty extras in Intent" );
94- if (!notificationData .equals ( "" )) {
89+ if (!notificationData .isEmpty ( )) {
9590 data = new KrollDict (new JSONObject (notificationData ));
9691 data .put ("inBackground" , true );
9792 }
9893 }
9994
10095 if (data .get ("message" ) == null ) {
10196 SharedPreferences preferences =
102- PreferenceManager .getDefaultSharedPreferences (Utils . getApplicationContext ());
97+ PreferenceManager .getDefaultSharedPreferences (getApplicationContext ());
10398 String prefMessage = preferences .getString ("titanium.firebase.cloudmessaging.message" , null );
10499 if (prefMessage != null ) {
105100 data .put ("message" , new KrollDict (new JSONObject (prefMessage )));
@@ -117,7 +112,7 @@ private KrollDict getLastData()
117112 @ Kroll .method
118113 public void registerForPushNotifications () {
119114 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 ) {
121116 fireEvent ("success" , new KrollDict ());
122117 getToken ();
123118 } else {
@@ -132,34 +127,20 @@ public void registerForPushNotifications() {
132127
133128 @ Kroll .method
134129 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 );
146134 });
147135 Log .d (LCAT , "subscribe to " + topic );
148136 }
149137
150138 @ Kroll .method
151139 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 );
163144 });
164145 Log .d (LCAT , "unsubscribe from " + topic );
165146 }
@@ -172,7 +153,7 @@ public void appDidReceiveMessage(KrollDict opt) {
172153 @ Kroll .method
173154 public void clearLastData () {
174155 SharedPreferences preferences =
175- PreferenceManager .getDefaultSharedPreferences (Utils . getApplicationContext ());
156+ PreferenceManager .getDefaultSharedPreferences (getApplicationContext ());
176157 preferences .edit ().remove ("titanium.firebase.cloudmessaging.message" ).apply ();
177158
178159 // remove intent value
@@ -186,34 +167,26 @@ public void clearLastData() {
186167 @ Kroll .method
187168 public void getToken () {
188169 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 );
198176 }
199177 });
200178 }
201179
202180 @ Kroll .method
203181 public void deleteToken () {
204182 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 ;
216187 }
188+ data .put ("success" , task .isSuccessful ());
189+ fireEvent ("tokenRemoved" , data );
217190 });
218191 }
219192
@@ -232,11 +205,11 @@ public void sendMessage(KrollDict obj) {
232205 // add custom data
233206 Map <String , String > data = (HashMap ) obj .get ("data" );
234207 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 ));
237210 }
238211
239- if (!fireTo .equals ( "" ) && !fireMessageId .equals ( "" )) {
212+ if (!fireTo .isEmpty ( ) && !fireMessageId .isEmpty ( )) {
240213 fm .send (rm .build ());
241214 } else {
242215 Log .e (LCAT , "Please set 'to' and 'messageId'" );
@@ -256,7 +229,7 @@ public void onTokenRefresh(String token) {
256229 }
257230 }
258231
259- public void onMessageReceived (HashMap message ) {
232+ public void onMessageReceived (HashMap < String , Object > message ) {
260233 try {
261234 if (hasListeners ("didReceiveMessage" )) {
262235 KrollDict data = new KrollDict ();
@@ -274,14 +247,14 @@ public void createNotificationChannel(KrollDict options) {
274247 return ;
275248 }
276249 Log .d (LCAT , "createNotificationChannel " + options .toString ());
277- Context context = Utils . getApplicationContext ();
250+ Context context = getApplicationContext ();
278251 String sound = options .optString ("sound" , "default" );
279252 String importance = options .optString ("importance" , sound .equals ("silent" ) ? "low" : "default" );
280253 String channelId = options .optString ("channelId" , "default" );
281254 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 );
285258 int importanceVal = NotificationManager .IMPORTANCE_DEFAULT ;
286259 if (importance .equals ("low" )) {
287260 importanceVal = NotificationManager .IMPORTANCE_LOW ;
@@ -290,7 +263,7 @@ public void createNotificationChannel(KrollDict options) {
290263 }
291264
292265 Uri soundUri = null ;
293- if (sound .equals ("default" ) || sound .equals ( "" )) {
266+ if (sound .equals ("default" ) || sound .isEmpty ( )) {
294267 soundUri = RingtoneManager .getDefaultUri (RingtoneManager .TYPE_NOTIFICATION );
295268 } else if (!sound .equals ("silent" )) {
296269 soundUri = Utils .getSoundUri (sound );
@@ -321,7 +294,7 @@ public void deleteNotificationChannel(String channelId) {
321294 }
322295 Log .d (LCAT , "deleteNotificationChannel " + channelId );
323296
324- Context context = Utils . getApplicationContext ();
297+ Context context = getApplicationContext ();
325298 NotificationManager notificationManager =
326299 (NotificationManager ) context .getSystemService (Context .NOTIFICATION_SERVICE );
327300 assert notificationManager != null ;
@@ -338,25 +311,22 @@ public String fcmToken() {
338311 }
339312 }
340313
341- @ Kroll .setProperty
342- public void apnsToken (String str ) {
343- // empty
344- }
345-
346314 // clang-format off
347315 @ Kroll .setProperty
348316 @ Kroll .method
349317 public void setNotificationChannel (Object channel )
350318 // clang-format on
351319 {
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 )) {
353325 return ;
354326 }
355327
356- Context context = Utils .getApplicationContext ();
357- NotificationChannelProxy channelProxy = (NotificationChannelProxy ) channel ;
358328 NotificationManager notificationManager =
359- (NotificationManager ) context .getSystemService (Context .NOTIFICATION_SERVICE );
329+ (NotificationManager ) getApplicationContext () .getSystemService (Context .NOTIFICATION_SERVICE );
360330 assert notificationManager != null ;
361331 notificationManager .createNotificationChannel (channelProxy .getNotificationChannel ());
362332 }
@@ -367,8 +337,7 @@ public void setNotificationChannel(Object channel)
367337 public void setForceShowInForeground (final Boolean showInForeground )
368338 // clang-format on
369339 {
370- Context context = Utils .getApplicationContext ();
371- SharedPreferences prefs = PreferenceManager .getDefaultSharedPreferences (context );
340+ SharedPreferences prefs = PreferenceManager .getDefaultSharedPreferences (getApplicationContext ());
372341
373342 SharedPreferences .Editor editor = prefs .edit ();
374343 editor .putBoolean (FORCE_SHOW_IN_FOREGROUND , showInForeground );
@@ -377,8 +346,7 @@ public void setForceShowInForeground(final Boolean showInForeground)
377346
378347 @ Kroll .getProperty
379348 public Boolean forceShowInForeground () {
380- Context context = Utils .getApplicationContext ();
381- SharedPreferences prefs = PreferenceManager .getDefaultSharedPreferences (context );
349+ SharedPreferences prefs = PreferenceManager .getDefaultSharedPreferences (getApplicationContext ());
382350 return prefs .getBoolean (FORCE_SHOW_IN_FOREGROUND , false );
383351 }
384352
@@ -391,7 +359,7 @@ public void parseBootIntent() {
391359 Intent intent = TiApplication .getAppRootOrCurrentActivity ().getIntent ();
392360 String notification = intent .getStringExtra ("fcm_data" );
393361 if (notification != null ) {
394- HashMap <String , Object > msg = new HashMap <String , Object >();
362+ HashMap <String , Object > msg = new HashMap <>();
395363 msg .put ("data" , new KrollDict (new JSONObject (notification )));
396364 onMessageReceived (msg );
397365 intent .removeExtra ("fcm_data" );
@@ -402,7 +370,7 @@ public void parseBootIntent() {
402370 Log .e (LCAT , "parseBootIntent" + ex );
403371 }
404372
405- SharedPreferences preferences = PreferenceManager .getDefaultSharedPreferences (Utils . getApplicationContext ());
373+ SharedPreferences preferences = PreferenceManager .getDefaultSharedPreferences (getApplicationContext ());
406374 preferences .edit ().remove ("titanium.firebase.cloudmessaging.message" ).apply ();
407375 }
408376}
0 commit comments