Skip to content

Commit

Permalink
Merge pull request #799 from BranchMetrics/gdeluna-branch/SDK-1593
Browse files Browse the repository at this point in the history
[SDK-1593] Implementation for JS deferred init
  • Loading branch information
gdeluna-branch authored Mar 30, 2023
2 parents ce2023c + efcba74 commit b8b4414
Show file tree
Hide file tree
Showing 22 changed files with 179 additions and 100 deletions.
17 changes: 17 additions & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
2023-03-29
- 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
by this plugin in `subscribe()`.
This can be enabled by creating a `branch.json` file with the contents:
```js
{
"deferInitForPluginRuntime": true
}
```
Android: Place this file in your src/main/assets folder
iOS: Add this file through Xcode, File -> Add Files to "YourProject.xcodeproj"
and add to Copy Bundle Resources for each target that inits the Branch SDK.
- Update Android SDK to 5.3.0
- Update iOS SDK 2.1.0
It may be necessary to clear out pod cache and reinstall
- Fixes the typing of `isTrackingDisabled` to return `Promise<boolean>`

2023-01-23 Version 5.7.0
- Update Android SDK to 5.2.7
- Update iOS SDK to 1.45.2
Expand Down
2 changes: 1 addition & 1 deletion android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,5 @@ def safeExtGet(prop, fallback) {
dependencies {
implementation 'androidx.localbroadcastmanager:localbroadcastmanager:1.0.0'
implementation 'com.facebook.react:react-native:+' // From node_modules
api 'io.branch.sdk.android:library:5.2.7'
api 'io.branch.sdk.android:library:5.3.0'
}
33 changes: 29 additions & 4 deletions android/src/main/java/io/branch/rnbranch/RNBranchModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ public static void getAutoInstance(Context context) {
public static void reInitSession(Activity reactActivity) {
Branch branch = Branch.getInstance();
Intent intent = reactActivity.getIntent();
Log.d(REACT_CLASS,"reInitSession intent " + intent);
if (intent != null) {
intent.putExtra("branch_force_new_session", true);
notifyJSOfInitSessionStart(reactActivity, intent.getData());
Expand All @@ -119,11 +120,14 @@ public static void reInitSession(Activity reactActivity) {
}

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

public static void initSession(final Uri uri, Activity reactActivity) {
Log.d(REACT_CLASS,"initSession uri " + uri + " reactActivity " + reactActivity);

Branch branch = setupBranch(reactActivity.getApplicationContext());

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

@Override
public void onInitFinished(JSONObject referringParams, BranchError error) {
Log.d(REACT_CLASS,"onInitFinished referringParams " + referringParams);

// react native currently expects this to never be null
if (referringParams == null) {
referringParams = new JSONObject();
Expand All @@ -148,14 +154,15 @@ public void onInitFinished(JSONObject referringParams, BranchError error) {
result.put(NATIVE_INIT_SESSION_FINISHED_EVENT_URI, isNewIntent && uri != null ? uri.toString() : JSONObject.NULL);
}
catch (JSONException e) {

Log.e(REACT_CLASS, e.getMessage());
}
initSessionResult = result;

BranchUniversalObject branchUniversalObject = BranchUniversalObject.getReferredBranchUniversalObject();
LinkProperties linkProperties = LinkProperties.getReferredLinkProperties();

if (initListener != null) {
Log.d(REACT_CLASS,"onInitFinished " + branchUniversalObject + " " + linkProperties + " error " +error);
initListener.onInitFinished(branchUniversalObject, linkProperties, error);
}
generateLocalBroadcast(referringParams, uri, branchUniversalObject, linkProperties, error);
Expand All @@ -171,6 +178,7 @@ private void generateLocalBroadcast(JSONObject referringParams,
BranchUniversalObject branchUniversalObject,
LinkProperties linkProperties,
BranchError error) {

Intent broadcastIntent = new Intent(NATIVE_INIT_SESSION_FINISHED_EVENT);

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

notifyJSOfInitSessionStart(reactActivity, uri);
Branch.sessionBuilder(reactActivity).withCallback(referralInitListener).withData(uri).init();

Branch.InitSessionBuilder initSessionBuilder = Branch.sessionBuilder(reactActivity).withCallback(referralInitListener).withData(uri);
Log.d(REACT_CLASS, "sessionBuilder " + initSessionBuilder);
initSessionBuilder.init();
}

/**
Expand All @@ -215,6 +226,7 @@ private void generateLocalBroadcast(JSONObject referringParams,
* @param intent the new Intent received via Activity.onNewIntent
*/
public static void onNewIntent(@Nonnull Intent intent) {
Log.d(REACT_CLASS,"onNewIntent " + intent);
mActivity.setIntent(intent);
mNewIntent = true;
reInitSession(mActivity);
Expand All @@ -227,6 +239,8 @@ public static void onNewIntent(@Nonnull Intent intent) {
* @param uri the URI to include in the notification or null
*/
private static void notifyJSOfInitSessionStart(Context context, Uri uri) {
Log.d(REACT_CLASS,"notifyJSOfInitSessionStart " + uri);

/*
* This check just ensures that we only generate one RNBranch.initSessionStart
* event per call to onNewIntent().
Expand All @@ -239,15 +253,16 @@ private static void notifyJSOfInitSessionStart(Context context, Uri uri) {
broadcastIntent.putExtra(NATIVE_INIT_SESSION_STARTED_EVENT_URI, uri);
}

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

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

public static void enableLogging() {
Expand Down Expand Up @@ -329,6 +344,7 @@ private void listenForInitSessionEventsToReactNative(ReactApplicationContext rea
public void onReceive(Context context, Intent intent) {
final boolean hasError = (initSessionResult.has("error") && !initSessionResult.isNull("error"));
final String eventName = hasError ? RN_INIT_SESSION_ERROR_EVENT : RN_INIT_SESSION_SUCCESS_EVENT;

mBranchModule.sendRNEvent(eventName, convertJsonToMap(initSessionResult));
}

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

@Override
public void onCatalystInstanceDestroy() {
Log.d(REACT_CLASS,"onCatalystInstanceDestroy ");

LocalBroadcastManager.getInstance(getReactApplicationContext()).unregisterReceiver(mInitSessionFinishedEventReceiver);
LocalBroadcastManager.getInstance(getReactApplicationContext()).unregisterReceiver(mInitSessionStartedEventReceiver);
}
Expand All @@ -376,6 +394,12 @@ public String getName() {
return NAME;
}

@ReactMethod
public void notifyNativeToInit(){
Log.d(REACT_CLASS, "notifyNativeToInit");
Branch.notifyNativeToInit();
}

@ReactMethod
public void disableTracking(boolean disable) {
Branch branch = Branch.getInstance();
Expand Down Expand Up @@ -681,6 +705,7 @@ public void onLinkCreate(String url, BranchError error) {

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

private static Branch setupBranch(Context context) {
Branch branch = Branch.getInstance(context);
Branch branch = Branch.getAutoInstance(context);

if (!mInitialized) {
Log.i(REACT_CLASS, "Initializing Branch SDK v. " + BuildConfig.VERSION_NAME);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"enableLogging": true,
"deferInitForPluginRuntime": true
}
2 changes: 1 addition & 1 deletion branchreactnativetestbed/components/BranchWrapper.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {Alert, Modal} from 'react-native';
import {Alert} from 'react-native';
import branch, {BranchEvent} from 'react-native-branch';

export default class BranchWrapper {
Expand Down
12 changes: 6 additions & 6 deletions branchreactnativetestbed/ios/Podfile.lock
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
PODS:
- boost (1.76.0)
- Branch (1.45.2)
- BranchSDK (2.1.0)
- CocoaAsyncSocket (7.6.5)
- DoubleConversion (1.1.6)
- FBLazyVector (0.71.4)
Expand Down Expand Up @@ -330,8 +330,8 @@ PODS:
- React-jsinspector (0.71.4)
- React-logger (0.71.4):
- glog
- react-native-branch (5.7.0):
- Branch (= 1.45.2)
- react-native-branch (5.8.0-alpha.2):
- BranchSDK (= 2.1.0)
- React-Core
- React-perflogger (0.71.4)
- React-RCTActionSheet (0.71.4):
Expand Down Expand Up @@ -486,7 +486,7 @@ DEPENDENCIES:

SPEC REPOS:
trunk:
- Branch
- BranchSDK
- CocoaAsyncSocket
- Flipper
- Flipper-Boost-iOSX
Expand Down Expand Up @@ -577,7 +577,7 @@ EXTERNAL SOURCES:

SPEC CHECKSUMS:
boost: 57d2868c099736d80fcd648bf211b4431e51a558
Branch: 1fc81857f63403d4682305088b11270719dca8db
BranchSDK: ce28650272c658fcdb66675769e670ef83845d17
CocoaAsyncSocket: 065fd1e645c7abab64f7a6a2007a48038fdc6a99
DoubleConversion: 5189b271737e1565bdce30deb4a08d647e3f5f54
FBLazyVector: 446e84642979fff0ba57f3c804c2228a473aeac2
Expand Down Expand Up @@ -610,7 +610,7 @@ SPEC CHECKSUMS:
React-jsiexecutor: d6b7fa9260aa3cb40afee0507e3bc1d17ecaa6f2
React-jsinspector: 1f51e775819199d3fe9410e69ee8d4c4161c7b06
React-logger: 0d58569ec51d30d1792c5e86a8e3b78d24b582c6
react-native-branch: 882341157fe7f47b0d8ac05b722040075eeefb7e
react-native-branch: a1d770fbde5cc8706f9507771ad98917e1380d17
React-perflogger: 0bb0522a12e058f6eb69d888bc16f40c16c4b907
React-RCTActionSheet: bfd675a10f06a18728ea15d82082d48f228a213a
React-RCTAnimation: 2fa220b2052ec75b733112aca39143d34546a941
Expand Down
4 changes: 4 additions & 0 deletions branchreactnativetestbed/ios/branch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"enableLogging": true,
"deferInitForPluginRuntime": true
}
Loading

0 comments on commit b8b4414

Please sign in to comment.