Skip to content

Commit 31c6a4e

Browse files
authored
Merge pull request #642 from OneSignal/user_model/OneSignal_base_namespace
Adding methods to the OneSignal static object
2 parents 9fc14d9 + 50e53d8 commit 31c6a4e

File tree

4 files changed

+169
-13
lines changed

4 files changed

+169
-13
lines changed

ios/Classes/OSFlutterDebug.m

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,13 @@ - (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
5050
}
5151

5252
- (void)setLogLevel:(FlutterMethodCall *)call {
53-
[OneSignal.Debug setLogLevel:call.arguments[@"logLevel"]];
53+
ONE_S_LOG_LEVEL logLevel = (ONE_S_LOG_LEVEL)[call.arguments[@"logLevel"] intValue];
54+
[OneSignal.Debug setLogLevel:logLevel];
5455
}
5556

5657
- (void)setVisualLevel:(FlutterMethodCall *)call {
57-
[OneSignal.Debug setVisualLevel:call.arguments[@"visualLevel"]];
58+
ONE_S_LOG_LEVEL visualLogLevel = (ONE_S_LOG_LEVEL)[call.arguments[@"visualLevel"] intValue];
59+
[OneSignal.Debug setVisualLevel:visualLogLevel];
5860
}
5961

6062
@end

ios/Classes/OneSignalPlugin.m

Lines changed: 73 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,28 @@ + (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar>*)registrar {
7575
- (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
7676
if ([@"OneSignal#initialize" isEqualToString:call.method])
7777
[self initialize:call];
78-
else
78+
else if ([@"OneSignal#login" isEqualToString:call.method])
79+
result(@(OneSignal.login));
80+
else if ([@"OneSignal#getPrivacyConsent" isEqualToString:call.method])
81+
result(@(OneSignal.getPrivacyConsent));
82+
else if ([@"OneSignal#setPrivacyConsent" isEqualToString:call.method])
83+
[self getPrivacyConsent:call];
84+
else if ([@"OneSignal#requiresPrivacyConsent" isEqualToString:call.method])
85+
result(@(OneSignal.requiresPrivacyConsent));
86+
else if ([@"OneSignal#setRequiresPrivacyConsent" isEqualToString:call.method])
87+
[self setRequiresPrivacyConsent:call];
88+
else if ([@"OneSignal#setLaunchURLsInApp" isEqualToString:call.method])
89+
[self setLaunchURLsInApp:call];
90+
else if ([@"OneSignal#enterLiveActivity" isEqualToString:call.method])
91+
[self enterLiveActivity:call withResult:result];
92+
else if ([@"OneSignal#exitLiveActivity" isEqualToString:call.method])
93+
[self exitLiveActivity:call withResult:result];
94+
else
7995
result(FlutterMethodNotImplemented);
8096
}
8197

98+
#pragma mark Init
99+
82100
- (void)initialize:(FlutterMethodCall *)call {
83101

84102
[OneSignal initialize:call.arguments[@"appId"] withLaunchOptions:nil];
@@ -93,4 +111,58 @@ - (void)initialize:(FlutterMethodCall *)call {
93111
// result(nil);
94112
}
95113

114+
#pragma mark Login Logout
115+
116+
- (void)login:(FlutterMethodCall *)call {
117+
[OneSignal login:call.arguments[@"externalId"]];
118+
}
119+
120+
- (void)logout:(FlutterMethodCall *)call {
121+
[OneSignal logout];
122+
}
123+
124+
#pragma mark Privacy Consent
125+
126+
- (void)setPrivacyConsent:(FlutterMethodCall *)call {
127+
BOOL granted = [call.arguments[@"granted"] boolValue];
128+
[OneSignal setPrivacyConsent:granted];
129+
}
130+
131+
- (void)setRequiresPrivacyConsent:(FlutterMethodCall *)call {
132+
BOOL required = [call.arguments[@"required"] boolValue];
133+
[OneSignal setRequiresPrivacyConsent:required];
134+
}
135+
136+
#pragma mark Launch Urls In App
137+
138+
- (void)setLaunchURLsInApp:(FlutterMethodCall *)call {
139+
BOOL launchUrlsInApp = [call.arguments[@"launchUrlsInApp"] boolValue];
140+
[OneSignal setLaunchURLsInApp:launchUrlsInApp]
141+
}
142+
143+
#pragma mark Live Activity
144+
145+
- (void)enterLiveActivity:(FlutterMethodCall *)call withResult:(FlutterResult)result {
146+
NSString *activityId = call.arguments[@"activityId"];
147+
NSString *token = call.arguments[@"token"];
148+
149+
[OneSignal enterLiveActivity:activityId withToken:token withSuccess:^(NSDictionary *results) {
150+
result(results);
151+
} withFailure:^(NSError *error) {
152+
[OneSignal onesignalLog:ONE_S_LL_VERBOSE message:[NSString stringWithFormat:@"enterLiveActivity Failure with error: %@", error]];
153+
result(error.flutterError);
154+
}];
155+
}
156+
157+
- (void)exitLiveActivity:(FlutterMethodCall *)call withResult:(FlutterResult)result {
158+
NSString *activityId = call.arguments[@"activityId"];
159+
160+
[OneSignal exitLiveActivity:activityId withSuccess:^(NSDictionary *results) {
161+
result(results);
162+
} withFailure:^(NSError *error) {
163+
[OneSignal onesignalLog:ONE_S_LL_VERBOSE message:[NSString stringWithFormat:@"exitLiveActivity Failure with error: %@", error]];
164+
result(error.flutterError);
165+
}];
166+
}
167+
96168
@end

lib/onesignal_flutter.dart

Lines changed: 90 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,94 @@ class OneSignal {
3636
_channel.invokeMethod(
3737
'OneSignal#initialize', {'appId': appId});
3838
}
39-
// constructor method
40-
// OneSignal() {
41-
// this._channel.setMethodCallHandler(_handleMethod);
42-
// }
43-
// // Private function that gets called by ObjC/Java
44-
// Future<Null> _handleMethod(MethodCall call) async {
45-
// return null;
46-
// }
39+
40+
/// Login to OneSignal under the user identified by the [externalId] provided.
41+
///
42+
/// The act of logging a user into the OneSignal SDK will switch the
43+
/// [user] context to that specific user.
44+
void login(String externalId) {
45+
_channel.invokeMethod(
46+
'OneSignal#login', {'externalId': externalId});
47+
}
48+
49+
/// Logout the user previously logged in via [login]. The [user] property now
50+
///
51+
/// references a new device-scoped user. A device-scoped user has no user identity
52+
/// that can later be retrieved, except through this device as long as the app
53+
/// remains installed and the app data is not cleared.
54+
void logout() {
55+
_channel.invokeMethod(
56+
'OneSignal#logout');
57+
}
58+
59+
/// Indicates whether privacy consent has been granted.
60+
///
61+
/// This field is only relevant when the application has
62+
/// opted into data privacy protections. See [requiresPrivacyConsent].
63+
Future<bool> getPrivacyConsent() async {
64+
var val =
65+
await _channel.invokeMethod("OneSignal#getPrivacyConsent");
66+
67+
return val as bool;
68+
}
69+
70+
/// Sets the whether or not privacy consent has been [granted]
71+
///
72+
/// This field is only relevant when the application has
73+
/// opted into data privacy protections. See [requiresPrivacyConsent].
74+
Future<void> setPrivacyConsent(bool granted) async {
75+
await _channel
76+
.invokeMethod("OneSignal#setPrivacyConsent", {'granted': granted});
77+
}
78+
79+
/// A boolean value indicating if the OneSignal SDK is waiting for the
80+
/// user's consent before it can initialize (if you set the app to
81+
/// require the user's consent)
82+
Future<bool> requiresPrivacyConsent() async {
83+
var val =
84+
await _channel.invokeMethod("OneSignal#requiresPrivacyConsent");
85+
86+
return val as bool;
87+
}
88+
89+
/// Allows you to completely disable the SDK until your app calls the
90+
/// OneSignal.setPrivacyConsent(true) function. This is useful if you want
91+
/// to show a Terms and Conditions or privacy popup for GDPR.
92+
Future<void> setRequiresPrivacyConsent(bool require) async {
93+
await _channel.invokeMethod(
94+
"OneSignal#setRequiresPrivacyConsent", {'required': require});
95+
}
96+
97+
/// This method can be used to set if launch URLs should be opened in safari or
98+
/// within the application. Set to true to launch all notifications with a URL
99+
/// in the app instead of the default web browser. Make sure to call setLaunchURLsInApp
100+
/// before the initialize call.
101+
void setLaunchURLsInApp(bool launchUrlsInApp) {
102+
_channel.invokeMethod(
103+
'OneSignal#setLaunchURLsInApp', {'launchUrlsInApp': launchUrlsInApp});
104+
}
105+
106+
/// Only applies to iOS
107+
/// Associates a temporary push token with an Activity ID on the OneSignal server.
108+
Future<void> enterLiveActivity(String activityId, String token) async {
109+
if (Platform.isIOS) {
110+
await _channel.invokeMethod("OneSignal#enterLiveActivity", {'activityId': activityId, 'token': token});
111+
} else {
112+
_onesignalLog(OSLogLevel.info,
113+
"enterLiveActivity: this function is not supported on Android");
114+
}
115+
}
116+
117+
/// Only applies to iOS
118+
/// Deletes activityId associated temporary push token on the OneSignal server.
119+
Future<void> exitLiveActivity(String activityId) async {
120+
if (Platform.isIOS) {
121+
await _channel.invokeMethod("OneSignal#exitLiveActivity",
122+
{'activityId': activityId});
123+
} else {
124+
_onesignalLog(OSLogLevel.info,
125+
"exitLiveActivity: this function is not supported on Android");
126+
}
127+
}
128+
47129
}

lib/src/debug.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class OneSignalDebug {
1414
/// how verbose logs in the console/logcat are
1515
void setLogLevel(OSLogLevel logLevel) {
1616
_channel.invokeMethod("OneSignal#setLogLevel",
17-
{'console': logLevel.index});
17+
{'logLevel': logLevel.index});
1818
}
1919

2020
/// Sets the log level for the SDK.
@@ -23,6 +23,6 @@ class OneSignalDebug {
2323
/// if the SDK will show alerts for each logged message
2424
void setVisualLevel( OSLogLevel visualLevel) {
2525
_channel.invokeMethod("OneSignal#setVisualLevel",
26-
{'visual': visualLevel.index});
26+
{'visualLevel': visualLevel.index});
2727
}
2828
}

0 commit comments

Comments
 (0)