Skip to content

Commit c538ead

Browse files
committed
bridge: add phone number and sms subscription apis
1 parent b8875d7 commit c538ead

File tree

6 files changed

+116
-14
lines changed

6 files changed

+116
-14
lines changed

dist/src/android/interop/Bridge.java

+26
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import com.batch.android.Batch;
1818
import com.batch.android.BatchAttributesFetchListener;
1919
import com.batch.android.BatchEmailSubscriptionState;
20+
import com.batch.android.BatchSMSSubscriptionState;
2021
import com.batch.android.BatchEventAttributes;
2122
import com.batch.android.BatchMessage;
2223
import com.batch.android.BatchMigration;
@@ -410,6 +411,31 @@ private static void editProfileAttributes(Map<String, Object> parameters) throws
410411
Log.e(TAG, "Invalid SET_EMAIL_MARKETING_SUBSCRIPTION value: it can only be `subscribed` or `unsubscribed`.");
411412
}
412413
}
414+
case "SET_PHONE_NUMBER" -> {
415+
Object value = operationDescription.get("value");
416+
if (value != null && !(value instanceof String)) {
417+
Log.e(TAG, "Invalid SET_PHONE_NUMBER value: it can only be a string or null");
418+
// Invalid value, continue. NULL is allowed though
419+
continue;
420+
}
421+
editor.setPhoneNumber((String) value);
422+
}
423+
case "SET_SMS_MARKETING_SUB" -> {
424+
Object value = operationDescription.get("value");
425+
if (value == null || !(value instanceof String)) {
426+
Log.e(TAG, "Invalid SET_SMS_MARKETING_SUB value: it can only be a string");
427+
// Invalid value, continue.
428+
continue;
429+
}
430+
431+
if ("subscribed".equals(value)) {
432+
editor.setSMSMarketingSubscription(BatchSMSSubscriptionState.SUBSCRIBED);
433+
} else if ("unsubscribed".equals(value)) {
434+
editor.setSMSMarketingSubscription(BatchSMSSubscriptionState.UNSUBSCRIBED);
435+
} else {
436+
Log.e(TAG, "Invalid SET_SMS_MARKETING_SUB value: it can only be `subscribed` or `unsubscribed`.");
437+
}
438+
}
413439
case "SET_ATTRIBUTE" -> {
414440
String key = getTypedParameter(operationDescription, "key", String.class);
415441
String type = getTypedParameter(operationDescription, "type", String.class);

dist/src/ios/interop/BatchProfileBridge.m

+14
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,20 @@ + (void)editAttributes:(NSDictionary*)params
6060
NSLog(@"Batch Bridge - Invalid value for email marketing subscription state. Must be `subscribed` or `unsubscribed`.");
6161
}
6262
}
63+
else if([@"SET_PHONE_NUMBER" isEqualToString:operationName])
64+
{
65+
[editor setPhoneNumber:[BatchBridgeUtils nullableString:operationDescription forKey:@"value"] error:nil];
66+
}
67+
else if([@"SET_SMS_MARKETING_SUB" isEqualToString:operationName]) {
68+
NSString* value = [operationDescription objectForKey:@"value"];
69+
if([[value uppercaseString] isEqualToString:@"SUBSCRIBED"]) {
70+
[editor setSMSMarketingSubscriptionState:BatchSMSSubscriptionStateSubscribed];
71+
} else if ([[value uppercaseString] isEqualToString:@"UNSUBSCRIBED"]) {
72+
[editor setSMSMarketingSubscriptionState: BatchSMSSubscriptionStateUnsubscribed];
73+
} else {
74+
NSLog(@"Batch Bridge - Invalid value for SMS marketing subscription state. Must be `subscribed` or `unsubscribed`.");
75+
}
76+
}
6377
else if ([@"SET_ATTRIBUTE" isEqualToString:operationName])
6478
{
6579
NSString *type = operationDescription[@"type"];

src/actions.ts

+2
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ export enum ProfileAttributeOperation {
6262
SetRegion = "SET_REGION",
6363
SetEmail = "SET_EMAIL_ADDRESS",
6464
SetEmailMarketingSubscription = "SET_EMAIL_MARKETING_SUB",
65+
SetPhoneNumber = "SET_PHONE_NUMBER",
66+
SetSMSMarketingSubscription = "SET_SMS_MARKETING_SUB",
6567
SetAttribute = "SET_ATTRIBUTE",
6668
RemoveAttribute = "REMOVE_ATTRIBUTE",
6769
AddToArray = "ADD_TO_ARRAY",

src/batchStub.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -132,13 +132,18 @@ class BatchUserDataEditorStub implements BatchSDK.BatchProfileAttributeEditor {
132132
public setRegion(_region: string | null) {
133133
return this;
134134
}
135-
136135
public setEmailAddress(_email: string | null) {
137136
return this;
138137
}
139138
public setEmailMarketingSubscription(_state: "subscribed" | "unsubscribed") {
140139
return this;
141140
}
141+
public setPhoneNumber(_phoneNumber: string | null) {
142+
return this;
143+
}
144+
public setSMSMarketingSubscription(_state: "subscribed" | "unsubscribed") {
145+
return this;
146+
}
142147
public setAttribute(
143148
_key: string,
144149
_value: string | number | boolean | Date | URL

src/modules/profile/profileAttributeEditor.ts

+36
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,42 @@ export class BatchProfileAttributeEditor
9696
return this;
9797
}
9898

99+
public setPhoneNumber(phoneNumber: string | null): this {
100+
if (typeof phoneNumber !== "string" && phoneNumber !== null) {
101+
writeBatchLog(
102+
false,
103+
"BatchProfileAttributeEditor - Phone number must be a string or null"
104+
);
105+
return this;
106+
}
107+
this._enqueueOperation(ProfileAttributeOperation.SetPhoneNumber, {
108+
value: phoneNumber,
109+
});
110+
return this;
111+
}
112+
113+
public setSMSMarketingSubscription(
114+
state: "subscribed" | "unsubscribed"
115+
): this {
116+
if (
117+
typeof state !== "string" ||
118+
(state !== "subscribed" && state !== "unsubscribed")
119+
) {
120+
writeBatchLog(
121+
false,
122+
"BatchProfileAttributeEditor - SMS marketing subscription state must be `subscribed` or `unsubscribed`."
123+
);
124+
return this;
125+
}
126+
this._enqueueOperation(
127+
ProfileAttributeOperation.SetSMSMarketingSubscription,
128+
{
129+
value: state,
130+
}
131+
);
132+
return this;
133+
}
134+
99135
public setAttribute(
100136
key: string,
101137
value: string | number | boolean | Date | URL | Array<string>

types/index.d.ts

+32-13
Original file line numberDiff line numberDiff line change
@@ -146,11 +146,11 @@ export declare namespace BatchSDK {
146146
* - Prevent batch.start()
147147
* - Disable any network capability from the SDK
148148
* - Disable all In-App campaigns
149-
* - Make the Inbox module return an error immediatly when used
149+
* - Make the Inbox module return an error immediately when used
150150
* - Make the SDK reject any editor calls
151-
* - Make the SDK reject calls to batch.user.trackEvent(), batch.user.trackTransaction(), batch.user.trackLocation() and any related methods
151+
* - Make the SDK reject calls to batch.profile.trackEvent(), batch.profile.trackLocation() and any related methods
152152
*
153-
* Even if you opt in afterwards, data that has been generated while opted out WILL be lost.
153+
* Even if you opt in afterward, data that has been generated while opted out WILL be lost.
154154
*
155155
* If you're also looking at deleting user data, please use batch.optOutAndWipeData()
156156
*
@@ -282,8 +282,8 @@ export declare namespace BatchSDK {
282282
identify(identifier: string | null): void;
283283

284284
/**
285-
* Get the user data editor. Don't forget to call save when you're done.
286-
* @return Batch user data editor
285+
* Get the profile data editor. Don't forget to call save when you're done.
286+
* @return Batch profile data editor
287287
*/
288288
getEditor(): BatchProfileAttributeEditor;
289289

@@ -318,20 +318,20 @@ export declare namespace BatchSDK {
318318
getInstallationID(): Promise<undefined | string>;
319319

320320
/**
321-
* Get the application language override set using BatchUserDataEditor. Batch must be started to read it.
321+
* Get the application language override set using BatchProfileAttributeEditor. Batch must be started to read it.
322322
* The promise will return the language you have previously set, if any, or undefined. Might be null/undefined if Batch isn't started.
323323
* Might throw if Batch isn't started.
324324
*/
325325
getLanguage(): Promise<undefined | string>;
326326

327327
/**
328-
* Get the application region override set using BatchUserDataEditor. Batch must be started to read it.
328+
* Get the application region override set using BatchProfileAttributeEditor. Batch must be started to read it.
329329
* The promise will return the region you have previously set, if any, or undefined. Might be null/undefined if Batch isn't started.
330330
*/
331331
getRegion(): Promise<undefined | string>;
332332

333333
/**
334-
* Get the user identifier set using BatchUserDataEditor. Batch must be started to read it.
334+
* Get the user identifier set using BatchProfileAttributeEditor. Batch must be started to read it.
335335
* The promise will return the user identifier you have previously set, if any, or undefined. Might be null/undefined if Batch isn't started.
336336
*/
337337
getIdentifier(): Promise<undefined | string>;
@@ -417,8 +417,8 @@ export declare namespace BatchSDK {
417417
setiOSNotificationTypes(notifTypes: iOSNotificationTypes): void;
418418

419419
/**
420-
* Set whether notifications should be show in the foreground on iOS.
421-
* If true, notifications will be shown like if the user was outside of your application and
420+
* Set whether notifications should be shown in the foreground on iOS.
421+
* If true, notifications will be shown like if the user was outside your application and
422422
* `batchPushReceived` will only be triggered when the notification is tapped.
423423
* @param showForegroundNotifications Show foreground notifications?
424424
*/
@@ -658,7 +658,7 @@ export declare namespace BatchSDK {
658658
}
659659

660660
/**
661-
* User data editor
661+
* Profile attribute editor
662662
*/
663663
interface BatchProfileAttributeEditor {
664664
/**
@@ -676,7 +676,7 @@ export declare namespace BatchSDK {
676676
setRegion(region: string | null): BatchProfileAttributeEditor;
677677

678678
/**
679-
* Set the user email address.
679+
* Set the profile email address.
680680
*
681681
* This requires to have a custom user ID registered
682682
* or to call the `setIdentifier` method on the editor instance beforehand.
@@ -685,14 +685,33 @@ export declare namespace BatchSDK {
685685
setEmailAddress(email: string | null): BatchProfileAttributeEditor;
686686

687687
/**
688-
* Set the user email marketing subscription state
688+
* Set the profile email marketing subscription state
689689
*
690690
* @param state The state of the marketing email subscription. Must be "subscribed" or "unsubscribed".
691691
*/
692692
setEmailMarketingSubscription(
693693
state: "subscribed" | "unsubscribed"
694694
): BatchProfileAttributeEditor;
695695

696+
/**
697+
* Set the profile phone number.
698+
*
699+
* This requires to have a custom profile ID registered or to call the `identify` method beforehand.
700+
* @param phoneNumber A valid E.164 formatted string. Must start with a `+` and not be longer than 15 digits
701+
* without special characters (eg: "+33123456789"). Null to reset.
702+
*/
703+
setPhoneNumber(phoneNumber: string | null): BatchProfileAttributeEditor;
704+
705+
/**
706+
* Set the profile SMS marketing subscription state.
707+
*
708+
* Note that profile's subscription status is automatically set to unsubscribed when users send a STOP message.
709+
* @param state The state of the SMS marketing subscription. Must be "subscribed" or "unsubscribed".
710+
*/
711+
setSMSMarketingSubscription(
712+
state: "subscribed" | "unsubscribed"
713+
): BatchProfileAttributeEditor;
714+
696715
/**
697716
* Set an attribute for a key
698717
* @param key Attribute key. Cannot be null, empty or undefined. It should be made of letters, numbers or underscores ([a-z0-9_]) and can't be longer than 30 characters.

0 commit comments

Comments
 (0)