diff --git a/README.md b/README.md index 7c1774e1..87b04645 100644 --- a/README.md +++ b/README.md @@ -412,6 +412,54 @@ branchUniversalObj.showShareSheet({ }); ``` +##### Share Sheet Callbacks (Android ONLY) + +To implement the callback, you must add listeners to the following events: + +###### onShareSheetLaunched + +The event fires when the share sheet is presented. + +```js +branchUniversalObj.onShareSheetLaunched(function () { + console.log('Share sheet launched'); +}); +``` + +###### onShareSheetDismissed + +The event fires when the share sheet is dismissed. + +```js +branchUniversalObj.onShareSheetDismissed(function () { + console.log('Share sheet dimissed'); +}); +``` + +###### onLinkShareResponse + +The event returns a dictionary of the response data. + +```js +branchUniversalObj.onLinkShareResponse(function (res) { + console.log('Share link response: ' + JSON.stringify(res)); +}); +``` + +###### onChannelSelected + +The event fires when a channel is selected. + +```js +branchUniversalObj.onChannelSelected(function (res) { + console.log('Channel selected: ' + JSON.stringify(res)); +}); +``` + +**Note:** Callbacks in iOS are ignored. There is no need to implement them as the events are handled by `UIActivityViewController`. + +**Note:** Avoid passing `alias` in iOS. Adding an `alias` key in the `options` parameter will return a Non-Universal link which will not work in iOS 9.2. + ### listOnSpotlight() **Note: iOS only.** Used for Spotlight listing @@ -452,11 +500,14 @@ Redeems a reward with the given amount/value. **Parameters** -**value**: `int` - Amount to be redeemed. +| KEY | TYPE | MEANING +| -------- | -------- |------------------------ +| value | `int` | Amount to be redeemed. +| bucket | `int` | Bucket where the amount will be redeemed. _optional_ ##### Usage ```js -Branch.redeemRewards(100).then(function (res) { +Branch.redeemRewards(100, "default").then(function (res) { // Success Callback console.log(res); }).catch(function (err) { diff --git a/src/android/io/branch/BranchSDK.java b/src/android/io/branch/BranchSDK.java index bbe5c466..a26bf31d 100644 --- a/src/android/io/branch/BranchSDK.java +++ b/src/android/io/branch/BranchSDK.java @@ -31,6 +31,10 @@ static class BranchLinkProperties extends io.branch.referral.util.LinkProperties // Private Method Properties private BranchUniversalObject branchObj; private CallbackContext callbackContext; + private CallbackContext onShareLinkDialogLaunched; + private CallbackContext onShareLinkDialogDismissed; + private CallbackContext onLinkShareResponse; + private CallbackContext onChannelSelected; private Activity activity; private Branch instance; @@ -43,6 +47,10 @@ public BranchSDK() this.instance = null; this.branchObj = null; this.callbackContext = null; + this.onShareLinkDialogLaunched = null; + this.onShareLinkDialogDismissed = null; + this.onLinkShareResponse = null; + this.onChannelSelected = null; } /** @@ -134,7 +142,11 @@ public boolean execute(String action, JSONArray args, CallbackContext callbackCo this.loadRewards(); return true; } else if (action.equals("redeemRewards")) { - this.redeemRewards(args.getInt(0)); + if (args.length() == 1) { + this.redeemRewards(args.getInt(0)); + } else if (args.length() == 2) { + this.redeemRewards(args.getInt(0), args.getString(1)); + } return true; } else if (action.equals("getCreditHistory")) { this.getCreditHistory(); @@ -151,6 +163,18 @@ public boolean execute(String action, JSONArray args, CallbackContext callbackCo } else if (action.equals("showShareSheet")) { this.showShareSheet(args.getJSONObject(0), args.getJSONObject(1)); return true; + } else if (action.equals("onShareLinkDialogLaunched")) { + this.onShareLinkDialogLaunched = callbackContext; + return true; + } else if (action.equals("onShareLinkDialogDismissed")) { + this.onShareLinkDialogDismissed = callbackContext; + return true; + } else if (action.equals("onLinkShareResponse")) { + this.onLinkShareResponse = callbackContext; + return true; + } else if (action.equals("onChannelSelected")) { + this.onChannelSelected = callbackContext; + return true; } } } @@ -200,7 +224,7 @@ private void logout() * @param count A {@link Integer} specifying the number of credits to attempt to redeem from * the bucket. */ - private void redeemRewards(int value) + private void redeemRewards(final int value) { Log.d(LCAT, "start redeemRewards()"); @@ -209,6 +233,23 @@ private void redeemRewards(int value) } + /** + *

Redeems the specified number of credits from the "default" bucket, if there are sufficient + * credits within it. If the number to redeem exceeds the number available in the bucket, all of + * the available credits will be redeemed instead.

+ * + * @param count A {@link Integer} specifying the number of credits to attempt to redeem from + * the bucket. + */ + private void redeemRewards(int value, String bucket) + { + + Log.d(LCAT, "start redeemRewards()"); + + this.instance.redeemRewards(bucket, value, new LoadRewardsListener()); + + } + /** *

Retrieves rewards for the current session, with a callback to perform a predefined * action following successful report of state change. You'll then need to call getCredits @@ -730,11 +771,29 @@ protected class ShowShareSheetListener implements Branch.BranchLinkShareListener @Override public void onShareLinkDialogLaunched() { Log.d(LCAT, "inside onShareLinkDialogLaunched"); + + if (onShareLinkDialogLaunched != null) { + PluginResult result = new PluginResult(PluginResult.Status.OK); + + result.setKeepCallback(true); + + onShareLinkDialogLaunched.sendPluginResult(result); + } + } @Override public void onShareLinkDialogDismissed() { Log.d(LCAT, "inside onShareLinkDialogDismissed"); + + if (onShareLinkDialogDismissed != null) { + PluginResult result = new PluginResult(PluginResult.Status.OK); + + result.setKeepCallback(true); + + onShareLinkDialogDismissed.sendPluginResult(result); + } + } @Override @@ -771,6 +830,14 @@ public void onLinkShareResponse(String sharedLink, String sharedChannel, BranchE Log.d(LCAT, response.toString()); + if (onLinkShareResponse != null) { + PluginResult result = new PluginResult(PluginResult.Status.OK, response); + + result.setKeepCallback(true); + + onLinkShareResponse.sendPluginResult(result); + } + } @Override @@ -790,6 +857,14 @@ public void onChannelSelected(String channelName) { Log.d(LCAT, response.toString()); + if (onChannelSelected != null) { + PluginResult result = new PluginResult(PluginResult.Status.OK, response); + + result.setKeepCallback(true); + + onChannelSelected.sendPluginResult(result); + } + } } diff --git a/src/ios/BranchSDK.m b/src/ios/BranchSDK.m index dae7d19f..03bb60bb 100644 --- a/src/ios/BranchSDK.m +++ b/src/ios/BranchSDK.m @@ -280,34 +280,68 @@ - (void)loadRewards:(CDVInvokedUrlCommand*)command - (void)redeemRewards:(CDVInvokedUrlCommand*)command { NSLog(@"start redeemRewards"); + NSInteger amount = ((NSNumber *)[command.arguments objectAtIndex:0]).integerValue; Branch *branch = [self getInstance]; - [branch redeemRewards:amount callback:^(BOOL changed, NSError *error) { - NSLog(@"inside redeemRewards block"); - CDVPluginResult* pluginResult = nil; - if (!error) { - NSNumber *changedValue = [NSNumber numberWithBool:changed]; - NSError *err; - NSData *jsonData = [NSJSONSerialization dataWithJSONObject:@{@"changed":changedValue} - options:0 - error:&err]; - if (!jsonData) { - NSLog(@"Parsing Error: %@", [err localizedDescription]); - pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:[err localizedDescription]]; - } else { - NSLog(@"Success"); - NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding]; - pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:jsonString]; + if ([command.arguments count] == 2) { + + NSString *bucket = [command.arguments objectAtIndex:1]; + + [branch redeemRewards:(NSInteger)amount forBucket:(NSString *)bucket callback:^(BOOL changed, NSError *error) { + NSLog(@"inside redeemRewards:forBucket block"); + CDVPluginResult* pluginResult = nil; + if (!error) { + NSNumber *changedValue = [NSNumber numberWithBool:changed]; + NSError *err; + NSData *jsonData = [NSJSONSerialization dataWithJSONObject:@{@"changed":changedValue} + options:0 + error:&err]; + if (!jsonData) { + NSLog(@"Parsing Error: %@", [err localizedDescription]); + pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:[err localizedDescription]]; + } else { + NSLog(@"Success"); + NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding]; + pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:jsonString]; + } } - } - else { - NSLog(@"Redeem Rewards Error: %@", [error localizedDescription]); - pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:[error localizedDescription]]; - } - NSLog(@"returning data to js interface.."); - [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; - }]; + else { + NSLog(@"Redeem Rewards Error: %@", [error localizedDescription]); + pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:[error localizedDescription]]; + } + NSLog(@"returning data to js interface.."); + [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; + }]; + } else { + [branch redeemRewards:amount callback:^(BOOL changed, NSError *error) { + NSLog(@"inside redeemRewards block"); + CDVPluginResult* pluginResult = nil; + if (!error) { + NSNumber *changedValue = [NSNumber numberWithBool:changed]; + NSError *err; + NSData *jsonData = [NSJSONSerialization dataWithJSONObject:@{@"changed":changedValue} + options:0 + error:&err]; + if (!jsonData) { + NSLog(@"Parsing Error: %@", [err localizedDescription]); + pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:[err localizedDescription]]; + } else { + NSLog(@"Success"); + NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding]; + pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:jsonString]; + } + } + else { + NSLog(@"Redeem Rewards Error: %@", [error localizedDescription]); + pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:[error localizedDescription]]; + } + NSLog(@"returning data to js interface.."); + [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; + }]; + } + + } - (void)getCreditHistory:(CDVInvokedUrlCommand*)command diff --git a/src/ios/dependencies/Branch.framework/Versions/A/Headers/BNCServerRequestQueue.h b/src/ios/dependencies/Branch.framework/Versions/A/Headers/BNCServerRequestQueue.h old mode 100755 new mode 100644 diff --git a/testbed/init.sh b/testbed/init.sh old mode 100755 new mode 100644 diff --git a/testbed/www/js/index.js b/testbed/www/js/index.js index fedd5e02..52e260a3 100644 --- a/testbed/www/js/index.js +++ b/testbed/www/js/index.js @@ -232,6 +232,21 @@ function ShowShareSheet() }; branchUniversalObj.showShareSheet(properties, controlParams); + + // Set listeners + branchUniversalObj.onShareSheetLaunched(function () { + console.log('Share sheet launched'); + }); + branchUniversalObj.onShareSheetDismissed(function () { + console.log('Share sheet dimissed'); + }); + branchUniversalObj.onLinkShareResponse(function (res) { + console.log('Share link response: ' + JSON.stringify(res)); + }); + branchUniversalObj.onChannelSelected(function (res) { + console.log('Channel selected: ' + JSON.stringify(res)); + }); + } function ListOnSpotlight() diff --git a/www/branch.js b/www/branch.js index f884033d..907c32d6 100644 --- a/www/branch.js +++ b/www/branch.js @@ -11,8 +11,8 @@ var _API_CLASS = 'BranchSDK'; // SDK Class /** * Execute SDK method using cordova.exec() * - * @param (String) method - The class method to execute - * @param (Array) params - Method parameter(s) to pass + * @param (String) method - The class method to execute. + * @param (Array) params - Method parameter(s) to pass. * * @return (Promise) */ @@ -20,8 +20,6 @@ function execute(method, params) { params = ( ! params) ? [] : params; - var self = this; - return new Promise(function (resolve, reject) { exec(function (res) { resolve(res); @@ -32,6 +30,22 @@ function execute(method, params) { } +/** + * Set listener callback for SDK method. + * + * @param (String) method - The class method to execute. + * @param (Function) callback - The method listener callback. + * + * @return (Promise) + */ +function executeCallback(method, callback) { + + exec(callback, function (err) { + console.error(err); + }, _API_CLASS, method, []); + +} + /** * @class Branch */ @@ -264,6 +278,50 @@ Branch.prototype.createBranchUniversalObject = function (options) { }; + /** + * Set on share sheet launched listener callback. + * + * @param (Function) callback + */ + res.onShareSheetLaunched = function (callback) { + + executeCallback('onShareLinkDialogLaunched', callback); + + }; + + /** + * Set on share sheet dismissed listener callback. + * + * @param (Function) callback + */ + res.onShareSheetDismissed = function (callback) { + + executeCallback('onShareLinkDialogDismissed', callback); + + }; + + /** + * Set on link share listener callback. + * + * @param (Function) callback + */ + res.onLinkShareResponse = function (callback) { + + executeCallback('onLinkShareResponse', callback); + + }; + + /** + * Set on channel select listener callback. + * + * @param (Function) callback + */ + res.onChannelSelected = function (callback) { + + executeCallback('onChannelSelected', callback); + + }; + /** * List item on Spotlight (iOS Only). */ @@ -294,13 +352,20 @@ Branch.prototype.loadRewards = function () { /** * Redeem rewards to your account. * - * @param (Int) value - The amount to redeem + * @param (Int) value - The amount to redeem. + * @param (String) bucket - The value containing the name of the referral bucket to attempt to redeem credits from. [OPTIONAL] * * @return (Promise) */ -Branch.prototype.redeemRewards = function (value) { +Branch.prototype.redeemRewards = function (value, bucket) { + + var params = [value]; + + if (bucket) { + params.push(bucket); + } - return execute('redeemRewards', [value]); + return execute('redeemRewards', params); };