Skip to content

Commit fda094f

Browse files
authored
Merge pull request #648 from OneSignal/user_model/Location-namespace
[User Model] Location Namespace
2 parents 2edf842 + 08d3e44 commit fda094f

File tree

6 files changed

+139
-5
lines changed

6 files changed

+139
-5
lines changed

example/lib/main.dart

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ class _MyAppState extends State<MyApp> with OneSignalPushSubscriptionObserver {
222222

223223
void _handleSetLocationShared() {
224224
print("Setting location shared to true");
225-
// OneSignal.shared.setLocationShared(true);
225+
OneSignal.Location.setShared(true);
226226
}
227227

228228
void _handleRemoveTag() {
@@ -440,10 +440,10 @@ class _MyAppState extends State<MyApp> with OneSignalPushSubscriptionObserver {
440440
// new OneSignalButton("Provide GDPR Consent", _handleConsent,
441441
// _enableConsentButton)
442442
// ]),
443-
// new TableRow(children: [
444-
// new OneSignalButton("Set Location Shared",
445-
// _handleSetLocationShared, !_enableConsentButton)
446-
// ]),
443+
new TableRow(children: [
444+
new OneSignalButton("Set Location Shared",
445+
_handleSetLocationShared, !_enableConsentButton)
446+
]),
447447
new TableRow(children: [
448448
new OneSignalButton(
449449
"Remove Tag", _handleRemoveTag, !_enableConsentButton)

ios/Classes/OSFlutterLocation.h

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* Modified MIT License
3+
*
4+
* Copyright 2017 OneSignal
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* 1. The above copyright notice and this permission notice shall be included in
14+
* all copies or substantial portions of the Software.
15+
*
16+
* 2. All copies of substantial portions of the Software may only be used in connection
17+
* with services provided by OneSignal.
18+
*
19+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25+
* THE SOFTWARE.
26+
*/
27+
28+
29+
#import <Foundation/Foundation.h>
30+
#import <Flutter/Flutter.h>
31+
32+
@interface OSFlutterLocation : NSObject<FlutterPlugin>
33+
34+
@property (strong, nonatomic) FlutterMethodChannel *channel;
35+
36+
@end

ios/Classes/OSFlutterLocation.m

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/**
2+
* Modified MIT License
3+
*
4+
* Copyright 2023 OneSignal
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* 1. The above copyright notice and this permission notice shall be included in
14+
* all copies or substantial portions of the Software.
15+
*
16+
* 2. All copies of substantial portions of the Software may only be used in connection
17+
* with services provided by OneSignal.
18+
*
19+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25+
* THE SOFTWARE.
26+
*/
27+
28+
#import "OSFlutterLocation.h"
29+
#import <OneSignalFramework/OneSignalFramework.h>
30+
#import "OSFlutterCategories.h"
31+
32+
@implementation OSFlutterLocation
33+
+ (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar>*)registrar {
34+
OSFlutterLocation *instance = [OSFlutterLocation new];
35+
36+
instance.channel = [FlutterMethodChannel
37+
methodChannelWithName:@"OneSignal#location"
38+
binaryMessenger:[registrar messenger]];
39+
40+
[registrar addMethodCallDelegate:instance channel:instance.channel];
41+
}
42+
43+
- (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
44+
if ([@"OneSignal#requestPermission" isEqualToString:call.method])
45+
[self requestPermission:call withResult:result];
46+
else if ([@"OneSignal#setShared" isEqualToString:call.method])
47+
[self setLocationShared:call withResult:result];
48+
else if ([@"OneSignal#isShared" isEqualToString:call.method])
49+
result(@([OneSignal.Location isShared]));
50+
else
51+
result(FlutterMethodNotImplemented);
52+
}
53+
54+
55+
- (void)setLocationShared:(FlutterMethodCall *)call withResult:(FlutterResult)result {
56+
BOOL locationShared = [call.arguments boolValue];
57+
[OneSignal.Location setShared:locationShared];
58+
result(nil);
59+
}
60+
61+
- (void)requestPermission:(FlutterMethodCall *)call withResult:(FlutterResult)result {
62+
[OneSignal.Location requestPermission];
63+
result(nil);
64+
}
65+
66+
67+
68+
@end

ios/Classes/OneSignalPlugin.m

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#import "OSFlutterUser.h"
3232
#import "OSFlutterNotifications.h"
3333
#import "OSFlutterSession.h"
34+
#import "OSFlutterLocation.h"
3435

3536

3637
@interface OneSignalPlugin ()
@@ -74,6 +75,7 @@ + (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar>*)registrar {
7475
[OSFlutterUser registerWithRegistrar:registrar];
7576
[OSFlutterNotifications registerWithRegistrar:registrar];
7677
[OSFlutterSession registerWithRegistrar:registrar];
78+
[OSFlutterLocation registerWithRegistrar:registrar];
7779

7880
}
7981

lib/onesignal_flutter.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import 'package:onesignal_flutter/src/debug.dart';
88
import 'package:onesignal_flutter/src/user.dart';
99
import 'package:onesignal_flutter/src/notifications.dart';
1010
import 'package:onesignal_flutter/src/session.dart';
11+
import 'package:onesignal_flutter/src/location.dart';
1112

1213
export 'src/permission.dart';
1314
export 'src/defines.dart';
@@ -25,6 +26,7 @@ class OneSignal {
2526
static OneSignalUser User = new OneSignalUser();
2627
static OneSignalNotifications Notifications = new OneSignalNotifications();
2728
static OneSignalSession Session = new OneSignalSession();
29+
static OneSignalLocation Location = new OneSignalLocation();
2830

2931

3032
// private channels used to bridge to ObjC/Java

lib/src/location.dart

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import 'dart:async';
2+
import 'dart:io' show Platform;
3+
import 'package:flutter/services.dart';
4+
import 'package:onesignal_flutter/src/defines.dart';
5+
6+
class OneSignalLocation {
7+
8+
// private channels used to bridge to ObjC/Java
9+
MethodChannel _channel = const MethodChannel('OneSignal#location');
10+
11+
/// Allows you to prompt the user for permission to use location services
12+
Future<void> requestPermission() async {
13+
return await _channel.invokeMethod("OneSignal#requestPermission");
14+
}
15+
16+
/// Allows you to determine if the user's location data is shared with OneSignal.
17+
/// This allows you to do things like geofenced notifications, etc.
18+
Future<void> setShared(bool shared) async {
19+
return await _channel.invokeMethod("OneSignal#setShared", shared);
20+
}
21+
22+
/// Set whether location is currently shared with OneSignal.
23+
Future<bool> isShared() async {
24+
return await _channel.invokeMethod("OneSignal#isShared");
25+
}
26+
}

0 commit comments

Comments
 (0)