Skip to content

Commit 5f755b5

Browse files
committed
feat(initSessionWithCallback): add new method
1 parent d16b4ff commit 5f755b5

File tree

3 files changed

+49
-6
lines changed

3 files changed

+49
-6
lines changed

src/android/io/branch/BranchSDK.java

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ static class BranchLinkProperties extends io.branch.referral.util.LinkProperties
5252
private Activity activity;
5353
private Branch instance;
5454
private String deepLinkUrl;
55+
private CallbackContext initSessionCallbackContext;
56+
private boolean initSessionCallbackContextKeepCallback;
5557

5658
/**
5759
* Class Constructor
@@ -61,7 +63,8 @@ public BranchSDK() {
6163
this.activity = null;
6264
this.instance = null;
6365
this.branchObjectWrappers = new ArrayList<BranchUniversalObjectWrapper>();
64-
66+
this.initSessionCallbackContext = null;
67+
this.initSessionCallbackContextKeepCallback = false;
6568
}
6669

6770
/**
@@ -84,6 +87,7 @@ protected void pluginInitialize() {
8487
public void onNewIntent(Intent intent) {
8588
intent.putExtra("branch_force_new_session", true);
8689
this.activity.setIntent(intent);
90+
Branch.sessionBuilder(this.activity).withCallback(branchReferralInitListener).reInit();
8791
}
8892

8993
/**
@@ -279,7 +283,7 @@ public void lastAttributedTouchData(CallbackContext callbackContext) {
279283
*
280284
* @param callbackContext A callback to execute at the end of this method
281285
*/
282-
private void initSession(CallbackContext callbackContext) {
286+
private void initSession(boolean isKeepCallBack, CallbackContext callbackContext) {
283287

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

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

292-
this.instance.initSession(new SessionListener(callbackContext), data, activity);
296+
this.initSessionCallbackContext = callbackContext;
297+
this.initSessionCallbackContextKeepCallback = isKeepCallBack;
298+
299+
Branch.sessionBuilder(activity).withCallback(branchReferralInitListener).withData(data).init();
293300
}
294301

302+
private Branch.BranchReferralInitListener branchReferralInitListener = new Branch.BranchReferralInitListener() {
303+
@Override
304+
public void onInitFinished(JSONObject referringParams, BranchError error) {
305+
if (error == null && referringParams != null && initSessionCallbackContext != null) {
306+
PluginResult result = new PluginResult(PluginResult.Status.OK, referringParams);
307+
if(initSessionCallbackContextKeepCallback){
308+
result.setKeepCallback(true);
309+
}
310+
initSessionCallbackContext.sendPluginResult(result);
311+
} else {
312+
JSONObject message = new JSONObject();
313+
try {
314+
message.put("error", error.getMessage());
315+
} catch (JSONException e) {
316+
e.printStackTrace();
317+
}
318+
if (initSessionCallbackContext != null) {
319+
initSessionCallbackContext.error(message);
320+
}
321+
}
322+
}
323+
};
324+
295325
/**
296326
* <p>This method should be called if you know that a different person is about to use the app. For example,
297327
* if you allow users to log out and let their friend use the app, you should call this to notify Branch
@@ -618,7 +648,7 @@ private void setCookieBasedMatching(String linkDomain, CallbackContext callbackC
618648
*/
619649
private void setDebug(boolean isEnable, CallbackContext callbackContext) {
620650
this.activity = this.cordova.getActivity();
621-
Branch.enableDebugMode();
651+
Branch.enableLogging();
622652
callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, isEnable));
623653
}
624654

@@ -653,7 +683,7 @@ private void setIdentity(String newIdentity, CallbackContext callbackContext) {
653683
/**
654684
* <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>
655685
*
656-
* @param token A {@link String} value containing the unique identifier of the Mixpanel user.
686+
* @param key A {@link String} value containing the unique identifier of the Mixpanel user.
657687
* @param callbackContext A callback to execute at the end of this method
658688
*/
659689
private void setRequestMetadata(String key, String val, CallbackContext callbackContext) {
@@ -1423,7 +1453,8 @@ public void run() {
14231453
} else if (this.action.equals("disableTracking")) {
14241454
disableTracking(this.args.getBoolean(0), this.callbackContext);
14251455
} else if (this.action.equals("initSession")) {
1426-
initSession(this.callbackContext);
1456+
boolean keepCallBack = this.args.length() != 0 && this.args.getBoolean(0);
1457+
initSession(keepCallBack, this.callbackContext);
14271458
} else if (this.action.equals("setRequestMetadata")) {
14281459
setRequestMetadata(this.args.getString(0), this.args.getString(1), this.callbackContext);
14291460
} else {

src/index.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,14 @@ Branch.prototype.initSession = function initSession() {
9898
return execute("initSession");
9999
};
100100

101+
Branch.prototype.initSessionWithCallback = function initSession(onSuccess, onFail) {
102+
this.sessionInitialized = true;
103+
if (!onSuccess || typeof onSuccess !== "function") {
104+
return executeReject("Please set onSuccess callback");
105+
}
106+
return executeCallback("initSession", onSuccess, [true]);
107+
};
108+
101109
Branch.prototype.setRequestMetadata = function setRequestMetadata(key, val) {
102110
if (!key || typeof key !== "string") {
103111
return executeReject("Please set key");

src/ios/BranchSDK.m

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ - (void)initSession:(CDVInvokedUrlCommand*)command
9898

9999
NSString *resultString = nil;
100100
CDVPluginResult *pluginResult = nil;
101+
bool enableCallBack = [[command.arguments objectAtIndex:0] boolValue];
101102

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

132133
if (command != nil) {
134+
if(enableCallBack){
135+
[pluginResult setKeepCallbackAsBool:YES];
136+
}
133137
[self.commandDelegate sendPluginResult: pluginResult callbackId: command.callbackId];
134138
}
135139
}];

0 commit comments

Comments
 (0)