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 #719

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 15 additions & 5 deletions src/android/io/branch/BranchSDK.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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();
}

/**
Expand Down Expand Up @@ -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();

Expand All @@ -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();
}

/**
Expand Down Expand Up @@ -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
Expand All @@ -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();
Expand Down Expand Up @@ -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 {
Expand Down
8 changes: 8 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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");
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