Skip to content

Commit 05b5032

Browse files
authored
Merge pull request #39 from OneSignal/set_external_id
Set External ID
2 parents bb124ae + 27ddffe commit 05b5032

File tree

11 files changed

+102
-9
lines changed

11 files changed

+102
-9
lines changed

android/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ android {
3434
}
3535

3636
dependencies {
37-
compile('com.onesignal:OneSignal:3.10.3')
37+
compile('com.onesignal:OneSignal:3.10.5')
3838
}
3939

4040
// Adds required manifestPlaceholders keys to allow mainifest merge gradle step to complete

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,10 @@ else if (call.method.contentEquals("OneSignal#promptPermission"))
9292
Log.e("onesignal", "promptPermission() is not applicable in Android.");
9393
else if (call.method.contentEquals("OneSignal#didSetNotificationOpenedHandler"))
9494
this.didSetNotificationOpenedHandler();
95+
else if (call.method.contentEquals("OneSignal#setExternalUserId"))
96+
this.setExternalUserId(call, result);
97+
else if (call.method.contentEquals("OneSignal#removeExternalUserId"))
98+
this.removeExternalUserId(result);
9599
else
96100
result.notImplemented();
97101
}
@@ -250,6 +254,18 @@ private void didSetNotificationOpenedHandler() {
250254
}
251255
}
252256

257+
private void setExternalUserId(MethodCall call, Result result) {
258+
OneSignal.setExternalUserId((String)call.argument("externalUserId"));
259+
260+
result.success(null);
261+
}
262+
263+
private void removeExternalUserId(Result result) {
264+
OneSignal.removeExternalUserId();
265+
266+
result.success(null);
267+
}
268+
253269
@Override
254270
public void onOSSubscriptionChanged(OSSubscriptionStateChanges stateChanges) {
255271
this.channel.invokeMethod("OneSignal#subscriptionChanged", OneSignalSerializer.convertSubscriptionStateChangesToMap(stateChanges));

example/ios/Podfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ target 'Runner' do
5555
end
5656

5757
target 'OneSignalNotificationServiceExtension' do
58-
pod 'OneSignal', '>= 2.9.3', '< 3.0'
58+
pod 'OneSignal', '>= 2.9.4', '< 3.0'
5959
end
6060

6161
post_install do |installer|

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
@@ -165,6 +166,21 @@ class _MyAppState extends State<MyApp> {
165166
});
166167
}
167168

169+
void _handleSetExternalUserId() {
170+
print("Setting external user ID");
171+
OneSignal.shared.setExternalUserId(_externalUserId);
172+
this.setState(() {
173+
_debugLabelString = "Set External User ID";
174+
});
175+
}
176+
177+
void _handleRemoveExternalUserId() {
178+
OneSignal.shared.removeExternalUserId();
179+
this.setState(() {
180+
_debugLabelString = "Removed external user ID";
181+
});
182+
}
183+
168184
void _handleSendNotification() async {
169185
var status = await OneSignal.shared.getPermissionSubscriptionState();
170186

@@ -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
),

example/pubspec.yaml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@ description: Demonstrates how to use the onesignal plugin.
44
dependencies:
55
flutter:
66
sdk: flutter
7-
8-
flutter_test:
9-
sdk: flutter
107
# The following adds the Cupertino Icons font to your application.
118
# Use with the CupertinoIcons class for iOS style icons.
129
cupertino_icons: ^0.1.2

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
}

ios/onesignal.podspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ Pod::Spec.new do |s|
1313
s.source_files = 'Classes/**/*'
1414
s.public_header_files = 'Classes/**/*.h'
1515
s.dependency 'Flutter'
16-
s.dependency 'OneSignal', '>= 2.9.3', '< 3.0'
16+
s.dependency 'OneSignal', '>= 2.9.4', '< 3.0'
1717
s.ios.deployment_target = '8.0'
1818
end
1919

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' &&

pubspec.yaml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@ dependencies:
1414
sdk: flutter
1515

1616
dev_dependencies:
17+
test: ^1.5.1
1718
flutter_test:
1819
sdk: flutter
1920

2021
environment:
21-
sdk: ">=2.1.0-dev.7.1 <3.0.0"
22-
flutter: ">=0.10.1-pre.45 <2.0.0"
22+
sdk: ">=2.0.0-dev.28.0 <3.0.0"
23+
flutter: ">=0.1.4 <2.0.0"

test/mock_channel.dart

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,12 @@ class OneSignalMockChannelController {
6464
case "OneSignal#deleteTags":
6565
this.state.deleteTags = call.arguments;
6666
return {"success": true};
67+
case "OneSignal#setExternalUserId":
68+
this.state.externalId = (call.arguments as Map<dynamic, dynamic>)['externalUserId'] as String;
69+
return {"success" : true};
70+
case "OneSignal#removeExternalUserId":
71+
this.state.externalId = null;
72+
return {"success" : true};
6773
}
6874
}
6975
}
@@ -90,6 +96,7 @@ class OneSignalState {
9096
bool locationShared;
9197
OSNotificationDisplayType inFocusDisplayType;
9298
bool subscriptionState;
99+
String externalId;
93100

94101
// tags
95102
Map<dynamic, dynamic> tags;

0 commit comments

Comments
 (0)