diff --git a/src/android/com/onesignal/cordova/OneSignalObserverController.java b/src/android/com/onesignal/cordova/OneSignalObserverController.java index 57ab7d53..88e298bd 100644 --- a/src/android/com/onesignal/cordova/OneSignalObserverController.java +++ b/src/android/com/onesignal/cordova/OneSignalObserverController.java @@ -76,15 +76,15 @@ public static boolean addUserStateObserver(CallbackContext callbackContext) { userStateObserver = new IUserStateObserver() { @Override public void onUserStateChange(UserChangedState state) { - UserState user = state.getCurrent(); + UserState current = state.getCurrent(); - if (!(user instanceof UserState)) { + if (!(current instanceof UserState)) { return; } try { JSONObject hash = new JSONObject(); - hash.put("current", createUserIds(state.getCurrent())); + hash.put("current", createUserIds(current)); CallbackHelper.callbackSuccess(jsUserObserverCallBack, hash); @@ -98,14 +98,23 @@ public void onUserStateChange(UserChangedState state) { return true; } + /** Helper method to return JSONObject.NULL if string is empty or nil **/ + private static Object getStringOrJSONObjectNull(String str) { + if (str != null && !str.isEmpty()) { + return str; + } else { + return JSONObject.NULL; + } + } + private static JSONObject createUserIds(UserState user) { JSONObject userIds = new JSONObject(); try { String externalId = user.getExternalId(); String onesignalId = user.getOnesignalId(); - userIds.put("externalId", externalId != null && !externalId.isEmpty() ? externalId : JSONObject.NULL); - userIds.put("onesignalId", onesignalId != null && !onesignalId.isEmpty() ? onesignalId : JSONObject.NULL); + userIds.put("externalId", getStringOrJSONObjectNull(externalId)); + userIds.put("onesignalId", getStringOrJSONObjectNull(onesignalId)); } catch (JSONException e) { e.printStackTrace(); } diff --git a/src/ios/OneSignalPush.m b/src/ios/OneSignalPush.m index 8fc9f7e4..edc0ea50 100644 --- a/src/ios/OneSignalPush.m +++ b/src/ios/OneSignalPush.m @@ -105,6 +105,16 @@ void initOneSignalObject(NSDictionary* launchOptions) { initialLaunchFired = true; } +/** Helper method to return NSNull if string is empty or nil **/ +NSString* getStringOrNSNull(NSString* string) { + // length method can be used on nil and strings + if (string.length > 0) { + return string; + } else { + return [NSNull null]; + } +} + @implementation UIApplication(OneSignalCordovaPush) static void injectSelectorCordova(Class newClass, SEL newSel, Class addToClass, SEL makeLikeSel) { @@ -174,18 +184,8 @@ - (void)onUserStateDidChangeWithState:(OSUserChangedState * _Nonnull)state { NSMutableDictionary *currentObject = [NSMutableDictionary new]; - if (onesignalId && ![onesignalId isEqualToString:@""]) { - currentObject[@"onesignalId"] = onesignalId; - } else { - currentObject[@"onesignalId"] = [NSNull null]; - } - - if (externalId && ![externalId isEqualToString:@""]) { - currentObject[@"externalId"] = externalId; - } else { - currentObject[@"externalId"] = [NSNull null]; - } - + currentObject[@"onesignalId"] = getStringOrNSNull(onesignalId); + currentObject[@"externalId"] = getStringOrNSNull(externalId); result[@"current"] = currentObject; successCallback(userObserverCallbackId, result);