Skip to content

Commit 8073e92

Browse files
committed
Adding methods to the Notification namespace and the permission oberserver
1 parent 39e60a2 commit 8073e92

12 files changed

+172
-201
lines changed

example/lib/main.dart

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

3535
OneSignal.Debug.setLogLevel(OSLogLevel.debug);
3636

37-
OneSignal.Debug.setVisualLevel(OSLogLevel.none);
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");
@@ -189,29 +189,22 @@ class _MyAppState extends State<MyApp> with OneSignalPushSubscriptionObserver {
189189
if (_emailAddress == null) return;
190190
print("Remove email");
191191

192-
OneSignal.User.removeEmail(_emailAddress!).then((response) {
193-
print("Successfully remove email with response $response");
194-
}).catchError((error) {
195-
print("Failed to remove email: $error");
196-
});
192+
OneSignal.User.removeEmail(_emailAddress!);
197193
}
198194

199195
void _handleSetSMSNumber() {
200196
if (_smsNumber == null) return;
201197
print("Setting SMS Number");
202198

203-
OneSignal.User.addSmsNumber(_smsNumber!);
199+
OneSignal.User.addSms(_smsNumber!);
204200
}
205201

206202
void _handleRemoveSMSNumber() {
207203
if (_smsNumber == null) return;
208204
print("Remove smsNumber");
209205

210-
OneSignal.User.removeSmsNumber(_smsNumber!).then((response) {
211-
print("Successfully remove smsNumber with response $response");
212-
}).catchError((error) {
213-
print("Failed to remove SMSNumber: $error");
214-
});
206+
OneSignal.User.removeSms(_smsNumber!);
207+
215208
}
216209

217210
void _handleConsent() {

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: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,8 @@
3434

3535
@property (strong, nonatomic) FlutterMethodChannel *channel;
3636

37+
@property (atomic) BOOL hasSetNotificationWillShowInForegroundHandler;
38+
@property (strong, nonatomic) NSMutableDictionary* notificationCompletionCache;
39+
@property (strong, nonatomic) NSMutableDictionary* receivedNotificationCache;
40+
3741
@end

ios/Classes/OSFlutterNotifications.m

Lines changed: 56 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,19 @@
3131
#import "OSFlutterCategories.h"
3232

3333
@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+
3447
+ (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar>*)registrar {
3548
OSFlutterNotifications *instance = [OSFlutterNotifications new];
3649

@@ -39,9 +52,10 @@ + (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar>*)registrar {
3952
binaryMessenger:[registrar messenger]];
4053

4154
[registrar addMethodCallDelegate:instance channel:instance.channel];
42-
// NSLog(@"OSFlutterPushSubscription initialized");
43-
44-
// [OneSignal.Notifications addPermissionObserver:self];
55+
56+
[OneSignal.Notifications setNotificationWillShowInForegroundHandler:^(OSNotification *notification, OSNotificationDisplayResponse completion) {
57+
[OSFlutterNotifications.sharedInstance handleNotificationWillShowInForeground:notification completion:completion];
58+
}];
4559
}
4660

4761
- (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
@@ -55,6 +69,12 @@ - (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
5569
[self requestPermission:call withResult:result];
5670
else if ([@"OneSignal#registerForProvisionalAuthorization" isEqualToString:call.method])
5771
[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];
5878
else if ([@"OneSignal#initNotificationOpenedHandlerParams" isEqualToString:call.method])
5979
[self initNotificationOpenedHandlerParams];
6080
else
@@ -80,24 +100,48 @@ - (void)registerForProvisionalAuthorization:(FlutterMethodCall *)call withResult
80100
}];
81101
}
82102

103+
104+
83105
- (void)onOSPermissionChanged:(OSPermissionState*)state {
84-
// Example of detecting the curret permission
85-
if (state.reachable == true) {
86-
NSLog(@"Device has permission to display notifications");
87-
} else {
88-
NSLog(@"Device does not have permission to display notifications");
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;
89130
}
90-
// prints out all properties
91-
NSLog(@"PermissionState:\n%@", state);
131+
132+
self.receivedNotificationCache[notification.notificationId] = notification;
133+
self.notificationCompletionCache[notification.notificationId] = completion;
134+
[self.channel invokeMethod:@"OneSignal#handleNotificationWillShowInForeground" arguments:notification.toJson];
92135
}
93136

137+
#pragma mark Opened Notification
138+
94139
- (void)initNotificationOpenedHandlerParams {
95140
[OneSignal.Notifications setNotificationOpenedHandler:^(OSNotificationOpenedResult * _Nonnull result) {
96-
[self handleNotificationOpened:result];
141+
[OSFlutterNotifications.sharedInstance handleNotificationOpened:result];
97142
}];
98143
}
99144

100-
#pragma mark Opened Notification Handlers
101145
- (void)handleNotificationOpened:(OSNotificationOpenedResult *)result {
102146
[self.channel invokeMethod:@"OneSignal#handleOpenedNotification" arguments:result.toJson];
103147
}

ios/Classes/OSFlutterPushSubscription.m

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#import "OSFlutterCategories.h"
3232

3333
@implementation OSFlutterPushSubscription
34+
3435
+ (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar>*)registrar {
3536
OSFlutterPushSubscription *instance = [OSFlutterPushSubscription new];
3637

@@ -72,7 +73,6 @@ - (void)optOut:(FlutterMethodCall *)call withResult:(FlutterResult)result {
7273

7374
- (void)addObserver:(FlutterMethodCall *)call withResult:(FlutterResult)result {
7475
[OneSignal.User.pushSubscription addObserver:self];
75-
NSLog(@"addObserver");
7676
result(nil);
7777
}
7878

@@ -83,9 +83,7 @@ - (void)removeObserver:(FlutterMethodCall *)call withResult:(FlutterResult)resul
8383
}
8484

8585
- (void)onOSPushSubscriptionChangedWithStateChanges:(OSPushSubscriptionStateChanges*)stateChanges {
86-
[self.channel invokeMethod:@"OneSignal#pushSubscriptionChanged" arguments:stateChanges.toDictionary];
87-
NSLog(@"onOSPushSubscriptionChangedWithStateChanges");
88-
// NSLog(@"PushSubscriptionStateChanges:\n%@", stateChanges);
86+
[self.channel invokeMethod:@"OneSignal#pushSubscriptionChanged" arguments:stateChanges.jsonRepresentation];
8987
}
9088

9189
@end

ios/Classes/OSFlutterUser.m

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,10 @@ - (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
6767
[self addEmail:call];
6868
else if ([@"OneSignal#removeEmail" isEqualToString:call.method])
6969
[self removeEmail:call withResult:result];
70-
else if ([@"OneSignal#addSmsNumber" isEqualToString:call.method])
71-
[self addSmsNumber:call];
72-
else if ([@"OneSignal#removeSmsNumber" isEqualToString:call.method])
73-
[self removeSmsNumber:call withResult:result];
70+
else if ([@"OneSignal#addSms" isEqualToString:call.method])
71+
[self addSms:call];
72+
else if ([@"OneSignal#removeSms" isEqualToString:call.method])
73+
[self removeSms:call withResult:result];
7474

7575
else
7676
result(FlutterMethodNotImplemented);
@@ -138,17 +138,19 @@ - (void)addEmail:(FlutterMethodCall *)call {
138138

139139
- (void)removeEmail:(FlutterMethodCall *)call withResult:(FlutterResult)result {
140140
NSString *email = call.arguments[@"email"];
141-
result(@([OneSignal.User removeEmail:email]));
141+
[OneSignal.User removeEmail:email];
142+
result(nil);
142143
}
143144

144-
- (void)addSmsNumber:(FlutterMethodCall *)call {
145+
- (void)addSms:(FlutterMethodCall *)call {
145146
NSString *smsNumber = call.arguments;
146-
[OneSignal.User addSmsNumber:smsNumber];
147+
[OneSignal.User addSms:smsNumber];
147148
}
148149

149-
- (void)removeSmsNumber:(FlutterMethodCall *)call withResult:(FlutterResult)result {
150+
- (void)removeSms:(FlutterMethodCall *)call withResult:(FlutterResult)result {
150151
NSString *smsNumber = call.arguments[@"smsNumber"];
151-
result(@([OneSignal.User removeSmsNumber:smsNumber]));
152+
[OneSignal.User removeSms:smsNumber];
153+
result(nil);
152154
}
153155

154156

lib/src/debug.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ class OneSignalDebug {
2121
///
2222
/// The parameter [visualLevel] controls
2323
/// if the SDK will show alerts for each logged message
24-
void setVisualLevel( OSLogLevel visualLevel) {
25-
_channel.invokeMethod("OneSignal#setVisualLevel",
24+
void setAlertLevel( OSLogLevel visualLevel) {
25+
_channel.invokeMethod("OneSignal#setAlertLevel",
2626
{'visualLevel': visualLevel.index});
2727
}
2828
}

lib/src/notification.dart

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -381,13 +381,15 @@ class OSAndroidBackgroundImageLayout extends JSONStringRepresentable {
381381
}
382382
}
383383

384-
// class OSNotificationReceivedEvent extends JSONStringRepresentable {
384+
class OSNotificationReceivedEvent extends JSONStringRepresentable {
385385

386-
// late OSNotification notification;
386+
late OSNotification notification;
387387

388-
// OSNotificationReceivedEvent(Map<String, dynamic> json) {
389-
// notification = OSNotification(json);
390-
// }
388+
OSNotificationReceivedEvent(Map<String, dynamic> json) {
389+
notification = OSNotification(json);
390+
}
391+
392+
//TODO: remove
391393

392394
// void complete(OSNotification? notification) {
393395
// print('OSNotificationReceivedEvent complete with notification: $notification');
@@ -398,9 +400,9 @@ class OSAndroidBackgroundImageLayout extends JSONStringRepresentable {
398400
// }
399401
// }
400402

401-
// String jsonRepresentation() {
402-
// return convertToJsonString({
403-
// 'notification': this.notification.jsonRepresentation()
404-
// });
405-
// }
406-
// }
403+
String jsonRepresentation() {
404+
return convertToJsonString({
405+
'notification': this.notification.jsonRepresentation()
406+
});
407+
}
408+
}

0 commit comments

Comments
 (0)