Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(initSessionWithCallback): add new method #686

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
43 changes: 37 additions & 6 deletions src/android/io/branch/BranchSDK.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ static class BranchLinkProperties extends io.branch.referral.util.LinkProperties
private Activity activity;
private Branch instance;
private String deepLinkUrl;
private CallbackContext initSessionCallbackContext;
private boolean initSessionCallbackContextKeepCallback;

/**
* Class Constructor
Expand All @@ -61,7 +63,8 @@ public BranchSDK() {
this.activity = null;
this.instance = null;
this.branchObjectWrappers = new ArrayList<BranchUniversalObjectWrapper>();

this.initSessionCallbackContext = null;
this.initSessionCallbackContextKeepCallback = false;
}

/**
Expand All @@ -84,6 +87,7 @@ protected void pluginInitialize() {
public void onNewIntent(Intent intent) {
intent.putExtra("branch_force_new_session", true);
this.activity.setIntent(intent);
Branch.sessionBuilder(this.activity).withCallback(branchReferralInitListener).reInit();
}

/**
Expand Down Expand Up @@ -279,7 +283,7 @@ public void lastAttributedTouchData(CallbackContext callbackContext) {
*
* @param callbackContext A callback to execute at the end of this method
*/
private void initSession(CallbackContext callbackContext) {
private void initSession(boolean isKeepCallBack, CallbackContext callbackContext) {

this.activity = this.cordova.getActivity();

Expand All @@ -289,9 +293,35 @@ private void initSession(CallbackContext callbackContext) {
this.deepLinkUrl = data.toString();
}

this.instance.initSession(new SessionListener(callbackContext), data, activity);
this.initSessionCallbackContext = callbackContext;
this.initSessionCallbackContextKeepCallback = isKeepCallBack;

Branch.sessionBuilder(activity).withCallback(branchReferralInitListener).withData(data).init();
}

private Branch.BranchReferralInitListener branchReferralInitListener = new Branch.BranchReferralInitListener() {
@Override
public void onInitFinished(JSONObject referringParams, BranchError error) {
if (error == null && referringParams != null && initSessionCallbackContext != null) {
PluginResult result = new PluginResult(PluginResult.Status.OK, referringParams);
if(initSessionCallbackContextKeepCallback){
result.setKeepCallback(true);
}
initSessionCallbackContext.sendPluginResult(result);
} else {
JSONObject message = new JSONObject();
try {
message.put("error", error.getMessage());
} catch (JSONException e) {
e.printStackTrace();
}
if (initSessionCallbackContext != null) {
initSessionCallbackContext.error(message);
}
}
}
};

/**
* <p>This method should be called if you know that a different person is about to use the app. For example,
* if you allow users to log out and let their friend use the app, you should call this to notify Branch
Expand Down Expand Up @@ -618,7 +648,7 @@ private void setCookieBasedMatching(String linkDomain, CallbackContext callbackC
*/
private void setDebug(boolean isEnable, CallbackContext callbackContext) {
this.activity = this.cordova.getActivity();
Branch.enableDebugMode();
Branch.enableLogging();
callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, isEnable));
}

Expand Down Expand Up @@ -653,7 +683,7 @@ private void setIdentity(String newIdentity, CallbackContext callbackContext) {
/**
* <p>Allow Branch SDK to pass the user's Mixpanel distinct id to our servers. Branch will then pass that Distinct ID to Mixpanel when logging any event.</p>
*
* @param token A {@link String} value containing the unique identifier of the Mixpanel user.
* @param key A {@link String} value containing the unique identifier of the Mixpanel user.
* @param callbackContext A callback to execute at the end of this method
*/
private void setRequestMetadata(String key, String val, CallbackContext callbackContext) {
Expand Down Expand Up @@ -1423,7 +1453,8 @@ public void run() {
} else if (this.action.equals("disableTracking")) {
disableTracking(this.args.getBoolean(0), this.callbackContext);
} else if (this.action.equals("initSession")) {
initSession(this.callbackContext);
boolean keepCallBack = this.args.length() != 0 && this.args.getBoolean(0);
initSession(keepCallBack, this.callbackContext);
} else if (this.action.equals("setRequestMetadata")) {
setRequestMetadata(this.args.getString(0), this.args.getString(1), this.callbackContext);
} else {
Expand Down
8 changes: 8 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,14 @@ Branch.prototype.initSession = function initSession() {
return execute("initSession");
};

Branch.prototype.initSessionWithCallback = function initSession(onSuccess, onFail) {
this.sessionInitialized = true;
if (!onSuccess || typeof onSuccess !== "function") {
return executeReject("Please set onSuccess callback");
}
return executeCallback("initSession", onSuccess, [true]);
};

Branch.prototype.setRequestMetadata = function setRequestMetadata(key, val) {
if (!key || typeof key !== "string") {
return executeReject("Please set key");
Expand Down
4 changes: 4 additions & 0 deletions src/ios/BranchSDK.m
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ - (void)initSession:(CDVInvokedUrlCommand*)command

NSString *resultString = nil;
CDVPluginResult *pluginResult = nil;
bool enableCallBack = [[command.arguments objectAtIndex:0] boolValue];

if (!error) {
if (params != nil && [params count] > 0) {
Expand Down Expand Up @@ -130,6 +131,9 @@ - (void)initSession:(CDVInvokedUrlCommand*)command
}

if (command != nil) {
if(enableCallBack){
[pluginResult setKeepCallbackAsBool:YES];
}
[self.commandDelegate sendPluginResult: pluginResult callbackId: command.callbackId];
}
}];
Expand Down