Skip to content

Commit

Permalink
Merge pull request #625 from OneSignal/feature/add_external_user_id_c…
Browse files Browse the repository at this point in the history
…allback

Update external user id and set and remove methods
  • Loading branch information
mikechoch authored Apr 9, 2020
2 parents cd696c8 + 73fd6ac commit 6c0122f
Show file tree
Hide file tree
Showing 7 changed files with 120 additions and 100 deletions.
4 changes: 2 additions & 2 deletions plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
</engines>

<platform name="android">
<framework src="com.onesignal:OneSignal:3.12.7" />
<framework src="com.onesignal:OneSignal:3.13.1" />
<framework src="build-extras-onesignal.gradle" custom="true" type="gradleReference" />

<config-file target="res/xml/config.xml" parent="/*">
Expand Down Expand Up @@ -88,7 +88,7 @@
<string>production</string>
</config-file>

<framework src="OneSignal" type="podspec" spec="2.12.6" />
<framework src="OneSignal" type="podspec" spec="2.13.1" />
<header-file src="src/ios/OneSignalPush.h" />
<source-file src="src/ios/OneSignalPush.m" />

Expand Down
38 changes: 20 additions & 18 deletions src/android/com/plugin/gcm/CallbackHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,34 +5,36 @@
import org.apache.cordova.PluginResult;

public class CallbackHelper {

// This is to prevent an issue where if two Javascript calls are made to OneSignal expecting a callback then only one would fire.
public static void callbackSuccess(CallbackContext callbackContext, JSONObject jsonObject) {
if (jsonObject == null) // in case there are no data
jsonObject = new JSONObject();
if (jsonObject == null) // in case there are no data
jsonObject = new JSONObject();

PluginResult pluginResult = new PluginResult(PluginResult.Status.OK, jsonObject);
pluginResult.setKeepCallback(true);
callbackContext.sendPluginResult(pluginResult);
PluginResult pluginResult = new PluginResult(PluginResult.Status.OK, jsonObject);
pluginResult.setKeepCallback(true);
callbackContext.sendPluginResult(pluginResult);
}

public static void callbackSuccessBoolean(CallbackContext callbackContext, boolean param) {
PluginResult pluginResult = new PluginResult(PluginResult.Status.OK, param);
pluginResult.setKeepCallback(true);
callbackContext.sendPluginResult(pluginResult);
PluginResult pluginResult = new PluginResult(PluginResult.Status.OK, param);
pluginResult.setKeepCallback(true);
callbackContext.sendPluginResult(pluginResult);
}

public static void callbackError(CallbackContext callbackContext, JSONObject jsonObject) {
if (jsonObject == null) // in case there are no data
jsonObject = new JSONObject();
if (jsonObject == null) // in case there are no data
jsonObject = new JSONObject();

PluginResult pluginResult = new PluginResult(PluginResult.Status.ERROR, jsonObject);
pluginResult.setKeepCallback(true);
callbackContext.sendPluginResult(pluginResult);
PluginResult pluginResult = new PluginResult(PluginResult.Status.ERROR, jsonObject);
pluginResult.setKeepCallback(true);
callbackContext.sendPluginResult(pluginResult);
}

public static void callbackError(CallbackContext callbackContext, String str) {
PluginResult pluginResult = new PluginResult(PluginResult.Status.ERROR, str);
pluginResult.setKeepCallback(true);
callbackContext.sendPluginResult(pluginResult);
}
}
PluginResult pluginResult = new PluginResult(PluginResult.Status.ERROR, str);
pluginResult.setKeepCallback(true);
callbackContext.sendPluginResult(pluginResult);
}

}
67 changes: 36 additions & 31 deletions src/android/com/plugin/gcm/OneSignalController.java
Original file line number Diff line number Diff line change
Expand Up @@ -91,18 +91,18 @@ public static boolean postNotification(CallbackContext callbackContext, JSONArra
JSONObject jo = data.getJSONObject(0);
final CallbackContext jsPostNotificationCallBack = callbackContext;
OneSignal.postNotification(jo,
new PostNotificationResponseHandler() {
@Override
public void onSuccess(JSONObject response) {
CallbackHelper.callbackSuccess(jsPostNotificationCallBack, response);
}
@Override
public void onFailure(JSONObject response) {
CallbackHelper.callbackError(jsPostNotificationCallBack, response);
}
});
new PostNotificationResponseHandler() {
@Override
public void onSuccess(JSONObject response) {
CallbackHelper.callbackSuccess(jsPostNotificationCallBack, response);
}

@Override
public void onFailure(JSONObject response) {
CallbackHelper.callbackError(jsPostNotificationCallBack, response);
}
});

return true;
}
catch (Throwable t) {
Expand Down Expand Up @@ -158,7 +158,7 @@ public void idsAvailable(String userId, String registrationId) {
jsonIds.put("pushToken", registrationId);
else
jsonIds.put("pushToken", "");

CallbackHelper.callbackSuccess(jsIdsAvailableCallBack, jsonIds);
}
catch (Throwable t) {
Expand Down Expand Up @@ -191,14 +191,14 @@ public static boolean enableSound(JSONArray data) {
}
}

public static boolean setInFocusDisplaying(CallbackContext callbackContext, JSONArray data) {
public static boolean setInFocusDisplaying(JSONArray data) {
try {
OneSignal.setInFocusDisplaying(data.getInt(0));
return true;
}
catch (JSONException e) {
Log.e(TAG, "execute: Got JSON Exception " + e.getMessage());
return false;
Log.e(TAG, "execute: Got JSON Exception " + e.getMessage());
return false;
}
}

Expand Down Expand Up @@ -226,37 +226,42 @@ public static boolean setRequiresConsent(CallbackContext callbackContext, JSONAr
} catch (JSONException e) {
e.printStackTrace();
return false;
}
}
}

public static boolean grantConsent(JSONArray data) {
try {
OneSignal.provideUserConsent(data.getBoolean(0));
return true;
} catch (JSONException e) {
} catch (JSONException e) {
e.printStackTrace();
return false;
}
}
}

public static boolean setExternalUserId(JSONArray data) {
public static boolean setExternalUserId(final CallbackContext callback, JSONArray data) {
try {
OneSignal.setExternalUserId(data.getString(0));
OneSignal.setExternalUserId(data.getString(0), new OneSignal.OSExternalUserIdUpdateCompletionHandler() {
@Override
public void onComplete(JSONObject results) {
CallbackHelper.callbackSuccess(callback, results);
}
});
return true;
} catch (JSONException e) {
e.printStackTrace();
return false;
}
return false;
}

public static boolean removeExternalUserId() {
try {
OneSignal.removeExternalUserId();
return true;
}
catch(Throwable t) {
t.printStackTrace();
return false;
}
public static boolean removeExternalUserId(final CallbackContext callback) {
OneSignal.removeExternalUserId(new OneSignal.OSExternalUserIdUpdateCompletionHandler() {
@Override
public void onComplete(JSONObject results) {
CallbackHelper.callbackSuccess(callback, results);
}
});
return true;
}

}
62 changes: 31 additions & 31 deletions src/android/com/plugin/gcm/OneSignalPush.java
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
/**
* Modified MIT License
*
* Copyright 2017 OneSignal
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* 1. The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* 2. All copies of substantial portions of the Software may only be used in connection
* with services provided by OneSignal.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
* Modified MIT License
*
* Copyright 2017 OneSignal
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* 1. The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* 2. All copies of substantial portions of the Software may only be used in connection
* with services provided by OneSignal.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

package com.plugin.gcm;

Expand Down Expand Up @@ -174,7 +174,7 @@ public boolean execute(String action, JSONArray data, CallbackContext callbackCo
break;

case SET_IN_FOCUS_DISPLAYING:
result = OneSignalController.setInFocusDisplaying(callbackContext, data);
result = OneSignalController.setInFocusDisplaying(data);
break;

case ADD_PERMISSION_OBSERVER:
Expand Down Expand Up @@ -274,11 +274,11 @@ public boolean execute(String action, JSONArray data, CallbackContext callbackCo
break;

case SET_EXTERNAL_USER_ID:
result = OneSignalController.setExternalUserId(data);
result = OneSignalController.setExternalUserId(callbackContext, data);
break;

case REMOVE_EXTERNAL_USER_ID:
result = OneSignalController.removeExternalUserId();
result = OneSignalController.removeExternalUserId(callbackContext);
break;

case ADD_TRIGGERS:
Expand Down Expand Up @@ -309,9 +309,9 @@ public boolean execute(String action, JSONArray data, CallbackContext callbackCo
result = OneSignalOutcomeController.sendOutcomeWithValue(callbackContext, data);
break;

default:
Log.e(TAG, "Invalid action : " + action);
CallbackHelper.callbackError(callbackContext, "Invalid action : " + action);
default:
Log.e(TAG, "Invalid action : " + action);
CallbackHelper.callbackError(callbackContext, "Invalid action : " + action);
}

return result;
Expand Down
4 changes: 3 additions & 1 deletion src/ios/OneSignalPush.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
- (void)setInFocusDisplaying:(CDVInvokedUrlCommand*)command;
- (void)getPermissionSubscriptionState:(CDVInvokedUrlCommand*)command;


- (void)addPermissionObserver:(CDVInvokedUrlCommand*)command;
- (void)addSubscriptionObserver:(CDVInvokedUrlCommand*)command;
- (void)addEmailSubscriptionObserver:(CDVInvokedUrlCommand *)command;
Expand Down Expand Up @@ -71,6 +70,9 @@
- (void)userProvidedPrivacyConsent:(CDVInvokedUrlCommand *)command;
- (void)setRequiresUserPrivacyConsent:(CDVInvokedUrlCommand *)command;
- (void)provideUserConsent:(CDVInvokedUrlCommand *)command;

- (void)setExternalUserId:(CDVInvokedUrlCommand *)command;
- (void)removeExternalUserId:(CDVInvokedUrlCommand *)command;

// in app
- (void)setInAppMessageClickHandler:(CDVInvokedUrlCommand*)command;
Expand Down
28 changes: 15 additions & 13 deletions src/ios/OneSignalPush.m
Original file line number Diff line number Diff line change
Expand Up @@ -104,12 +104,11 @@ void initOneSignalObject(NSDictionary* launchOptions, const char* appId, int dis
notification = _notif;
if (pluginCommandDelegate)
processNotificationReceived(_notif);
}
handleNotificationAction:^(OSNotificationOpenedResult* openResult) {
actionNotification = openResult;
if (pluginCommandDelegate)
processNotificationOpened(openResult);
} settings: iOSSettings];
} handleNotificationAction:^(OSNotificationOpenedResult* openResult) {
actionNotification = openResult;
if (pluginCommandDelegate)
processNotificationOpened(openResult);
} settings: iOSSettings];

initialLaunchFired = true;
}
Expand Down Expand Up @@ -362,12 +361,15 @@ - (void)provideUserConsent:(CDVInvokedUrlCommand *)command {

- (void)setExternalUserId:(CDVInvokedUrlCommand *)command {
NSString *externalId = command.arguments[0];

[OneSignal setExternalUserId:externalId];
[OneSignal setExternalUserId:externalId withCompletion:^(NSDictionary *results) {
successCallback(command.callbackId, results);
}];
}

- (void)removeExternalUserId:(CDVInvokedUrlCommand *)command {
[OneSignal removeExternalUserId];
[OneSignal removeExternalUserId:^(NSDictionary *results) {
successCallback(command.callbackId, results);
}];
}

/**
Expand All @@ -377,10 +379,10 @@ - (void)removeExternalUserId:(CDVInvokedUrlCommand *)command {
- (void)setInAppMessageClickHandler:(CDVInvokedUrlCommand*)command {
[OneSignal setInAppMessageClickHandler:^(OSInAppMessageAction* action) {
NSDictionary *result = @{
@"click_name": action.clickName ?: [NSNull null],
@"click_url" : action.clickUrl.absoluteString ?: [NSNull null],
@"first_click" : @(action.firstClick),
@"closes_message" : @(action.closesMessage)
@"click_name": action.clickName ?: [NSNull null],
@"click_url" : action.clickUrl.absoluteString ?: [NSNull null],
@"first_click" : @(action.firstClick),
@"closes_message" : @(action.closesMessage)
};
successCallback(command.callbackId, result);
}
Expand Down
17 changes: 13 additions & 4 deletions www/OneSignal.js
Original file line number Diff line number Diff line change
Expand Up @@ -283,12 +283,21 @@ OneSignal.prototype.provideUserConsent = function(granted) {
cordova.exec(function() {}, function() {}, "OneSignalPush", "provideUserConsent", [granted]);
};

OneSignal.prototype.setExternalUserId = function(externalId) {
cordova.exec(function() {}, function() {}, "OneSignalPush", "setExternalUserId", [externalId]);
OneSignal.prototype.setExternalUserId = function(externalId, externalUserIdCallback) {
if (externalId == undefined)
externalId = null;

if (externalUserIdCallback == undefined)
externalUserIdCallback = function() {};

cordova.exec(externalUserIdCallback, function() {}, "OneSignalPush", "setExternalUserId", [externalId]);
};

OneSignal.prototype.removeExternalUserId = function() {
cordova.exec(function() {}, function() {}, "OneSignalPush", "removeExternalUserId", []);
OneSignal.prototype.removeExternalUserId = function(externalUserIdCallback) {
if (externalUserIdCallback == undefined)
externalUserIdCallback = function() {};

cordova.exec(externalUserIdCallback, function() {}, "OneSignalPush", "removeExternalUserId", []);
};

/**
Expand Down

0 comments on commit 6c0122f

Please sign in to comment.