25
25
import org .json .JSONException ;
26
26
import org .json .JSONObject ;
27
27
28
+ import static com .azure .reactnative .notificationhub .ReactNativeNotificationsHandler .ERROR_ACTIVITY_CLASS_NOT_FOUND ;
28
29
import static com .azure .reactnative .notificationhub .ReactNativeNotificationsHandler .ERROR_COVERT_ACTIONS ;
29
30
import static com .azure .reactnative .notificationhub .ReactNativeNotificationsHandler .ERROR_GET_ACTIONS_ARRAY ;
30
31
import static com .azure .reactnative .notificationhub .ReactNativeNotificationsHandler .KEY_INTENT_NOTIFICATION ;
@@ -50,22 +51,36 @@ public class NotificationHubUtil {
50
51
51
52
private static NotificationHubUtil sharedNotificationHubUtilInstance = null ;
52
53
53
- private static final String SHARED_PREFS_NAME = "com.azure.reactnative.notificationhub.NotificationHubUtil" ;
54
- private static final String KEY_FOR_PREFS_REGISTRATIONID = "AzureNotificationHub_registrationID" ;
55
- private static final String KEY_FOR_PREFS_CONNECTIONSTRING = "AzureNotificationHub_connectionString" ;
56
- private static final String KEY_FOR_PREFS_HUBNAME = "AzureNotificationHub_hubName" ;
57
- private static final String KEY_FOR_PREFS_FCMTOKEN = "AzureNotificationHub_FCMToken" ;
58
- private static final String KEY_FOR_PREFS_TAGS = "AzureNotificationHub_Tags" ;
59
- private static final String KEY_FOR_PREFS_SENDERID = "AzureNotificationHub_senderID" ;
60
- private static final String KEY_FOR_PREFS_CHANNELIMPORTANCE = "AzureNotificationHub_channelImportance" ;
61
- private static final String KEY_FOR_PREFS_CHANNELSHOWBADGE = "AzureNotificationHub_channelShowBadge" ;
62
- private static final String KEY_FOR_PREFS_CHANNELENABLELIGHTS = "AzureNotificationHub_channelEnableLights" ;
63
- private static final String KEY_FOR_PREFS_CHANNELENABLEVIBRATION = "AzureNotificationHub_channelEnableVibration" ;
54
+ public static final String SHARED_PREFS_NAME = "com.azure.reactnative.notificationhub.NotificationHubUtil" ;
55
+ public static final String KEY_FOR_PREFS_REGISTRATIONID = "AzureNotificationHub_registrationID" ;
56
+ public static final String KEY_FOR_PREFS_CONNECTIONSTRING = "AzureNotificationHub_connectionString" ;
57
+ public static final String KEY_FOR_PREFS_HUBNAME = "AzureNotificationHub_hubName" ;
58
+ public static final String KEY_FOR_PREFS_FCMTOKEN = "AzureNotificationHub_FCMToken" ;
59
+ public static final String KEY_FOR_PREFS_TAGS = "AzureNotificationHub_Tags" ;
60
+ public static final String KEY_FOR_PREFS_SENDERID = "AzureNotificationHub_senderID" ;
61
+ public static final String KEY_FOR_PREFS_CHANNELIMPORTANCE = "AzureNotificationHub_channelImportance" ;
62
+ public static final String KEY_FOR_PREFS_CHANNELSHOWBADGE = "AzureNotificationHub_channelShowBadge" ;
63
+ public static final String KEY_FOR_PREFS_CHANNELENABLELIGHTS = "AzureNotificationHub_channelEnableLights" ;
64
+ public static final String KEY_FOR_PREFS_CHANNELENABLEVIBRATION = "AzureNotificationHub_channelEnableVibration" ;
64
65
65
66
private final ExecutorService mPool = Executors .newFixedThreadPool (1 );
66
67
67
68
private boolean mIsForeground ;
68
69
70
+ public static class IntentFactory {
71
+ public static Intent createIntent () {
72
+ return new Intent ();
73
+ }
74
+
75
+ public static Intent createIntent (String action ) {
76
+ return new Intent (action );
77
+ }
78
+
79
+ public static Intent createIntent (Context context , Class intentClass ) {
80
+ return new Intent (context , intentClass );
81
+ }
82
+ }
83
+
69
84
public static NotificationHubUtil getInstance () {
70
85
if (sharedNotificationHubUtilInstance == null ) {
71
86
sharedNotificationHubUtilInstance = new NotificationHubUtil ();
@@ -202,7 +217,7 @@ public JSONObject convertBundleToJSON(Bundle bundle) {
202
217
}
203
218
204
219
public Intent createBroadcastIntent (String action , JSONObject json ) {
205
- Intent intent = new Intent (action );
220
+ Intent intent = IntentFactory . createIntent (action );
206
221
intent .putExtra ("event" , ReactNativeNotificationHubModule .DEVICE_NOTIF_EVENT );
207
222
intent .putExtra ("data" , json .toString ());
208
223
@@ -216,7 +231,7 @@ public Class getMainActivityClass(Context context) {
216
231
try {
217
232
return Class .forName (className );
218
233
} catch (ClassNotFoundException e ) {
219
- e . printStackTrace ( );
234
+ Log . e ( TAG , ERROR_ACTIVITY_CLASS_NOT_FOUND , e );
220
235
return null ;
221
236
}
222
237
}
@@ -289,10 +304,8 @@ public Uri getSoundUri(Context context, Bundle bundle) {
289
304
// So the strings 'my_sound.mp3' AND 'my_sound' are accepted
290
305
// The reason is to make the iOS and android javascript interfaces compatible
291
306
292
- int resId ;
293
- if (context .getResources ().getIdentifier (soundName , RESOURCE_DEF_TYPE_RAW , context .getPackageName ()) != 0 ) {
294
- resId = context .getResources ().getIdentifier (soundName , RESOURCE_DEF_TYPE_RAW , context .getPackageName ());
295
- } else {
307
+ int resId = context .getResources ().getIdentifier (soundName , RESOURCE_DEF_TYPE_RAW , context .getPackageName ());
308
+ if (resId == 0 ) {
296
309
soundName = soundName .substring (0 , soundName .lastIndexOf ('.' ));
297
310
resId = context .getResources ().getIdentifier (soundName , RESOURCE_DEF_TYPE_RAW , context .getPackageName ());
298
311
}
@@ -305,7 +318,7 @@ public Uri getSoundUri(Context context, Bundle bundle) {
305
318
}
306
319
307
320
public Intent createNotificationIntent (Context context , Bundle bundle , Class intentClass ) {
308
- Intent intent = new Intent (context , intentClass );
321
+ Intent intent = IntentFactory . createIntent (context , intentClass );
309
322
intent .addFlags (Intent .FLAG_ACTIVITY_SINGLE_TOP );
310
323
bundle .putBoolean (KEY_REMOTE_NOTIFICATION_FOREGROUND , true );
311
324
bundle .putBoolean (KEY_REMOTE_NOTIFICATION_USER_INTERACTION , false );
@@ -340,7 +353,7 @@ public void processNotificationActions(Context context, Bundle bundle,
340
353
continue ;
341
354
}
342
355
343
- Intent actionIntent = new Intent ();
356
+ Intent actionIntent = IntentFactory . createIntent ();
344
357
actionIntent .setAction (context .getPackageName () + "." + action );
345
358
// Add "action" for later identifying which button gets pressed.
346
359
bundle .putString (KEY_REMOTE_NOTIFICATION_ACTION , action );
0 commit comments