Skip to content

Commit b8b4414

Browse files
Merge pull request #799 from BranchMetrics/gdeluna-branch/SDK-1593
[SDK-1593] Implementation for JS deferred init
2 parents ce2023c + efcba74 commit b8b4414

File tree

22 files changed

+179
-100
lines changed

22 files changed

+179
-100
lines changed

ChangeLog.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,20 @@
1+
2023-03-29
2+
- To address a race condition where apps don't receive Branch params on cold starts, an opt in fix will defer loading the native iOS/Android layer until signaled
3+
by this plugin in `subscribe()`.
4+
This can be enabled by creating a `branch.json` file with the contents:
5+
```js
6+
{
7+
"deferInitForPluginRuntime": true
8+
}
9+
```
10+
Android: Place this file in your src/main/assets folder
11+
iOS: Add this file through Xcode, File -> Add Files to "YourProject.xcodeproj"
12+
and add to Copy Bundle Resources for each target that inits the Branch SDK.
13+
- Update Android SDK to 5.3.0
14+
- Update iOS SDK 2.1.0
15+
It may be necessary to clear out pod cache and reinstall
16+
- Fixes the typing of `isTrackingDisabled` to return `Promise<boolean>`
17+
118
2023-01-23 Version 5.7.0
219
- Update Android SDK to 5.2.7
320
- Update iOS SDK to 1.45.2

android/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,5 +47,5 @@ def safeExtGet(prop, fallback) {
4747
dependencies {
4848
implementation 'androidx.localbroadcastmanager:localbroadcastmanager:1.0.0'
4949
implementation 'com.facebook.react:react-native:+' // From node_modules
50-
api 'io.branch.sdk.android:library:5.2.7'
50+
api 'io.branch.sdk.android:library:5.3.0'
5151
}

android/src/main/java/io/branch/rnbranch/RNBranchModule.java

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ public static void getAutoInstance(Context context) {
109109
public static void reInitSession(Activity reactActivity) {
110110
Branch branch = Branch.getInstance();
111111
Intent intent = reactActivity.getIntent();
112+
Log.d(REACT_CLASS,"reInitSession intent " + intent);
112113
if (intent != null) {
113114
intent.putExtra("branch_force_new_session", true);
114115
notifyJSOfInitSessionStart(reactActivity, intent.getData());
@@ -119,11 +120,14 @@ public static void reInitSession(Activity reactActivity) {
119120
}
120121

121122
public static void initSession(final Uri uri, Activity reactActivity, Branch.BranchUniversalReferralInitListener anInitListener) {
123+
Log.d(REACT_CLASS,"initSession uri " + uri + " reactActivity " + reactActivity + " anInitListener" + anInitListener);
122124
initListener = anInitListener;
123125
initSession(uri, reactActivity);
124126
}
125127

126128
public static void initSession(final Uri uri, Activity reactActivity) {
129+
Log.d(REACT_CLASS,"initSession uri " + uri + " reactActivity " + reactActivity);
130+
127131
Branch branch = setupBranch(reactActivity.getApplicationContext());
128132

129133
mActivity = reactActivity;
@@ -134,6 +138,8 @@ public static void initSession(final Uri uri, Activity reactActivity) {
134138

135139
@Override
136140
public void onInitFinished(JSONObject referringParams, BranchError error) {
141+
Log.d(REACT_CLASS,"onInitFinished referringParams " + referringParams);
142+
137143
// react native currently expects this to never be null
138144
if (referringParams == null) {
139145
referringParams = new JSONObject();
@@ -148,14 +154,15 @@ public void onInitFinished(JSONObject referringParams, BranchError error) {
148154
result.put(NATIVE_INIT_SESSION_FINISHED_EVENT_URI, isNewIntent && uri != null ? uri.toString() : JSONObject.NULL);
149155
}
150156
catch (JSONException e) {
151-
157+
Log.e(REACT_CLASS, e.getMessage());
152158
}
153159
initSessionResult = result;
154160

155161
BranchUniversalObject branchUniversalObject = BranchUniversalObject.getReferredBranchUniversalObject();
156162
LinkProperties linkProperties = LinkProperties.getReferredLinkProperties();
157163

158164
if (initListener != null) {
165+
Log.d(REACT_CLASS,"onInitFinished " + branchUniversalObject + " " + linkProperties + " error " +error);
159166
initListener.onInitFinished(branchUniversalObject, linkProperties, error);
160167
}
161168
generateLocalBroadcast(referringParams, uri, branchUniversalObject, linkProperties, error);
@@ -171,6 +178,7 @@ private void generateLocalBroadcast(JSONObject referringParams,
171178
BranchUniversalObject branchUniversalObject,
172179
LinkProperties linkProperties,
173180
BranchError error) {
181+
174182
Intent broadcastIntent = new Intent(NATIVE_INIT_SESSION_FINISHED_EVENT);
175183

176184
if (referringParams != null) {
@@ -202,7 +210,10 @@ private void generateLocalBroadcast(JSONObject referringParams,
202210
}.init(reactActivity);
203211

204212
notifyJSOfInitSessionStart(reactActivity, uri);
205-
Branch.sessionBuilder(reactActivity).withCallback(referralInitListener).withData(uri).init();
213+
214+
Branch.InitSessionBuilder initSessionBuilder = Branch.sessionBuilder(reactActivity).withCallback(referralInitListener).withData(uri);
215+
Log.d(REACT_CLASS, "sessionBuilder " + initSessionBuilder);
216+
initSessionBuilder.init();
206217
}
207218

208219
/**
@@ -215,6 +226,7 @@ private void generateLocalBroadcast(JSONObject referringParams,
215226
* @param intent the new Intent received via Activity.onNewIntent
216227
*/
217228
public static void onNewIntent(@Nonnull Intent intent) {
229+
Log.d(REACT_CLASS,"onNewIntent " + intent);
218230
mActivity.setIntent(intent);
219231
mNewIntent = true;
220232
reInitSession(mActivity);
@@ -227,6 +239,8 @@ public static void onNewIntent(@Nonnull Intent intent) {
227239
* @param uri the URI to include in the notification or null
228240
*/
229241
private static void notifyJSOfInitSessionStart(Context context, Uri uri) {
242+
Log.d(REACT_CLASS,"notifyJSOfInitSessionStart " + uri);
243+
230244
/*
231245
* This check just ensures that we only generate one RNBranch.initSessionStart
232246
* event per call to onNewIntent().
@@ -239,15 +253,16 @@ private static void notifyJSOfInitSessionStart(Context context, Uri uri) {
239253
broadcastIntent.putExtra(NATIVE_INIT_SESSION_STARTED_EVENT_URI, uri);
240254
}
241255

256+
Log.d(REACT_CLASS, "Broadcasting NATIVE_INIT_SESSION_STARTED_EVENT");
242257
LocalBroadcastManager.getInstance(context).sendBroadcast(broadcastIntent);
243-
Log.d(REACT_CLASS, "Sent session start broadcast for " + uri);
244258
}
245259

246260
/**
247261
* @deprecated setDebug is deprecated and all functionality has been disabled. If you wish to enable
248262
* logging, please invoke enableLogging. If you wish to simulate installs, please Test Devices
249263
* (https://help.branch.io/using-branch/docs/adding-test-devices)
250264
*/
265+
@Deprecated
251266
public static void setDebug() { }
252267

253268
public static void enableLogging() {
@@ -329,6 +344,7 @@ private void listenForInitSessionEventsToReactNative(ReactApplicationContext rea
329344
public void onReceive(Context context, Intent intent) {
330345
final boolean hasError = (initSessionResult.has("error") && !initSessionResult.isNull("error"));
331346
final String eventName = hasError ? RN_INIT_SESSION_ERROR_EVENT : RN_INIT_SESSION_SUCCESS_EVENT;
347+
332348
mBranchModule.sendRNEvent(eventName, convertJsonToMap(initSessionResult));
333349
}
334350

@@ -367,6 +383,8 @@ private BroadcastReceiver init(RNBranchModule branchModule) {
367383

368384
@Override
369385
public void onCatalystInstanceDestroy() {
386+
Log.d(REACT_CLASS,"onCatalystInstanceDestroy ");
387+
370388
LocalBroadcastManager.getInstance(getReactApplicationContext()).unregisterReceiver(mInitSessionFinishedEventReceiver);
371389
LocalBroadcastManager.getInstance(getReactApplicationContext()).unregisterReceiver(mInitSessionStartedEventReceiver);
372390
}
@@ -376,6 +394,12 @@ public String getName() {
376394
return NAME;
377395
}
378396

397+
@ReactMethod
398+
public void notifyNativeToInit(){
399+
Log.d(REACT_CLASS, "notifyNativeToInit");
400+
Branch.notifyNativeToInit();
401+
}
402+
379403
@ReactMethod
380404
public void disableTracking(boolean disable) {
381405
Branch branch = Branch.getInstance();
@@ -681,6 +705,7 @@ public void onLinkCreate(String url, BranchError error) {
681705

682706
@ReactMethod
683707
public void openURL(String url, ReadableMap options) {
708+
Log.d(REACT_CLASS, "openURL url: " + url);
684709
if (mActivity == null) {
685710
// initSession is called before JS loads. This probably indicates failure to call initSession
686711
// in an activity.
@@ -831,7 +856,7 @@ public static LinkProperties createLinkProperties(ReadableMap linkPropertiesMap,
831856
}
832857

833858
private static Branch setupBranch(Context context) {
834-
Branch branch = Branch.getInstance(context);
859+
Branch branch = Branch.getAutoInstance(context);
835860

836861
if (!mInitialized) {
837862
Log.i(REACT_CLASS, "Initializing Branch SDK v. " + BuildConfig.VERSION_NAME);
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"enableLogging": true,
3+
"deferInitForPluginRuntime": true
4+
}

branchreactnativetestbed/components/BranchWrapper.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {Alert, Modal} from 'react-native';
1+
import {Alert} from 'react-native';
22
import branch, {BranchEvent} from 'react-native-branch';
33

44
export default class BranchWrapper {

branchreactnativetestbed/ios/Podfile.lock

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
PODS:
22
- boost (1.76.0)
3-
- Branch (1.45.2)
3+
- BranchSDK (2.1.0)
44
- CocoaAsyncSocket (7.6.5)
55
- DoubleConversion (1.1.6)
66
- FBLazyVector (0.71.4)
@@ -330,8 +330,8 @@ PODS:
330330
- React-jsinspector (0.71.4)
331331
- React-logger (0.71.4):
332332
- glog
333-
- react-native-branch (5.7.0):
334-
- Branch (= 1.45.2)
333+
- react-native-branch (5.8.0-alpha.2):
334+
- BranchSDK (= 2.1.0)
335335
- React-Core
336336
- React-perflogger (0.71.4)
337337
- React-RCTActionSheet (0.71.4):
@@ -486,7 +486,7 @@ DEPENDENCIES:
486486

487487
SPEC REPOS:
488488
trunk:
489-
- Branch
489+
- BranchSDK
490490
- CocoaAsyncSocket
491491
- Flipper
492492
- Flipper-Boost-iOSX
@@ -577,7 +577,7 @@ EXTERNAL SOURCES:
577577

578578
SPEC CHECKSUMS:
579579
boost: 57d2868c099736d80fcd648bf211b4431e51a558
580-
Branch: 1fc81857f63403d4682305088b11270719dca8db
580+
BranchSDK: ce28650272c658fcdb66675769e670ef83845d17
581581
CocoaAsyncSocket: 065fd1e645c7abab64f7a6a2007a48038fdc6a99
582582
DoubleConversion: 5189b271737e1565bdce30deb4a08d647e3f5f54
583583
FBLazyVector: 446e84642979fff0ba57f3c804c2228a473aeac2
@@ -610,7 +610,7 @@ SPEC CHECKSUMS:
610610
React-jsiexecutor: d6b7fa9260aa3cb40afee0507e3bc1d17ecaa6f2
611611
React-jsinspector: 1f51e775819199d3fe9410e69ee8d4c4161c7b06
612612
React-logger: 0d58569ec51d30d1792c5e86a8e3b78d24b582c6
613-
react-native-branch: 882341157fe7f47b0d8ac05b722040075eeefb7e
613+
react-native-branch: a1d770fbde5cc8706f9507771ad98917e1380d17
614614
React-perflogger: 0bb0522a12e058f6eb69d888bc16f40c16c4b907
615615
React-RCTActionSheet: bfd675a10f06a18728ea15d82082d48f228a213a
616616
React-RCTAnimation: 2fa220b2052ec75b733112aca39143d34546a941
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"enableLogging": true,
3+
"deferInitForPluginRuntime": true
4+
}

0 commit comments

Comments
 (0)