diff --git a/src/android/io/branch/BranchSDK.java b/src/android/io/branch/BranchSDK.java index 12f9c89b..80b4c3ec 100644 --- a/src/android/io/branch/BranchSDK.java +++ b/src/android/io/branch/BranchSDK.java @@ -47,6 +47,7 @@ static class BranchLinkProperties extends io.branch.referral.util.LinkProperties private Activity activity; private Branch instance; private String deepLinkUrl; + private Branch.BranchReferralInitListener branchReferralInitListener; /** * Class Constructor @@ -79,6 +80,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(); } /** @@ -226,7 +228,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(); @@ -236,7 +238,8 @@ private void initSession(CallbackContext callbackContext) { this.deepLinkUrl = data.toString(); } - Branch.sessionBuilder(activity).withData(data).withCallback(new SessionListener(callbackContext)).init(); + this.branchReferralInitListener = new SessionListener(callbackContext, isKeepCallBack); + Branch.sessionBuilder(activity).withData(data).withCallback(branchReferralInitListener).init(); } /** @@ -774,9 +777,11 @@ public void onDataFetched(JSONObject jsonObject, BranchError error) { protected class SessionListener implements Branch.BranchReferralInitListener { private CallbackContext _callbackContext; + private Boolean _keepCallback; - public SessionListener(CallbackContext callbackContext) { + public SessionListener(CallbackContext callbackContext, Boolean keepCallback) { this._callbackContext = callbackContext; + this._keepCallback = keepCallback; } //Listener that implements BranchReferralInitListener for initSession @@ -787,7 +792,11 @@ public void onInitFinished(JSONObject referringParams, BranchError error) { if (error == null && referringParams != null) { if (this._callbackContext != null) { - this._callbackContext.success(referringParams); + PluginResult result = new PluginResult(PluginResult.Status.OK, referringParams); + if(this._keepCallback){ + result.setKeepCallback(true); + } + this._callbackContext.sendPluginResult(result); } } else { JSONObject message = new JSONObject(); @@ -1086,7 +1095,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 { diff --git a/src/index.js b/src/index.js index a82a98b5..1ad56cdf 100644 --- a/src/index.js +++ b/src/index.js @@ -103,6 +103,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"); diff --git a/src/ios/BranchSDK.m b/src/ios/BranchSDK.m index 8148ce5c..47977757 100644 --- a/src/ios/BranchSDK.m +++ b/src/ios/BranchSDK.m @@ -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) { @@ -130,6 +131,9 @@ - (void)initSession:(CDVInvokedUrlCommand*)command } if (command != nil) { + if(enableCallBack){ + [pluginResult setKeepCallbackAsBool:YES]; + } [self.commandDelegate sendPluginResult: pluginResult callbackId: command.callbackId]; } }];