Skip to content

Commit 75b2cdf

Browse files
authored
Merge pull request #92 from AppsFlyerSDK/dev/userInviteRefactor
Refactoring of user invite feature
2 parents b82bcce + c229066 commit 75b2cdf

File tree

10 files changed

+120
-43
lines changed

10 files changed

+120
-43
lines changed

android/src/main/java/com/appsflyer/appsflyersdk/AppsflyerSdkPlugin.java

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -262,9 +262,9 @@ private void setAppInivteOneLinkID(MethodCall call, Result result) {
262262
result.success(null);
263263
}else{
264264
AppsFlyerLib.getInstance().setAppInviteOneLink(oneLinkId);
265-
if(mCallbacks.containsKey("successSetAppInviteOneLinkID")){
265+
if(mCallbacks.containsKey("setAppInviteOneLinkIDCallback")){
266266
JSONObject obj = buildJsonResponse("success", AF_SUCCESS);
267-
runOnUIThread(obj, "successSetAppInviteOneLinkID", AF_SUCCESS);
267+
runOnUIThread(obj, "setAppInviteOneLinkIDCallback", AF_SUCCESS);
268268
}
269269
}
270270
}
@@ -303,17 +303,29 @@ private void generateInviteLink(MethodCall call, Result rawResult) {
303303
}
304304

305305
CreateOneLinkHttpTask.ResponseListener listener = new CreateOneLinkHttpTask.ResponseListener() {
306+
JSONObject obj = new JSONObject();
307+
306308
@Override
307309
public void onResponse(final String oneLinkUrl) {
308-
if (mCallbacks.containsKey("successGenerateInviteLink")) {
309-
// runOnUIThread(oneLinkUrl, "successGenerateInviteLink", AF_SUCCESS);
310+
if (mCallbacks.containsKey("generateInviteLinkSuccess")) {
311+
try {
312+
obj.put("userInviteUrl", oneLinkUrl);
313+
runOnUIThread(obj, "generateInviteLinkSuccess", AF_SUCCESS);
314+
}catch (JSONException e) {
315+
e.printStackTrace();
316+
}
310317
}
311318
}
312319

313320
@Override
314321
public void onResponseError(final String error) {
315-
if (mCallbacks.containsKey("errorGenerateInviteLink")) {
316-
// runOnUIThread(error, "errorGenerateInviteLink", AF_FAILURE);
322+
if (mCallbacks.containsKey("generateInviteLinkFailure")) {
323+
try {
324+
obj.put("error", error);
325+
runOnUIThread(error, "generateInviteLinkFailure", AF_FAILURE);
326+
}catch (JSONException e) {
327+
e.printStackTrace();
328+
}
317329
}
318330
}
319331
};

doc/API.md

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
- [updateServerUninstallToken](#updateServerUninstallToken)
3333
- [Validate Purchase](#validatePurchase)
3434
- [setPushNotification](#setPushNotification)
35+
- [User Invite](#userInvite)
3536
- [stream](#streams)
3637
---
3738

@@ -385,6 +386,58 @@ Please check the following guide in order to understand the relevant payload nee
385386

386387
https://support.appsflyer.com/hc/en-us/articles/207364076-Measuring-push-notification-re-engagement-campaigns
387388

389+
---
390+
**<a id="userInvite"> User Invite**
391+
392+
1. First define the Onelink ID (find it in the AppsFlyer dashboard in the onelink section:
393+
394+
**`Future<void> setAppInviteOneLinkID(String oneLinkID, Function callback)`**
395+
396+
2. Set the AppsFlyerInviteLinkParams class to set the query params in the user invite link:
397+
398+
```dart
399+
class AppsFlyerInviteLinkParams {
400+
final String channel;
401+
final String campaign;
402+
final String referrerName;
403+
final String referreImageUrl;
404+
final String customerID;
405+
final String baseDeepLink;
406+
final String brandDomain;
407+
}
408+
```
409+
410+
3. Call the generateInviteLink API to generate the user invite link. Use the success and error callbacks for handling.
411+
412+
**`void generateInviteLink(AppsFlyerInviteLinkParams parameters, Function success, Function error)`**
413+
414+
415+
_Example:_
416+
```dart
417+
appsFlyerSdk.setAppInviteOneLinkID('OnelinkID',
418+
(res){
419+
print("setAppInviteOneLinkID callback: $res");
420+
});
421+
422+
AppsFlyerInviteLinkParams inviteLinkParams = new AppsFlyerInviteLinkParams(
423+
channel: "",
424+
referrerName: "",
425+
baseDeepLink: "",
426+
brandDomain: "",
427+
customerID: "",
428+
referreImageUrl: "",
429+
campaign: ""
430+
);
431+
432+
appsFlyerSdk.generateInviteLink(inviteLinkParams,
433+
(result){
434+
print(result);
435+
},
436+
(error){
437+
print(error);
438+
}
439+
);
440+
```
388441
---
389442
### **Conversion Data and on app open attribution for older versions**
390443
For plugin version `6.0.5+2` and below the user can access `conversionDataStream`, `appOpenAttributionStream` and `onDeepLinkingStream` to listen for events (see example app)

example/lib/main_page.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ class MainPageState extends State<MainPage> {
5757
future: _appsflyerSdk.getSDKVersion(),
5858
builder: (BuildContext context, AsyncSnapshot snapshot) {
5959
return Text(snapshot.hasData ? snapshot.data : "");
60-
})
60+
}),
6161
],
6262
),
6363
),

ios/Classes/AppsFlyerStreamHandler.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ NS_ASSUME_NONNULL_BEGIN
1616

1717
@interface AppsFlyerStreamHandler: NSObject<FlutterStreamHandler, AppsFlyerLibDelegate, AppsFlyerDeepLinkDelegate>
1818

19-
- (void) sendValidatePurchaseResponseToFlutter:(NSDictionary*) obj;
19+
- (void)sendResponseToFlutter:(NSString *)responseID status:(NSString *)status data:(NSDictionary *)data;
2020

2121
@end
2222

ios/Classes/AppsFlyerStreamHandler.m

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -155,20 +155,25 @@ - (void)didResolveDeepLink:(AppsFlyerDeepLinkResult* _Nonnull) deepLinkResult {
155155
}
156156
}
157157

158-
- (void)sendValidatePurchaseResponseToFlutter:(NSDictionary *)message{
158+
- (void)sendResponseToFlutter:(NSString *)responseID status:(NSString *)status data:(NSDictionary *)data{
159159
NSError *error;
160-
// NSData *JSON = [NSJSONSerialization dataWithJSONObject:message options:0 error:&error];
161-
NSString *JSONString = [self mapToJson:message withError:error];
160+
NSString *JSONdata;
161+
162+
if(data != nil){
163+
JSONdata = [self mapToJson:data withError:error];
164+
}else{
165+
JSONdata = @"empty data";
166+
}
162167
if (error) {
163168
return;
164169
}
165170
NSDictionary *fullResponse = @{
166-
@"id": afValidatePurchase,
167-
@"data": JSONString,
168-
@"status": afSuccess
171+
@"id": responseID,
172+
@"data": JSONdata,
173+
@"status": status
169174
};
170-
JSONString = [self mapToJson:fullResponse withError:error];
171-
[AppsflyerSdkPlugin.callbackChannel invokeMethod:@"callListener" arguments:JSONString];
175+
JSONdata = [self mapToJson:fullResponse withError:error];
176+
[AppsflyerSdkPlugin.callbackChannel invokeMethod:@"callListener" arguments:JSONdata];
172177
}
173178

174179
@end

ios/Classes/AppsflyerSdkPlugin.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@
3737
#define afGCDCallback @"onInstallConversionData"
3838
#define afOAOACallback @"onAppOpenAttribution"
3939
#define afUDPCallback @"onDeepLinking"
40+
#define afGenerateInviteLinkSuccess @"generateInviteLinkSuccess"
41+
#define afGenerateInviteLinkFailure @"generateInviteLinkFailure"
42+
#define afAppInviteOneLinkID @"appInviteOneLinkID"
4043

4144
// Stream Channels
4245
#define afMethodChannel @"af-api"

ios/Classes/AppsflyerSdkPlugin.m

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -183,12 +183,20 @@ - (void)generateInviteLink:(FlutterMethodCall*)call result:(FlutterResult)result
183183
return generator;
184184
} completionHandler:^(NSURL * _Nullable url) {
185185
NSString * resultURL = url.absoluteString;
186+
NSDictionary* resultURLObject;
186187
if(resultURL != nil){
187-
if([_callbackById containsObject:@"successGenerateInviteLink"]){
188-
[_callbackChannel invokeMethod:@"callListener" arguments:@{
189-
@"id": @"successGenerateInviteLink",
190-
@"data":resultURL
191-
}];
188+
resultURLObject = @{
189+
@"userInviteURL": resultURL
190+
};
191+
if([_callbackById containsObject:afGenerateInviteLinkSuccess]){
192+
[_streamHandler sendResponseToFlutter:afGenerateInviteLinkSuccess status:afSuccess data:resultURLObject];
193+
}
194+
}else{
195+
resultURLObject = @{
196+
@"error": @"The URL wasn't generated!"
197+
};
198+
if([_callbackById containsObject:afGenerateInviteLinkFailure]){
199+
[_streamHandler sendResponseToFlutter:afGenerateInviteLinkFailure status:afFailure data:resultURLObject];
192200
}
193201
}
194202
}];
@@ -199,10 +207,11 @@ - (void)generateInviteLink:(FlutterMethodCall*)call result:(FlutterResult)result
199207
- (void)setAppInviteOneLinkID:(FlutterMethodCall*)call result:(FlutterResult)result{
200208
NSString* oneLinkID = call.arguments[@"oneLinkID"];
201209
[AppsFlyerLib shared].appInviteOneLinkID = oneLinkID;
202-
if([_callbackById containsObject:@"successSetAppInviteOneLinkID"]){
203-
[_callbackChannel invokeMethod:@"callListener" arguments:@{
204-
@"id": @"successSetAppInviteOneLinkID"
205-
}];
210+
if([_callbackById containsObject:@"setAppInviteOneLinkIDCallback"]){
211+
NSDictionary* message = @{
212+
@"status": afSuccess
213+
};
214+
[_streamHandler sendResponseToFlutter:afAppInviteOneLinkID status:afSuccess data:message];
206215
}
207216
result(nil);
208217
}
@@ -294,21 +303,15 @@ - (void)validateAndLogInAppPurchase:(FlutterMethodCall*)call result:(FlutterResu
294303
}
295304

296305
- (void)onValidateSuccess: (NSDictionary*) data{
297-
NSDictionary* message = @{
298-
@"status": afSuccess,
299-
@"data": data
300-
};
301-
302-
[_streamHandler sendValidatePurchaseResponseToFlutter:message];
306+
[_streamHandler sendResponseToFlutter:afValidatePurchase status:afSuccess data:data];
303307
}
304308

305309
-(void)onValidateFail:(NSError*)error{
306-
NSDictionary* message = @{
307-
@"status": afFailure,
308-
@"error": @"error connecting"
309-
};
310-
[_streamHandler sendValidatePurchaseResponseToFlutter:message];
311-
[self performSelectorOnMainThread:@selector(handleCallback:) withObject:@[message,afValidatePurchaseChannel] waitUntilDone:NO];
310+
NSDictionary* errorObject = @{
311+
@"error": error.description
312+
};
313+
[_streamHandler sendResponseToFlutter:afValidatePurchase status:afFailure data:errorObject];
314+
[self performSelectorOnMainThread:@selector(handleCallback:) withObject:@[errorObject,afValidatePurchaseChannel] waitUntilDone:NO];
312315
}
313316

314317
- (void)setAdditionalData:(FlutterMethodCall*)call result:(FlutterResult)result{

lib/src/appsflyer_sdk.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -489,8 +489,8 @@ class AppsflyerSdk {
489489
if (parameters != null) {
490490
paramsMap = _translateInviteLinkParamsToMap(parameters);
491491
}
492-
startListening(success, "successGenerateInviteLink");
493-
startListening(error, "errorGenerateInviteLink");
492+
startListening(success, "generateInviteLinkSuccess");
493+
startListening(error, "generateInviteLinkFailure");
494494
_methodChannel.invokeMethod("generateInviteLink", paramsMap);
495495
}
496496

@@ -512,7 +512,7 @@ class AppsflyerSdk {
512512
///The link that is generated for the user invite will use this OneLink ID as the base link ID
513513
Future<void> setAppInviteOneLinkID(
514514
String oneLinkID, Function callback) async {
515-
startListening(callback, "successSetAppInviteOneLinkID");
515+
startListening(callback, "setAppInviteOneLinkIDCallback");
516516
await _methodChannel.invokeMethod("setAppInviteOneLinkID", {
517517
'oneLinkID': oneLinkID,
518518
});

lib/src/callbacks.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ Future<void> _methodCallHandler(MethodCall call) async {
1919
case "onInstallConversionData":
2020
case "onDeepLinking":
2121
case "validatePurchase":
22+
case "generateInviteLinkSuccess":
2223
String data = callMap["data"];
2324
Map decodedData = jsonDecode(data);
2425
Map fullResponse = {
@@ -28,7 +29,7 @@ Future<void> _methodCallHandler(MethodCall call) async {
2829
_callbacksById[callMap["id"]](fullResponse);
2930
break;
3031
default:
31-
_callbacksById[call.arguments["id"]](call.arguments["data"]);
32+
_callbacksById[callMap["id"]](callMap["data"]);
3233
break;
3334
}
3435
} catch (e) {

test/appsflyer_sdk_test.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ void main() {
6767
case 'setSharingFilter':
6868
case 'getSDKVersion':
6969
case 'getAppsFlyerUID':
70-
case 'validateAndLogInAppPurchase':
70+
case 'validateAndLogInAppAndroidPurchase':
7171
case 'setMinTimeBetweenSessions':
7272
case 'getHostPrefix':
7373
case 'getHostName':
@@ -189,7 +189,7 @@ void main() {
189189
});
190190

191191
test('check validateAndLogInAppPurchase call', () async {
192-
instance.validateAndLogInAppPurchase(
192+
instance.validateAndLogInAppAndroidPurchase(
193193
"publicKey", "signature", "purchaseData", "price", "currency", null);
194194

195195
expect(selectedMethod, 'listen');

0 commit comments

Comments
 (0)