Skip to content

Commit

Permalink
feat(initSessionWithCallback): add new method
Browse files Browse the repository at this point in the history
  • Loading branch information
MaximBelov committed Apr 29, 2021
1 parent d16b4ff commit 5f755b5
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 6 deletions.
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

0 comments on commit 5f755b5

Please sign in to comment.