Skip to content

Commit 992be95

Browse files
committed
Add SMS SubscriptionObserver
* Add setSMSSubscriptionObserver method * Add example app setSMSSubscriptionObserver logger
1 parent 34423f8 commit 992be95

File tree

6 files changed

+102
-0
lines changed

6 files changed

+102
-0
lines changed

android/src/main/java/com/onesignal/flutter/OneSignalPlugin.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
import com.onesignal.OSNotificationReceivedEvent;
1212
import com.onesignal.OSPermissionObserver;
1313
import com.onesignal.OSPermissionStateChanges;
14+
import com.onesignal.OSSMSSubscriptionObserver;
15+
import com.onesignal.OSSMSSubscriptionStateChanges;
1416
import com.onesignal.OSSubscriptionObserver;
1517
import com.onesignal.OSSubscriptionStateChanges;
1618
import com.onesignal.OneSignal;
@@ -39,6 +41,7 @@ public class OneSignalPlugin
3941
OneSignal.OSInAppMessageClickHandler,
4042
OSSubscriptionObserver,
4143
OSEmailSubscriptionObserver,
44+
OSSMSSubscriptionObserver,
4245
OSPermissionObserver,
4346
OneSignal.OSNotificationWillShowInForegroundHandler {
4447

@@ -157,6 +160,7 @@ private void setAppId(MethodCall call, Result reply) {
157160
private void addObservers() {
158161
OneSignal.addSubscriptionObserver(this);
159162
OneSignal.addEmailSubscriptionObserver(this);
163+
OneSignal.addSMSSubscriptionObserver(this);
160164
OneSignal.addPermissionObserver(this);
161165
OneSignal.setNotificationWillShowInForegroundHandler(this);
162166
}
@@ -438,6 +442,11 @@ public void onOSEmailSubscriptionChanged(OSEmailSubscriptionStateChanges stateCh
438442
invokeMethodOnUiThread("OneSignal#emailSubscriptionChanged", OneSignalSerializer.convertEmailSubscriptionStateChangesToMap(stateChanges));
439443
}
440444

445+
@Override
446+
public void onSMSSubscriptionChanged(OSSMSSubscriptionStateChanges stateChanges) {
447+
invokeMethodOnUiThread("OneSignal#smsSubscriptionChanged", OneSignalSerializer.convertSMSSubscriptionStateChangesToMap(stateChanges));
448+
}
449+
441450
@Override
442451
public void onOSPermissionChanged(OSPermissionStateChanges stateChanges) {
443452
invokeMethodOnUiThread("OneSignal#permissionChanged", OneSignalSerializer.convertPermissionStateChangesToMap(stateChanges));
@@ -483,4 +492,5 @@ public void notificationWillShowInForeground(OSNotificationReceivedEvent notific
483492
"Encountered an error attempting to convert OSNotificationReceivedEvent object to hash map: " + e.getMessage());
484493
}
485494
}
495+
486496
}

android/src/main/java/com/onesignal/flutter/OneSignalSerializer.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
import com.onesignal.OSOutcomeEvent;
1414
import com.onesignal.OSPermissionState;
1515
import com.onesignal.OSPermissionStateChanges;
16+
import com.onesignal.OSSMSSubscriptionState;
17+
import com.onesignal.OSSMSSubscriptionStateChanges;
1618
import com.onesignal.OSSubscriptionState;
1719
import com.onesignal.OSSubscriptionStateChanges;
1820
import com.onesignal.OSOutcomeEvent;
@@ -56,6 +58,16 @@ private static HashMap<String, Object> convertEmailSubscriptionStateToMap(OSEmai
5658
return hash;
5759
}
5860

61+
private static HashMap<String, Object> convertSMSSubscriptionStateToMap(OSSMSSubscriptionState state) {
62+
HashMap<String, Object> hash = new HashMap<>();
63+
64+
hash.put("smsUserId", state.getSMSNumber());
65+
hash.put("smsNumber", state.getSMSNumber());
66+
hash.put("isSubscribed", state.isSubscribed());
67+
68+
return hash;
69+
}
70+
5971
static HashMap<String, Object> convertDeviceStateToMap(OSDeviceState state) {
6072
HashMap<String, Object> hash = new HashMap<>();
6173

@@ -92,6 +104,15 @@ static HashMap<String, Object> convertEmailSubscriptionStateChangesToMap(OSEmail
92104
return hash;
93105
}
94106

107+
static HashMap<String, Object> convertSMSSubscriptionStateChangesToMap(OSSMSSubscriptionStateChanges changes) {
108+
HashMap<String, Object> hash = new HashMap<>();
109+
110+
hash.put("to", convertSMSSubscriptionStateToMap(changes.getTo()));
111+
hash.put("from", convertSMSSubscriptionStateToMap(changes.getFrom()));
112+
113+
return hash;
114+
}
115+
95116
static HashMap convertPermissionStateChangesToMap(OSPermissionStateChanges changes) {
96117
HashMap<String, Object> hash = new HashMap<>();
97118

example/lib/main.dart

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,11 @@ class _MyAppState extends State<MyApp> {
7878
print("EMAIL SUBSCRIPTION STATE CHANGED ${changes.jsonRepresentation()}");
7979
});
8080

81+
OneSignal.shared.setSMSSubscriptionObserver(
82+
(OSSMSSubscriptionStateChanges changes) {
83+
print("SMS SUBSCRIPTION STATE CHANGED ${changes.jsonRepresentation()}");
84+
});
85+
8186
// NOTE: Replace with your own app ID from https://www.onesignal.com
8287
await OneSignal.shared
8388
.setAppId("380dc082-5231-4cc2-ab51-a03da5a0e4c2");

ios/Classes/OneSignalPlugin.m

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ - (void)addObservers {
9999
[OneSignal addSubscriptionObserver:self];
100100
[OneSignal addPermissionObserver:self];
101101
[OneSignal addEmailSubscriptionObserver:self];
102+
[OneSignal addSMSSubscriptionObserver:self];
102103
[OneSignal setNotificationWillShowInForegroundHandler:^(OSNotification *notification, OSNotificationDisplayResponse completion) {
103104
[OneSignalPlugin.sharedInstance handleNotificationWillShowInForeground:notification completion:completion];
104105
}];
@@ -399,4 +400,9 @@ - (void)onOSEmailSubscriptionChanged:(OSEmailSubscriptionStateChanges *)stateCha
399400
[self.channel invokeMethod:@"OneSignal#emailSubscriptionChanged" arguments:stateChanges.toDictionary];
400401
}
401402

403+
#pragma mark OSSMSSubscriptionObserver
404+
- (void)onOSSMSSubscriptionChanged:(OSSMSSubscriptionStateChanges *)stateChanges {
405+
[self.channel invokeMethod:@"OneSignal#smsSubscriptionChanged" arguments:stateChanges.toDictionary];
406+
}
407+
402408
@end

lib/onesignal_flutter.dart

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ typedef void ReceivedNotificationHandler(OSNotification notification);
2222
typedef void OpenedNotificationHandler(OSNotificationOpenedResult openedResult);
2323
typedef void SubscriptionChangedHandler(OSSubscriptionStateChanges changes);
2424
typedef void EmailSubscriptionChangeHandler(OSEmailSubscriptionStateChanges changes);
25+
typedef void SMSSubscriptionChangeHandler(OSSMSSubscriptionStateChanges changes);
2526
typedef void PermissionChangeHandler(OSPermissionStateChanges changes);
2627
typedef void InAppMessageClickedHandler(OSInAppMessageAction action);
2728
typedef void NotificationWillShowInForegroundHandler(OSNotificationReceivedEvent event);
@@ -43,6 +44,7 @@ class OneSignal {
4344
OpenedNotificationHandler? _onOpenedNotification;
4445
SubscriptionChangedHandler? _onSubscriptionChangedHandler;
4546
EmailSubscriptionChangeHandler? _onEmailSubscriptionChangedHandler;
47+
SMSSubscriptionChangeHandler? _onSMSSubscriptionChangedHandler;
4648
PermissionChangeHandler? _onPermissionChangedHandler;
4749
InAppMessageClickedHandler? _onInAppMessageClickedHandler;
4850
NotificationWillShowInForegroundHandler? _onNotificationWillShowInForegroundHandler;
@@ -98,6 +100,13 @@ class OneSignal {
98100
_onEmailSubscriptionChangedHandler = handler;
99101
}
100102

103+
/// The SMS subscription handler will be called whenever the user's SMS
104+
/// subscription changes (OneSignal can also send SMSs in addition to push
105+
/// notifications). For example, if you call setSMSNumber() or logoutSMSNumber().
106+
void setSMSSubscriptionObserver(SMSSubscriptionChangeHandler handler) {
107+
_onSMSSubscriptionChangedHandler = handler;
108+
}
109+
101110
/// The in app message clicked handler is called whenever the user clicks a
102111
/// OneSignal IAM button or image with an action event attacthed to it
103112
void setInAppMessageClickedHandler(InAppMessageClickedHandler handler) {
@@ -405,6 +414,10 @@ class OneSignal {
405414
this._onEmailSubscriptionChangedHandler != null) {
406415
this._onEmailSubscriptionChangedHandler!(
407416
OSEmailSubscriptionStateChanges(call.arguments.cast<String, dynamic>()));
417+
} else if (call.method == 'OneSignal#smsSubscriptionChanged' &&
418+
this._onSMSSubscriptionChangedHandler != null) {
419+
this._onSMSSubscriptionChangedHandler!(
420+
OSSMSSubscriptionStateChanges(call.arguments.cast<String, dynamic>()));
408421
} else if (call.method == 'OneSignal#handleClickedInAppMessage' &&
409422
this._onInAppMessageClickedHandler != null) {
410423
this._onInAppMessageClickedHandler!(

lib/src/subscription.dart

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,3 +105,50 @@ class OSEmailSubscriptionStateChanges extends JSONStringRepresentable {
105105
});
106106
}
107107
}
108+
109+
/// Represents the user's OneSignal SMS subscription state,
110+
class OSSMSSubscriptionState extends JSONStringRepresentable {
111+
bool subscribed = false;
112+
String? smsUserId;
113+
String? smsNumber;
114+
115+
OSSMSSubscriptionState(Map<String, dynamic> json) {
116+
this.subscribed = false;
117+
if (json.containsKey('smsNumber') && json['smsNumber'] != null)
118+
this.smsNumber = json['smsNumber'] as String?;
119+
120+
if (json.containsKey('smsUserId') && json['smsUserId'] != null) {
121+
this.smsUserId = json['smsUserId'] as String?;
122+
this.subscribed = true;
123+
}
124+
}
125+
126+
String jsonRepresentation() {
127+
return convertToJsonString({
128+
'subscribed': this.subscribed,
129+
'smsUserId': this.smsUserId,
130+
'smsNumber': this.smsNumber
131+
});
132+
}
133+
}
134+
135+
/// An instance of this class describes a change in the user's
136+
/// email subscription state with OneSignal
137+
class OSSMSSubscriptionStateChanges extends JSONStringRepresentable {
138+
late OSSMSSubscriptionState from;
139+
late OSSMSSubscriptionState to;
140+
141+
OSSMSSubscriptionStateChanges(Map<String, dynamic> json) {
142+
if (json.containsKey('from'))
143+
this.from = OSSMSSubscriptionState(json['from'].cast<String, dynamic>());
144+
if (json.containsKey('to'))
145+
this.to = OSSMSSubscriptionState(json['to'].cast<String, dynamic>());
146+
}
147+
148+
String jsonRepresentation() {
149+
return convertToJsonString(<String, dynamic>{
150+
'from': this.from.jsonRepresentation(),
151+
'to': this.to.jsonRepresentation()
152+
});
153+
}
154+
}

0 commit comments

Comments
 (0)