Skip to content

Commit 5f257f2

Browse files
authored
Merge pull request #646 from OneSignal/user_model/Notification-and-pushSubscription-namespaces
Notification and pushSubscription namespaces
2 parents 3a516a0 + 8073e92 commit 5f257f2

20 files changed

+1041
-303
lines changed

example/ios/Podfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ target 'Runner' do
3232
end
3333

3434
target 'OneSignalNotificationServiceExtension' do
35-
pod 'OneSignalXCFramework', '5.0.0-alpha-02'
35+
pod 'OneSignalXCFramework', '5.0.0-beta-01'
3636
end
3737

3838
post_install do |installer|

example/ios/Podfile.lock

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,28 @@
11
PODS:
22
- Flutter (1.0.0)
3-
- onesignal_flutter (5.0.0-alpha-02):
3+
- onesignal_flutter (5.0.0-beta-01):
44
- Flutter
5-
- OneSignalXCFramework (= 5.0.0-alpha-02)
6-
- OneSignalXCFramework (5.0.0-alpha-02):
7-
- OneSignalXCFramework/OneSignalCore (= 5.0.0-alpha-02)
8-
- OneSignalXCFramework/OneSignalExtension (= 5.0.0-alpha-02)
9-
- OneSignalXCFramework/OneSignalNotifications (= 5.0.0-alpha-02)
10-
- OneSignalXCFramework/OneSignalOSCore (= 5.0.0-alpha-02)
11-
- OneSignalXCFramework/OneSignalOutcomes (= 5.0.0-alpha-02)
12-
- OneSignalXCFramework/OneSignalUser (= 5.0.0-alpha-02)
13-
- OneSignalXCFramework/OneSignalCore (5.0.0-alpha-02)
14-
- OneSignalXCFramework/OneSignalExtension (5.0.0-alpha-02):
5+
- OneSignalXCFramework (= 5.0.0-beta-01)
6+
- OneSignalXCFramework (5.0.0-beta-01):
7+
- OneSignalXCFramework/OneSignalCore (= 5.0.0-beta-01)
8+
- OneSignalXCFramework/OneSignalExtension (= 5.0.0-beta-01)
9+
- OneSignalXCFramework/OneSignalNotifications (= 5.0.0-beta-01)
10+
- OneSignalXCFramework/OneSignalOSCore (= 5.0.0-beta-01)
11+
- OneSignalXCFramework/OneSignalOutcomes (= 5.0.0-beta-01)
12+
- OneSignalXCFramework/OneSignalUser (= 5.0.0-beta-01)
13+
- OneSignalXCFramework/OneSignalCore (5.0.0-beta-01)
14+
- OneSignalXCFramework/OneSignalExtension (5.0.0-beta-01):
1515
- OneSignalXCFramework/OneSignalCore
1616
- OneSignalXCFramework/OneSignalOutcomes
17-
- OneSignalXCFramework/OneSignalNotifications (5.0.0-alpha-02):
17+
- OneSignalXCFramework/OneSignalNotifications (5.0.0-beta-01):
1818
- OneSignalXCFramework/OneSignalCore
1919
- OneSignalXCFramework/OneSignalExtension
2020
- OneSignalXCFramework/OneSignalOutcomes
21-
- OneSignalXCFramework/OneSignalOSCore (5.0.0-alpha-02):
21+
- OneSignalXCFramework/OneSignalOSCore (5.0.0-beta-01):
2222
- OneSignalXCFramework/OneSignalCore
23-
- OneSignalXCFramework/OneSignalOutcomes (5.0.0-alpha-02):
23+
- OneSignalXCFramework/OneSignalOutcomes (5.0.0-beta-01):
2424
- OneSignalXCFramework/OneSignalCore
25-
- OneSignalXCFramework/OneSignalUser (5.0.0-alpha-02):
25+
- OneSignalXCFramework/OneSignalUser (5.0.0-beta-01):
2626
- OneSignalXCFramework/OneSignalCore
2727
- OneSignalXCFramework/OneSignalNotifications
2828
- OneSignalXCFramework/OneSignalOSCore
@@ -31,7 +31,7 @@ PODS:
3131
DEPENDENCIES:
3232
- Flutter (from `Flutter`)
3333
- onesignal_flutter (from `.symlinks/plugins/onesignal_flutter/ios`)
34-
- OneSignalXCFramework (= 5.0.0-alpha-02)
34+
- OneSignalXCFramework (= 5.0.0-beta-01)
3535

3636
SPEC REPOS:
3737
trunk:
@@ -45,9 +45,9 @@ EXTERNAL SOURCES:
4545

4646
SPEC CHECKSUMS:
4747
Flutter: f04841e97a9d0b0a8025694d0796dd46242b2854
48-
onesignal_flutter: c4fe7cfb99196ccc89ecaef890bdd66abf00f5d7
49-
OneSignalXCFramework: 8eec9904167f8c97c23c441709b648f353d22a41
48+
onesignal_flutter: e369a8100290b3680412e27e91255916dd23ca22
49+
OneSignalXCFramework: 7d3eb90c3e78908b0d72aa88600bfdab694460ec
5050

51-
PODFILE CHECKSUM: f0497bd2adc984c2ee14f5ec68f1959c4d6f7a55
51+
PODFILE CHECKSUM: 004b8b86fe167483a0c84378eebc1addff2ac31b
5252

5353
COCOAPODS: 1.11.3

example/lib/main.dart

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class MyApp extends StatefulWidget {
1111
_MyAppState createState() => new _MyAppState();
1212
}
1313

14-
class _MyAppState extends State<MyApp> {
14+
class _MyAppState extends State<MyApp> with OneSignalPushSubscriptionObserver {
1515
String _debugLabelString = "";
1616
String? _emailAddress;
1717
String? _smsNumber;
@@ -32,13 +32,15 @@ class _MyAppState extends State<MyApp> {
3232
Future<void> initPlatformState() async {
3333
if (!mounted) return;
3434

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

37-
OneSignal.Debug.setVisualLevel(OSLogLevel.verbose);
37+
OneSignal.Debug.setAlertLevel(OSLogLevel.none);
3838

3939
// NOTE: Replace with your own app ID from https://www.onesignal.com
4040
OneSignal.shared.initialize("77e32082-ea27-42e3-a898-c72e141824ef");
4141

42+
OneSignal.User.pushSubscription.addObserver(this);
43+
4244
// OneSignal.shared.setRequiresUserPrivacyConsent(_requireConsent);
4345

4446
// OneSignal.shared
@@ -126,6 +128,10 @@ class _MyAppState extends State<MyApp> {
126128
// print("USER PROVIDED PRIVACY CONSENT: $userProvidedPrivacyConsent");
127129
}
128130

131+
void onOSPushSubscriptionChangedWithStateChanges(OSPushSubscriptionStateChanges stateChanges) {
132+
print(stateChanges.jsonRepresentation());
133+
}
134+
129135
void _handleGetTags() {
130136
// OneSignal.shared.getTags().then((tags) {
131137
// if (tags == null) return;
@@ -183,29 +189,22 @@ class _MyAppState extends State<MyApp> {
183189
if (_emailAddress == null) return;
184190
print("Remove email");
185191

186-
OneSignal.User.removeEmail(_emailAddress!).then((response) {
187-
print("Successfully remove email with response $response");
188-
}).catchError((error) {
189-
print("Failed to remove email: $error");
190-
});
192+
OneSignal.User.removeEmail(_emailAddress!);
191193
}
192194

193195
void _handleSetSMSNumber() {
194196
if (_smsNumber == null) return;
195197
print("Setting SMS Number");
196198

197-
OneSignal.User.addSmsNumber(_smsNumber!);
199+
OneSignal.User.addSms(_smsNumber!);
198200
}
199201

200202
void _handleRemoveSMSNumber() {
201203
if (_smsNumber == null) return;
202204
print("Remove smsNumber");
203205

204-
OneSignal.User.removeSmsNumber(_smsNumber!).then((response) {
205-
print("Successfully remove smsNumber with response $response");
206-
}).catchError((error) {
207-
print("Failed to remove SMSNumber: $error");
208-
});
206+
OneSignal.User.removeSms(_smsNumber!);
207+
209208
}
210209

211210
void _handleConsent() {
@@ -359,6 +358,14 @@ class _MyAppState extends State<MyApp> {
359358
// print(outcomeEvent.jsonRepresentation());
360359
}
361360

361+
void _handleOptIn() {
362+
OneSignal.User.pushSubscription.optIn();
363+
}
364+
365+
void _handleOptOut() {
366+
OneSignal.User.pushSubscription.optOut();
367+
}
368+
362369
@override
363370
Widget build(BuildContext context) {
364371
return new MaterialApp(
@@ -520,6 +527,14 @@ class _MyAppState extends State<MyApp> {
520527
alignment: Alignment.center,
521528
)
522529
]),
530+
new TableRow(children: [
531+
new OneSignalButton(
532+
"Opt In", _handleOptIn, !_enableConsentButton)
533+
]),
534+
new TableRow(children: [
535+
new OneSignalButton(
536+
"Opt Out", _handleOptOut, !_enableConsentButton)
537+
]),
523538
],
524539
),
525540
),

ios/Classes/OSFlutterDebug.m

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ + (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar>*)registrar {
4343
- (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
4444
if ([@"OneSignal#setLogLevel" isEqualToString:call.method])
4545
[self setLogLevel:call];
46-
else if ([@"OneSignal#setVisualLevel" isEqualToString:call.method])
47-
[self setVisualLevel:call];
46+
else if ([@"OneSignal#setAlertLevel" isEqualToString:call.method])
47+
[self setAlertLevel:call];
4848
else
4949
result(FlutterMethodNotImplemented);
5050
}
@@ -54,9 +54,9 @@ - (void)setLogLevel:(FlutterMethodCall *)call {
5454
[OneSignal.Debug setLogLevel:logLevel];
5555
}
5656

57-
- (void)setVisualLevel:(FlutterMethodCall *)call {
57+
- (void)setAlertLevel:(FlutterMethodCall *)call {
5858
ONE_S_LOG_LEVEL visualLogLevel = (ONE_S_LOG_LEVEL)[call.arguments[@"visualLevel"] intValue];
59-
[OneSignal.Debug setVisualLevel:visualLogLevel];
59+
[OneSignal.Debug setAlertLevel:visualLogLevel];
6060
}
6161

6262
@end

ios/Classes/OSFlutterNotifications.h

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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+
#import <OneSignalNotifications/OneSignalNotifications.h>
32+
33+
@interface OSFlutterNotifications : NSObject<FlutterPlugin, OSPermissionObserver>
34+
35+
@property (strong, nonatomic) FlutterMethodChannel *channel;
36+
37+
@property (atomic) BOOL hasSetNotificationWillShowInForegroundHandler;
38+
@property (strong, nonatomic) NSMutableDictionary* notificationCompletionCache;
39+
@property (strong, nonatomic) NSMutableDictionary* receivedNotificationCache;
40+
41+
@end

ios/Classes/OSFlutterNotifications.m

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
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 "OSFlutterNotifications.h"
29+
#import <OneSignalFramework/OneSignalFramework.h>
30+
#import <OneSignalNotifications/OneSignalNotifications.h>
31+
#import "OSFlutterCategories.h"
32+
33+
@implementation OSFlutterNotifications
34+
35+
+ (instancetype)sharedInstance {
36+
static OSFlutterNotifications *sharedInstance = nil;
37+
static dispatch_once_t onceToken;
38+
dispatch_once(&onceToken, ^{
39+
sharedInstance = [OSFlutterNotifications new];
40+
sharedInstance.hasSetNotificationWillShowInForegroundHandler = false;
41+
sharedInstance.receivedNotificationCache = [NSMutableDictionary new];
42+
sharedInstance.notificationCompletionCache = [NSMutableDictionary new];
43+
});
44+
return sharedInstance;
45+
}
46+
47+
+ (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar>*)registrar {
48+
OSFlutterNotifications *instance = [OSFlutterNotifications new];
49+
50+
instance.channel = [FlutterMethodChannel
51+
methodChannelWithName:@"OneSignal#notifications"
52+
binaryMessenger:[registrar messenger]];
53+
54+
[registrar addMethodCallDelegate:instance channel:instance.channel];
55+
56+
[OneSignal.Notifications setNotificationWillShowInForegroundHandler:^(OSNotification *notification, OSNotificationDisplayResponse completion) {
57+
[OSFlutterNotifications.sharedInstance handleNotificationWillShowInForeground:notification completion:completion];
58+
}];
59+
}
60+
61+
- (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
62+
if ([@"OneSignal#permission" isEqualToString:call.method])
63+
result(@([OneSignal.Notifications permission]));
64+
else if ([@"OneSignal#canRequest" isEqualToString:call.method])
65+
result(@([OneSignal.Notifications canRequestPermission]));
66+
else if ([@"OneSignal#clearAll" isEqualToString:call.method])
67+
[self clearAll:call withResult:result];
68+
else if ([@"OneSignal#requestPermission" isEqualToString:call.method])
69+
[self requestPermission:call withResult:result];
70+
else if ([@"OneSignal#registerForProvisionalAuthorization" isEqualToString:call.method])
71+
[self registerForProvisionalAuthorization:call withResult:result];
72+
else if ([@"OneSignal#addPermissionObserver" isEqualToString:call.method])
73+
[self addPermissionObserver:call withResult:result];
74+
else if ([@"OneSignal#removePermissionObserver" isEqualToString:call.method])
75+
[self removePermissionObserver:call withResult:result];
76+
else if ([@"OneSignal#initNotificationWillShowInForegroundHandlerParams" isEqualToString:call.method])
77+
[self initNotificationWillShowInForegroundHandlerParams];
78+
else if ([@"OneSignal#initNotificationOpenedHandlerParams" isEqualToString:call.method])
79+
[self initNotificationOpenedHandlerParams];
80+
else
81+
result(FlutterMethodNotImplemented);
82+
}
83+
84+
- (void)clearAll:(FlutterMethodCall *)call withResult:(FlutterResult)result {
85+
[OneSignal.Notifications clearAll];
86+
result(nil);
87+
}
88+
89+
- (void)requestPermission:(FlutterMethodCall *)call withResult:(FlutterResult)result {
90+
BOOL fallbackToSettings = [call.arguments[@"fallbackToSettings"] boolValue];
91+
92+
[OneSignal.Notifications requestPermission:^(BOOL accepted) {
93+
result(@(accepted));
94+
} fallbackToSettings:fallbackToSettings];
95+
}
96+
97+
- (void)registerForProvisionalAuthorization:(FlutterMethodCall *)call withResult:(FlutterResult)result {
98+
[OneSignal.Notifications registerForProvisionalAuthorization:^(BOOL accepted) {
99+
result(@(accepted));
100+
}];
101+
}
102+
103+
104+
105+
- (void)onOSPermissionChanged:(OSPermissionState*)state {
106+
[self.channel invokeMethod:@"OneSignal#OSPermissionChanged" arguments:state.jsonRepresentation];
107+
}
108+
109+
- (void)addPermissionObserver:(FlutterMethodCall *)call withResult:(FlutterResult)result {
110+
[OneSignal.Notifications addPermissionObserver:self];
111+
result(nil);
112+
}
113+
114+
// TODO: possibly don't need
115+
- (void)removePermissionObserver:(FlutterMethodCall *)call withResult:(FlutterResult)result {
116+
[OneSignal.Notifications removePermissionObserver:self];
117+
result(nil);
118+
}
119+
120+
#pragma mark Received in Foreground Notification
121+
122+
- (void)initNotificationWillShowInForegroundHandlerParams {
123+
self.hasSetNotificationWillShowInForegroundHandler = YES;
124+
}
125+
126+
- (void)handleNotificationWillShowInForeground:(OSNotification *)notification completion:(OSNotificationDisplayResponse)completion {
127+
if (!self.hasSetNotificationWillShowInForegroundHandler) {
128+
completion(notification);
129+
return;
130+
}
131+
132+
self.receivedNotificationCache[notification.notificationId] = notification;
133+
self.notificationCompletionCache[notification.notificationId] = completion;
134+
[self.channel invokeMethod:@"OneSignal#handleNotificationWillShowInForeground" arguments:notification.toJson];
135+
}
136+
137+
#pragma mark Opened Notification
138+
139+
- (void)initNotificationOpenedHandlerParams {
140+
[OneSignal.Notifications setNotificationOpenedHandler:^(OSNotificationOpenedResult * _Nonnull result) {
141+
[OSFlutterNotifications.sharedInstance handleNotificationOpened:result];
142+
}];
143+
}
144+
145+
- (void)handleNotificationOpened:(OSNotificationOpenedResult *)result {
146+
[self.channel invokeMethod:@"OneSignal#handleOpenedNotification" arguments:result.toJson];
147+
}
148+
149+
150+
@end

0 commit comments

Comments
 (0)