Skip to content

Commit 80350cf

Browse files
committed
Add Set External ID
• Adds functions to set an external ID for a user so that developers can use their own internal userID's with OneSignal
1 parent fd74628 commit 80350cf

File tree

3 files changed

+61
-1
lines changed

3 files changed

+61
-1
lines changed

example/lib/main.dart

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ class MyApp extends StatefulWidget {
1414
class _MyAppState extends State<MyApp> {
1515
String _debugLabelString = "";
1616
String _emailAddress;
17+
String _externalUserId;
1718
bool _enableConsentButton = false;
1819

1920
// CHANGE THIS parameter to true if you want to test GDPR privacy consent
@@ -141,6 +142,21 @@ class _MyAppState extends State<MyApp> {
141142
});
142143
}
143144

145+
void _handleSetExternalUserId() {
146+
print("Setting external user ID");
147+
OneSignal.shared.setExternalUserId(_externalUserId);
148+
this.setState(() {
149+
_debugLabelString = "Set External User ID";
150+
});
151+
}
152+
153+
void _handleRemoveExternalUserId() {
154+
OneSignal.shared.removeExternalUserId();
155+
this.setState(() {
156+
_debugLabelString = "Removed external user ID";
157+
});
158+
}
159+
144160
void _handleConsent() {
145161
print("Setting consent to true");
146162
OneSignal.shared.consentGranted(true);
@@ -285,12 +301,40 @@ class _MyAppState extends State<MyApp> {
285301
new OneSignalButton("Post Silent Notification",
286302
_handleSendSilentNotification, !_enableConsentButton)
287303
]),
304+
new TableRow(children: [
305+
new TextField(
306+
textAlign: TextAlign.center,
307+
decoration: InputDecoration(
308+
hintText: "External User ID",
309+
labelStyle: TextStyle(
310+
color: Color.fromARGB(255, 212, 86, 83),
311+
)),
312+
onChanged: (text) {
313+
this.setState(() {
314+
_externalUserId = text == "" ? null : text;
315+
});
316+
},
317+
)
318+
]),
319+
new TableRow(children: [
320+
Container(
321+
height: 8.0,
322+
)
323+
]),
324+
new TableRow(children: [
325+
new OneSignalButton(
326+
"Set External User ID", _handleSetExternalUserId, !_enableConsentButton)
327+
]),
328+
new TableRow(children: [
329+
new OneSignalButton(
330+
"Remove External User ID", _handleRemoveExternalUserId, !_enableConsentButton)
331+
]),
288332
new TableRow(children: [
289333
new Container(
290334
child: new Text(_debugLabelString),
291335
alignment: Alignment.center,
292336
)
293-
])
337+
]),
294338
],
295339
),
296340
),

ios/Classes/OneSignalPlugin.m

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,10 @@ - (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
126126
[self logoutEmail:call withResult:result];
127127
} else if ([@"OneSignal#didSetNotificationOpenedHandler" isEqualToString:call.method]) {
128128
[self didSetNotificationOpenedHandler];
129+
} else if ([@"OneSignal#setExternalUserId" isEqualToString:call.method]) {
130+
[OneSignal setExternalUserId:call.arguments[@"externalUserId"]];
131+
} else if ([@"OneSignal#removeExternalUserId" isEqualToString:call.method]) {
132+
[OneSignal removeExternalUserId];
129133
} else {
130134
result(FlutterMethodNotImplemented);
131135
}

lib/onesignal.dart

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,18 @@ class OneSignal {
262262
return await _channel.invokeMethod("OneSignal#logoutEmail");
263263
}
264264

265+
/// OneSignal allows you to set a custom ID for your users. This makes it so that
266+
/// if your app has its own user ID's, you can use your own custom user ID's with
267+
/// our API instead of having to save their OneSignal user ID's.
268+
Future<void> setExternalUserId(String externalId) async {
269+
return await _channel.invokeMethod("OneSignal#setExternalUserId", {'externalUserId' : externalId});
270+
}
271+
272+
/// Removes the external user ID that was set for the current user.
273+
Future<void> removeExternalUserId() async {
274+
return await _channel.invokeMethod("OneSignal#removeExternalUserId");
275+
}
276+
265277
// Private function that gets called by ObjC/Java
266278
Future<Null> _handleMethod(MethodCall call) async {
267279
if (call.method == 'OneSignal#handleReceivedNotification' &&

0 commit comments

Comments
 (0)