Skip to content

Add setBranchKey support and refactor Branch key handling #450

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

Open
wants to merge 1 commit into
base: dev
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
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@
import io.flutter.plugin.common.MethodChannel.Result;
import io.flutter.plugin.common.PluginRegistry.NewIntentListener;

public class FlutterBranchSdkPlugin implements FlutterPlugin, MethodCallHandler, StreamHandler, NewIntentListener, ActivityAware,
public class FlutterBranchSdkPlugin
implements FlutterPlugin, MethodCallHandler, StreamHandler, NewIntentListener, ActivityAware,
Application.ActivityLifecycleCallbacks {
private static final String DEBUG_NAME = "FlutterBranchSDK";
private static final String MESSAGE_CHANNEL = "flutter_branch_sdk/message";
Expand All @@ -68,46 +69,45 @@ public class FlutterBranchSdkPlugin implements FlutterPlugin, MethodCallHandler,
* Branch SDK Call Methods
* --------------------------------------------------------------------------------------------
**/
private final Branch.BranchReferralInitListener branchReferralInitListener = new
Branch.BranchReferralInitListener() {
@Override
public void onInitFinished(JSONObject params, BranchError error) {
LogUtils.debug(DEBUG_NAME, "triggered onInitFinished");
if (error == null) {
LogUtils.debug(DEBUG_NAME, "BranchReferralInitListener - params: " + params.toString());
try {
sessionParams = branchSdkHelper.paramsToMap(params);
} catch (JSONException e) {
LogUtils.debug(DEBUG_NAME, "BranchReferralInitListener - error to Map: " + e.getLocalizedMessage());
return;
}
if (eventSink != null) {
eventSink.success(sessionParams);
sessionParams = null;
}
} else if (error.getErrorCode() == BranchError.ERR_BRANCH_ALREADY_INITIALIZED) {
LogUtils.debug(DEBUG_NAME, "BranchReferralInitListener : " + error.getMessage());
try {
sessionParams = branchSdkHelper.paramsToMap(Branch.getInstance().getLatestReferringParams());
} catch (JSONException e) {
LogUtils.debug(DEBUG_NAME, "BranchReferralInitListener - error to Map: " + e.getLocalizedMessage());
return;
}
if (eventSink != null) {
eventSink.success(sessionParams);
sessionParams = null;
}
} else {
LogUtils.debug(DEBUG_NAME, "BranchReferralInitListener - error: " + error);
if (eventSink != null) {
eventSink.error(String.valueOf(error.getErrorCode()), error.getMessage(), null);
initialError = null;
} else {
initialError = error;
}
}
private final Branch.BranchReferralInitListener branchReferralInitListener = new Branch.BranchReferralInitListener() {
@Override
public void onInitFinished(JSONObject params, BranchError error) {
LogUtils.debug(DEBUG_NAME, "triggered onInitFinished");
if (error == null) {
LogUtils.debug(DEBUG_NAME, "BranchReferralInitListener - params: " + params.toString());
try {
sessionParams = branchSdkHelper.paramsToMap(params);
} catch (JSONException e) {
LogUtils.debug(DEBUG_NAME, "BranchReferralInitListener - error to Map: " + e.getLocalizedMessage());
return;
}
if (eventSink != null) {
eventSink.success(sessionParams);
sessionParams = null;
}
};
} else if (error.getErrorCode() == BranchError.ERR_BRANCH_ALREADY_INITIALIZED) {
LogUtils.debug(DEBUG_NAME, "BranchReferralInitListener : " + error.getMessage());
try {
sessionParams = branchSdkHelper.paramsToMap(Branch.getInstance().getLatestReferringParams());
} catch (JSONException e) {
LogUtils.debug(DEBUG_NAME, "BranchReferralInitListener - error to Map: " + e.getLocalizedMessage());
return;
}
if (eventSink != null) {
eventSink.success(sessionParams);
sessionParams = null;
}
} else {
LogUtils.debug(DEBUG_NAME, "BranchReferralInitListener - error: " + error);
if (eventSink != null) {
eventSink.error(String.valueOf(error.getErrorCode()), error.getMessage(), null);
initialError = null;
} else {
initialError = error;
}
}
}
};
private boolean isInitialized = false;

/**
Expand Down Expand Up @@ -147,7 +147,8 @@ private void setActivity(Activity activity) {
activity.getApplication().registerActivityLifecycleCallbacks(this);

if (this.activity != null && FlutterFragmentActivity.class.isAssignableFrom(activity.getClass())) {
Branch.sessionBuilder(activity).withCallback(branchReferralInitListener).withData(activity.getIntent().getData()).init();
Branch.sessionBuilder(activity).withCallback(branchReferralInitListener)
.withData(activity.getIntent().getData()).init();
}
}

Expand Down Expand Up @@ -235,7 +236,8 @@ public void onActivityStarted(@NonNull Activity activity) {
return;
}
LogUtils.debug(DEBUG_NAME, "triggered SessionBuilder init");
Branch.sessionBuilder(activity).withCallback(branchReferralInitListener).withData(activity.getIntent().getData()).init();
Branch.sessionBuilder(activity).withCallback(branchReferralInitListener)
.withData(activity.getIntent().getData()).init();
}

@Override
Expand Down Expand Up @@ -295,6 +297,13 @@ public boolean onNewIntent(@NonNull Intent intent) {
public void onMethodCall(@NonNull MethodCall call, @NonNull Result rawResult) {
Result result = new MethodResultWrapper(rawResult);
switch (call.method) {
case "setBranchKey":
String branchKey = call.argument("branchKey");
if (branchKey != null) {
Branch.getAutoInstance(context).setBranchKey(branchKey);
}
result.success(null);
break;
case "init":
setupBranch(call, result);
break;
Expand Down Expand Up @@ -462,7 +471,8 @@ private void setupBranch(MethodCall call, final Result result) {

final String branchAttributionLevelString = call.argument("branchAttributionLevel");
if (branchAttributionLevelString != null && !branchAttributionLevelString.isEmpty()) {
Branch.getInstance().setConsumerProtectionAttributionLevel(Defines.BranchAttributionLevel.valueOf(branchAttributionLevelString));
Branch.getInstance().setConsumerProtectionAttributionLevel(
Defines.BranchAttributionLevel.valueOf(branchAttributionLevelString));
}

LogUtils.debug(DEBUG_NAME, "notifyNativeToInit()");
Expand All @@ -481,7 +491,8 @@ private void getShortUrl(MethodCall call, final Result result) {
}
HashMap<String, Object> argsMap = (HashMap<String, Object>) call.arguments;
BranchUniversalObject buo = branchSdkHelper.convertToBUO((HashMap<String, Object>) argsMap.get("buo"));
LinkProperties linkProperties = branchSdkHelper.convertToLinkProperties((HashMap<String, Object>) argsMap.get("lp"));
LinkProperties linkProperties = branchSdkHelper
.convertToLinkProperties((HashMap<String, Object>) argsMap.get("lp"));
final Map<String, Object> response = new HashMap<>();
buo.generateShortUrl(activity, linkProperties, new Branch.BranchLinkCreateListener() {
@Override
Expand All @@ -507,7 +518,8 @@ private void showShareSheet(MethodCall call, final Result result) {
}
HashMap<String, Object> argsMap = (HashMap<String, Object>) call.arguments;
BranchUniversalObject buo = branchSdkHelper.convertToBUO((HashMap<String, Object>) argsMap.get("buo"));
LinkProperties linkProperties = branchSdkHelper.convertToLinkProperties((HashMap<String, Object>) argsMap.get("lp"));
LinkProperties linkProperties = branchSdkHelper
.convertToLinkProperties((HashMap<String, Object>) argsMap.get("lp"));
String messageText = (String) argsMap.get("messageText");
String messageTitle = (String) argsMap.get("messageTitle");
String sharingTitle = (String) argsMap.get("sharingTitle");
Expand All @@ -516,25 +528,25 @@ private void showShareSheet(MethodCall call, final Result result) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) {

Branch.getInstance().share(activity, buo, linkProperties, new Branch.BranchNativeLinkShareListener() {
@Override
public void onLinkShareResponse(String sharedLink, BranchError error) {
if (error == null) {
LogUtils.debug(DEBUG_NAME, "Branch link share: " + sharedLink);
response.put("success", Boolean.TRUE);
response.put("url", sharedLink);
} else {
response.put("success", Boolean.FALSE);
response.put("errorCode", String.valueOf(error.getErrorCode()));
response.put("errorMessage", error.getMessage());
}
result.success(response);
}
@Override
public void onLinkShareResponse(String sharedLink, BranchError error) {
if (error == null) {
LogUtils.debug(DEBUG_NAME, "Branch link share: " + sharedLink);
response.put("success", Boolean.TRUE);
response.put("url", sharedLink);
} else {
response.put("success", Boolean.FALSE);
response.put("errorCode", String.valueOf(error.getErrorCode()));
response.put("errorMessage", error.getMessage());
}
result.success(response);
}

@Override
public void onChannelSelected(String channelName) {
LogUtils.debug(DEBUG_NAME, "Branch link share channel: " + channelName);
}
},
@Override
public void onChannelSelected(String channelName) {
LogUtils.debug(DEBUG_NAME, "Branch link share channel: " + channelName);
}
},
messageTitle,
messageText);
} else {
Expand Down Expand Up @@ -574,7 +586,8 @@ public void onChannelSelected(String channelName) {
}

@Override
public boolean onChannelSelected(String channelName, BranchUniversalObject buo, LinkProperties linkProperties) {
public boolean onChannelSelected(String channelName, BranchUniversalObject buo,
LinkProperties linkProperties) {
return false;
}
});
Expand Down Expand Up @@ -845,8 +858,10 @@ private void getQRCode(final MethodCall call, final Result result) {
}
HashMap<String, Object> argsMap = (HashMap<String, Object>) call.arguments;
final BranchUniversalObject buo = branchSdkHelper.convertToBUO((HashMap<String, Object>) argsMap.get("buo"));
final LinkProperties linkProperties = branchSdkHelper.convertToLinkProperties((HashMap<String, Object>) argsMap.get("lp"));
final BranchQRCode branchQRCode = branchSdkHelper.convertToQRCode((HashMap<String, Object>) argsMap.get("qrCodeSettings"));
final LinkProperties linkProperties = branchSdkHelper
.convertToLinkProperties((HashMap<String, Object>) argsMap.get("lp"));
final BranchQRCode branchQRCode = branchSdkHelper
.convertToQRCode((HashMap<String, Object>) argsMap.get("qrCodeSettings"));
final Map<String, Object> response = new HashMap<>();
try {
branchQRCode.getQRCodeAsData(context, buo, linkProperties, new BranchQRCode.BranchQRCodeDataHandler() {
Expand Down Expand Up @@ -995,8 +1010,7 @@ private void setConsumerProtectionAttributionLevel(MethodCall call) {
throw new IllegalArgumentException("Map argument expected");
}
final String branchAttributionLevelString = call.argument("branchAttributionLevel");
Branch.getInstance().setConsumerProtectionAttributionLevel(Defines.BranchAttributionLevel.valueOf(branchAttributionLevelString));
Branch.getInstance().setConsumerProtectionAttributionLevel(
Defines.BranchAttributionLevel.valueOf(branchAttributionLevelString));
}
}


8 changes: 4 additions & 4 deletions example/android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,13 @@
android:name="flutterEmbedding"
android:value="2" />
<!-- Branch init -->
<meta-data
<!-- <meta-data
android:name="io.branch.sdk.BranchKey"
android:value="key_live_bkJRqpb15wLqUAgsDVNUIobjyviWi6Wx" />
android:value="key_live_bkJRqpb15wLqUAgsDVNUIobjyviWi6Wx" /> -->
<!-- For your test app, if you have one; Again, use your actual test Branch key -->
<meta-data
<!-- <meta-data
android:name="io.branch.sdk.BranchKey.test"
android:value="key_test_ipQTteg11ENANDeCzSXgqdgfuycWoXYH" />
android:value="key_test_ipQTteg11ENANDeCzSXgqdgfuycWoXYH" /> -->
<meta-data
android:name="io.branch.sdk.TestMode" android:value="true" />
</application>
Expand Down
13 changes: 13 additions & 0 deletions example/ios/Podfile.lock
Original file line number Diff line number Diff line change
@@ -1,15 +1,28 @@
PODS:
- BranchSDK (3.12.2)
- Flutter (1.0.0)
- flutter_branch_sdk (8.6.0):
- BranchSDK (~> 3.12.0)
- Flutter

DEPENDENCIES:
- Flutter (from `Flutter`)
- flutter_branch_sdk (from `.symlinks/plugins/flutter_branch_sdk/ios`)

SPEC REPOS:
trunk:
- BranchSDK

EXTERNAL SOURCES:
Flutter:
:path: Flutter
flutter_branch_sdk:
:path: ".symlinks/plugins/flutter_branch_sdk/ios"

SPEC CHECKSUMS:
BranchSDK: db2c2ffa5b85ad3e202f73393ca794878afd3c52
Flutter: e0871f40cf51350855a761d2e70bf5af5b9b5de7
flutter_branch_sdk: e61c2074456e7df20555378ba0c7603a6094f6c2

PODFILE CHECKSUM: 4e8f8b2be68aeea4c0d5beb6ff1e79fface1d048

Expand Down
18 changes: 18 additions & 0 deletions example/ios/Runner.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@
97C146EC1CF9000F007C117D /* Resources */,
9705A1C41CF9048500538489 /* Embed Frameworks */,
3B06AD1E1E4923F5004D2608 /* Thin Binary */,
3ECB9F60410FE9CE00AB9B59 /* [CP] Embed Pods Frameworks */,
);
buildRules = (
);
Expand Down Expand Up @@ -229,6 +230,23 @@
shellPath = /bin/sh;
shellScript = "/bin/sh \"$FLUTTER_ROOT/packages/flutter_tools/bin/xcode_backend.sh\" embed_and_thin";
};
3ECB9F60410FE9CE00AB9B59 /* [CP] Embed Pods Frameworks */ = {
isa = PBXShellScriptBuildPhase;
buildActionMask = 2147483647;
files = (
);
inputFileListPaths = (
"${PODS_ROOT}/Target Support Files/Pods-Runner/Pods-Runner-frameworks-${CONFIGURATION}-input-files.xcfilelist",
);
name = "[CP] Embed Pods Frameworks";
outputFileListPaths = (
"${PODS_ROOT}/Target Support Files/Pods-Runner/Pods-Runner-frameworks-${CONFIGURATION}-output-files.xcfilelist",
);
runOnlyForDeploymentPostprocessing = 0;
shellPath = /bin/sh;
shellScript = "\"${PODS_ROOT}/Target Support Files/Pods-Runner/Pods-Runner-frameworks.sh\"\n";
showEnvVarsInLog = 0;
};
9740EEB61CF901F6004384FC /* Run Script */ = {
isa = PBXShellScriptBuildPhase;
alwaysOutOfDate = 1;
Expand Down

This file was deleted.

This file was deleted.

6 changes: 3 additions & 3 deletions example/ios/Runner/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,13 @@
</array>
<key>UIViewControllerBasedStatusBarAppearance</key>
<false/>
<key>branch_key</key>
<dict>
<!-- <key>branch_key</key> -->
<!-- <dict>
<key>live</key>
<string>key_live_bkJRqpb15wLqUAgsDVNUIobjyviWi6Wx</string>
<key>test</key>
<string>key_test_ipQTteg11ENANDeCzSXgqdgfuycWoXYH</string>
</dict>
</dict> -->
<key>branch_universal_link_domains</key>
<array>
<string>flutterbranchsdk.app.link</string>
Expand Down
1 change: 1 addition & 0 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ void main() async {
FlqutterBranchSdk.setRequestMetadata('key2', 'value2');
*/

await FlutterBranchSdk.e setBranchKey('r');
await FlutterBranchSdk.init(enableLogging: true, branchAttributionLevel: BranchAttributionLevel.FULL);
FlutterBranchSdk.setConsumerProtectionAttributionLevel(BranchAttributionLevel.FULL);

Expand Down
Loading