2525import org .json .JSONException ;
2626import org .json .JSONObject ;
2727
28+ import static com .azure .reactnative .notificationhub .ReactNativeNotificationsHandler .ERROR_ACTIVITY_CLASS_NOT_FOUND ;
2829import static com .azure .reactnative .notificationhub .ReactNativeNotificationsHandler .ERROR_COVERT_ACTIONS ;
2930import static com .azure .reactnative .notificationhub .ReactNativeNotificationsHandler .ERROR_GET_ACTIONS_ARRAY ;
3031import static com .azure .reactnative .notificationhub .ReactNativeNotificationsHandler .KEY_INTENT_NOTIFICATION ;
@@ -50,22 +51,36 @@ public class NotificationHubUtil {
5051
5152 private static NotificationHubUtil sharedNotificationHubUtilInstance = null ;
5253
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" ;
6465
6566 private final ExecutorService mPool = Executors .newFixedThreadPool (1 );
6667
6768 private boolean mIsForeground ;
6869
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+
6984 public static NotificationHubUtil getInstance () {
7085 if (sharedNotificationHubUtilInstance == null ) {
7186 sharedNotificationHubUtilInstance = new NotificationHubUtil ();
@@ -202,7 +217,7 @@ public JSONObject convertBundleToJSON(Bundle bundle) {
202217 }
203218
204219 public Intent createBroadcastIntent (String action , JSONObject json ) {
205- Intent intent = new Intent (action );
220+ Intent intent = IntentFactory . createIntent (action );
206221 intent .putExtra ("event" , ReactNativeNotificationHubModule .DEVICE_NOTIF_EVENT );
207222 intent .putExtra ("data" , json .toString ());
208223
@@ -216,7 +231,7 @@ public Class getMainActivityClass(Context context) {
216231 try {
217232 return Class .forName (className );
218233 } catch (ClassNotFoundException e ) {
219- e . printStackTrace ( );
234+ Log . e ( TAG , ERROR_ACTIVITY_CLASS_NOT_FOUND , e );
220235 return null ;
221236 }
222237 }
@@ -289,10 +304,8 @@ public Uri getSoundUri(Context context, Bundle bundle) {
289304 // So the strings 'my_sound.mp3' AND 'my_sound' are accepted
290305 // The reason is to make the iOS and android javascript interfaces compatible
291306
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 ) {
296309 soundName = soundName .substring (0 , soundName .lastIndexOf ('.' ));
297310 resId = context .getResources ().getIdentifier (soundName , RESOURCE_DEF_TYPE_RAW , context .getPackageName ());
298311 }
@@ -305,7 +318,7 @@ public Uri getSoundUri(Context context, Bundle bundle) {
305318 }
306319
307320 public Intent createNotificationIntent (Context context , Bundle bundle , Class intentClass ) {
308- Intent intent = new Intent (context , intentClass );
321+ Intent intent = IntentFactory . createIntent (context , intentClass );
309322 intent .addFlags (Intent .FLAG_ACTIVITY_SINGLE_TOP );
310323 bundle .putBoolean (KEY_REMOTE_NOTIFICATION_FOREGROUND , true );
311324 bundle .putBoolean (KEY_REMOTE_NOTIFICATION_USER_INTERACTION , false );
@@ -340,7 +353,7 @@ public void processNotificationActions(Context context, Bundle bundle,
340353 continue ;
341354 }
342355
343- Intent actionIntent = new Intent ();
356+ Intent actionIntent = IntentFactory . createIntent ();
344357 actionIntent .setAction (context .getPackageName () + "." + action );
345358 // Add "action" for later identifying which button gets pressed.
346359 bundle .putString (KEY_REMOTE_NOTIFICATION_ACTION , action );
0 commit comments