Skip to content

Commit 62f909a

Browse files
committed
adding Android impl for Location namespace
1 parent 284bba3 commit 62f909a

File tree

3 files changed

+133
-85
lines changed

3 files changed

+133
-85
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.onesignal.flutter;
2+
3+
import com.onesignal.OneSignal;
4+
import com.onesignal.Continue;
5+
6+
import org.json.JSONException;
7+
import org.json.JSONObject;
8+
9+
import java.util.List;
10+
import java.util.Map;
11+
import java.util.concurrent.atomic.AtomicBoolean;
12+
13+
import io.flutter.plugin.common.BinaryMessenger;
14+
import io.flutter.plugin.common.MethodCall;
15+
import io.flutter.plugin.common.MethodChannel;
16+
import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
17+
import io.flutter.plugin.common.MethodChannel.Result;
18+
import io.flutter.plugin.common.PluginRegistry;
19+
import io.flutter.plugin.common.PluginRegistry.Registrar;
20+
21+
public class OneSignalLocation extends FlutterRegistrarResponder implements MethodCallHandler {
22+
private MethodChannel channel;
23+
24+
static void registerWith(BinaryMessenger messenger) {
25+
OneSignalLocation controller = new OneSignalLocation();
26+
controller.messenger = messenger;
27+
controller.channel = new MethodChannel(messenger, "OneSignal#location");
28+
controller.channel.setMethodCallHandler(controller);
29+
}
30+
31+
@Override
32+
public void onMethodCall(MethodCall call, Result result) {
33+
if (call.method.contentEquals("OneSignal#requestPermission"))
34+
this.requestPermission(result);
35+
else if (call.method.contentEquals("OneSignal#setShared"))
36+
this.setShared(call, result);
37+
else if (call.method.contentEquals("OneSignal#isShared"))
38+
replySuccess(result, OneSignal.getLocation().isShared());
39+
else
40+
replyNotImplemented(result);
41+
}
42+
43+
private void requestPermission(Result reply) {
44+
OneSignal.getLocation().requestPermission(Continue.none());
45+
replySuccess(reply, null);
46+
}
47+
48+
private void setShared(MethodCall call, Result result) {
49+
OneSignal.getLocation().setShared((boolean) call.arguments);
50+
replySuccess(result, null);
51+
}
52+
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ private void init(Context context, BinaryMessenger messenger)
4747
channel.setMethodCallHandler(this);
4848

4949
OneSignalDebug.registerWith(messenger);
50+
OneSignalLocation.registerWith(messenger);
5051
// OneSignalTagsController.registerWith(messenger);
5152
// OneSignalInAppMessagingController.registerWith(messenger);
5253
// OneSignalOutcomeEventsController.registerWith(messenger);

example/lib/main.dart

Lines changed: 80 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class _MyAppState extends State<MyApp> with OneSignalPushSubscriptionObserver {
3232
Future<void> initPlatformState() async {
3333
if (!mounted) return;
3434

35-
OneSignal.Debug.setLogLevel(OSLogLevel.debug);
35+
OneSignal.Debug.setLogLevel(OSLogLevel.none);
3636

3737
OneSignal.Debug.setAlertLevel(OSLogLevel.none);
3838

@@ -48,30 +48,31 @@ class _MyAppState extends State<MyApp> with OneSignalPushSubscriptionObserver {
4848

4949
// OneSignal.shared.setRequiresUserPrivacyConsent(_requireConsent);
5050

51-
OneSignal.Notifications
52-
.setNotificationOpenedHandler((OSNotificationOpenedResult result) {
53-
print('NOTIFICATION OPENED HANDLER CALLED WITH: ${result}');
54-
this.setState(() {
55-
_debugLabelString =
56-
"Opened notification: \n${result.notification.jsonRepresentation().replaceAll("\\n", "\n")}";
51+
OneSignal.Notifications.setNotificationOpenedHandler(
52+
(OSNotificationOpenedResult result) {
53+
print('NOTIFICATION OPENED HANDLER CALLED WITH: ${result}');
54+
this.setState(() {
55+
_debugLabelString =
56+
"Opened notification: \n${result.notification.jsonRepresentation().replaceAll("\\n", "\n")}";
5757
});
5858
});
5959

60-
OneSignal.Notifications
61-
.setNotificationWillShowInForegroundHandler((OSNotificationReceivedEvent event) {
62-
print('FOREGROUND HANDLER CALLED WITH: ${event}');
63-
/// Display Notification, send null to not display
64-
event.complete(null);
65-
66-
this.setState(() {
67-
_debugLabelString =
68-
"Notification received in foreground notification: \n${event.notification.jsonRepresentation().replaceAll("\\n", "\n")}";
60+
OneSignal.Notifications.setNotificationWillShowInForegroundHandler(
61+
(OSNotificationReceivedEvent event) {
62+
print('FOREGROUND HANDLER CALLED WITH: ${event}');
63+
64+
/// Display Notification, send null to not display
65+
event.complete(null);
66+
67+
this.setState(() {
68+
_debugLabelString =
69+
"Notification received in foreground notification: \n${event.notification.jsonRepresentation().replaceAll("\\n", "\n")}";
6970
});
70-
});
71+
});
7172

72-
OneSignal.InAppMessages
73-
.setInAppMessageClickedHandler((OSInAppMessageAction action) {
74-
this.setState(() {
73+
OneSignal.InAppMessages.setInAppMessageClickedHandler(
74+
(OSInAppMessageAction action) {
75+
this.setState(() {
7576
_debugLabelString =
7677
"In App Message Clicked: \n${action.jsonRepresentation().replaceAll("\\n", "\n")}";
7778
});
@@ -114,11 +115,11 @@ class _MyAppState extends State<MyApp> with OneSignalPushSubscriptionObserver {
114115
// print("USER PROVIDED PRIVACY CONSENT: $userProvidedPrivacyConsent");
115116
}
116117

117-
void onOSPushSubscriptionChangedWithStateChanges(OSPushSubscriptionStateChanges stateChanges) {
118+
void onOSPushSubscriptionChangedWithStateChanges(
119+
OSPushSubscriptionStateChanges stateChanges) {
118120
print(stateChanges.jsonRepresentation());
119121
}
120122

121-
122123
void _handleSendTags() {
123124
print("Sending tags");
124125
OneSignal.User.addTagWithKey("test2", "val2");
@@ -151,7 +152,7 @@ class _MyAppState extends State<MyApp> with OneSignalPushSubscriptionObserver {
151152
// });
152153
}
153154

154-
void _handleSetLanguage() {
155+
void _handleSetLanguage() {
155156
if (_language == null) return;
156157
print("Setting language");
157158
OneSignal.User.setLanguage(_language!);
@@ -160,7 +161,7 @@ class _MyAppState extends State<MyApp> with OneSignalPushSubscriptionObserver {
160161
void _handleSetEmail() {
161162
if (_emailAddress == null) return;
162163
print("Setting email");
163-
164+
164165
OneSignal.User.addEmail(_emailAddress!);
165166
}
166167

@@ -183,7 +184,6 @@ class _MyAppState extends State<MyApp> with OneSignalPushSubscriptionObserver {
183184
print("Remove smsNumber");
184185

185186
OneSignal.User.removeSms(_smsNumber!);
186-
187187
}
188188

189189
void _handleConsent() {
@@ -201,8 +201,6 @@ class _MyAppState extends State<MyApp> with OneSignalPushSubscriptionObserver {
201201
OneSignal.Location.setShared(true);
202202
}
203203

204-
205-
206204
void _handleSetExternalUserId() {
207205
print("Setting external user ID");
208206
// if (_externalUserId == null) return;
@@ -291,7 +289,6 @@ class _MyAppState extends State<MyApp> with OneSignalPushSubscriptionObserver {
291289
// these triggers they will not be shown until the trigger is added back
292290
OneSignal.InAppMessages.removeTrigger("trigger_2");
293291

294-
295292
// Create a list and bulk remove triggers based on keys supplied
296293
List<String> keys = ["trigger_1", "trigger_3"];
297294
OneSignal.InAppMessages.removeTriggers(keys);
@@ -302,11 +299,9 @@ class _MyAppState extends State<MyApp> with OneSignalPushSubscriptionObserver {
302299
OneSignal.InAppMessages.paused(false);
303300
var arePaused = await OneSignal.InAppMessages.arePaused();
304301
print('Notifications paused ${arePaused}');
305-
306302
}
307303

308304
oneSignalOutcomeExamples() async {
309-
310305
OneSignal.Session.addOutcome("normal_1");
311306
OneSignal.Session.addOutcome("normal_2");
312307

@@ -318,12 +313,12 @@ class _MyAppState extends State<MyApp> with OneSignalPushSubscriptionObserver {
318313
}
319314

320315
void _handleOptIn() {
321-
OneSignal.User.pushSubscription.optIn();
322-
}
316+
OneSignal.User.pushSubscription.optIn();
317+
}
323318

324319
void _handleOptOut() {
325320
OneSignal.User.pushSubscription.optOut();
326-
}
321+
}
327322

328323
@override
329324
Widget build(BuildContext context) {
@@ -346,12 +341,12 @@ class _MyAppState extends State<MyApp> with OneSignalPushSubscriptionObserver {
346341
new OneSignalButton("Prompt for Push Permission",
347342
_handlePromptForPushPermission, !_enableConsentButton)
348343
]),
349-
// new TableRow(children: [
350-
// new OneSignalButton(
351-
// "Print Device State",
352-
// _handleGetDeviceState,
353-
// !_enableConsentButton)
354-
// ]),
344+
// new TableRow(children: [
345+
// new OneSignalButton(
346+
// "Print Device State",
347+
// _handleGetDeviceState,
348+
// !_enableConsentButton)
349+
// ]),
355350
new TableRow(children: [
356351
new TextField(
357352
textAlign: TextAlign.center,
@@ -401,17 +396,17 @@ class _MyAppState extends State<MyApp> with OneSignalPushSubscriptionObserver {
401396
)
402397
]),
403398
new TableRow(children: [
404-
new OneSignalButton(
405-
"Set SMS Number", _handleSetSMSNumber, !_enableConsentButton)
399+
new OneSignalButton("Set SMS Number", _handleSetSMSNumber,
400+
!_enableConsentButton)
406401
]),
407402
// new TableRow(children: [
408403
// new OneSignalButton("Remove SMS Number", _handleRemoveSmsNumber,
409404
// !_enableConsentButton)
410405
// ]),
411-
// new TableRow(children: [
412-
// new OneSignalButton("Provide GDPR Consent", _handleConsent,
413-
// _enableConsentButton)
414-
// ]),
406+
// new TableRow(children: [
407+
// new OneSignalButton("Provide GDPR Consent", _handleConsent,
408+
// _enableConsentButton)
409+
// ]),
415410
new TableRow(children: [
416411
new OneSignalButton("Set Location Shared",
417412
_handleSetLocationShared, !_enableConsentButton)
@@ -420,42 +415,42 @@ class _MyAppState extends State<MyApp> with OneSignalPushSubscriptionObserver {
420415
new OneSignalButton(
421416
"Remove Tag", _handleRemoveTag, !_enableConsentButton)
422417
]),
423-
// new TableRow(children: [
424-
// new OneSignalButton("Post Notification",
425-
// _handleSendNotification, !_enableConsentButton)
426-
// ]),
427-
// new TableRow(children: [
428-
// new OneSignalButton("Post Silent Notification",
429-
// _handleSendSilentNotification, !_enableConsentButton)
430-
// ]),
431-
// new TableRow(children: [
432-
// new TextField(
433-
// textAlign: TextAlign.center,
434-
// decoration: InputDecoration(
435-
// hintText: "External User ID",
436-
// labelStyle: TextStyle(
437-
// color: Color.fromARGB(255, 212, 86, 83),
438-
// )),
439-
// onChanged: (text) {
440-
// this.setState(() {
441-
// _externalUserId = text == "" ? null : text;
442-
// });
443-
// },
444-
// )
445-
// ]),
446-
// new TableRow(children: [
447-
// Container(
448-
// height: 8.0,
449-
// )
450-
// ]),
451-
// new TableRow(children: [
452-
// new OneSignalButton(
453-
// "Set External User ID", _handleSetExternalUserId, !_enableConsentButton)
454-
// ]),
455-
// new TableRow(children: [
456-
// new OneSignalButton(
457-
// "Remove External User ID", _handleRemoveExternalUserId, !_enableConsentButton)
458-
// ]),
418+
// new TableRow(children: [
419+
// new OneSignalButton("Post Notification",
420+
// _handleSendNotification, !_enableConsentButton)
421+
// ]),
422+
// new TableRow(children: [
423+
// new OneSignalButton("Post Silent Notification",
424+
// _handleSendSilentNotification, !_enableConsentButton)
425+
// ]),
426+
// new TableRow(children: [
427+
// new TextField(
428+
// textAlign: TextAlign.center,
429+
// decoration: InputDecoration(
430+
// hintText: "External User ID",
431+
// labelStyle: TextStyle(
432+
// color: Color.fromARGB(255, 212, 86, 83),
433+
// )),
434+
// onChanged: (text) {
435+
// this.setState(() {
436+
// _externalUserId = text == "" ? null : text;
437+
// });
438+
// },
439+
// )
440+
// ]),
441+
// new TableRow(children: [
442+
// Container(
443+
// height: 8.0,
444+
// )
445+
// ]),
446+
// new TableRow(children: [
447+
// new OneSignalButton(
448+
// "Set External User ID", _handleSetExternalUserId, !_enableConsentButton)
449+
// ]),
450+
// new TableRow(children: [
451+
// new OneSignalButton(
452+
// "Remove External User ID", _handleRemoveExternalUserId, !_enableConsentButton)
453+
// ]),
459454
new TableRow(children: [
460455
new TextField(
461456
textAlign: TextAlign.center,
@@ -477,8 +472,8 @@ class _MyAppState extends State<MyApp> with OneSignalPushSubscriptionObserver {
477472
)
478473
]),
479474
new TableRow(children: [
480-
new OneSignalButton(
481-
"Set Language", _handleSetLanguage, !_enableConsentButton)
475+
new OneSignalButton("Set Language", _handleSetLanguage,
476+
!_enableConsentButton)
482477
]),
483478
new TableRow(children: [
484479
new Container(
@@ -523,10 +518,10 @@ class OneSignalButtonState extends State<OneSignalButton> {
523518
new TableRow(children: [
524519
new TextButton(
525520
style: TextButton.styleFrom(
526-
foregroundColor:Colors.white,
521+
foregroundColor: Colors.white,
527522
disabledForegroundColor: Colors.white,
528523
backgroundColor: Color.fromARGB(255, 212, 86, 83),
529-
disabledBackgroundColor:Color.fromARGB(180, 212, 86, 83),
524+
disabledBackgroundColor: Color.fromARGB(180, 212, 86, 83),
530525
padding: EdgeInsets.all(8.0),
531526
),
532527
child: new Text(widget.title),

0 commit comments

Comments
 (0)