Skip to content

Commit db6d8e2

Browse files
committed
Adding Session namespace and methods + updates to the Notification namespace
1 parent 5f257f2 commit db6d8e2

File tree

9 files changed

+213
-53
lines changed

9 files changed

+213
-53
lines changed

example/lib/main.dart

Lines changed: 30 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -41,28 +41,31 @@ class _MyAppState extends State<MyApp> with OneSignalPushSubscriptionObserver {
4141

4242
OneSignal.User.pushSubscription.addObserver(this);
4343

44-
// OneSignal.shared.setRequiresUserPrivacyConsent(_requireConsent);
44+
// Outcome Examples
45+
oneSignalOutcomeExamples();
4546

46-
// OneSignal.shared
47-
// .setNotificationOpenedHandler((OSNotificationOpenedResult result) {
48-
// print('NOTIFICATION OPENED HANDLER CALLED WITH: ${result}');
49-
// this.setState(() {
50-
// _debugLabelString =
51-
// "Opened notification: \n${result.notification.jsonRepresentation().replaceAll("\\n", "\n")}";
52-
// });
53-
// });
47+
// OneSignal.shared.setRequiresUserPrivacyConsent(_requireConsent);
5448

55-
// OneSignal.shared
56-
// .setNotificationWillShowInForegroundHandler((OSNotificationReceivedEvent event) {
57-
// print('FOREGROUND HANDLER CALLED WITH: ${event}');
58-
// /// Display Notification, send null to not display
59-
// event.complete(null);
49+
OneSignal.Notifications
50+
.setNotificationOpenedHandler((OSNotificationOpenedResult result) {
51+
print('NOTIFICATION OPENED HANDLER CALLED WITH: ${result}');
52+
this.setState(() {
53+
_debugLabelString =
54+
"Opened notification: \n${result.notification.jsonRepresentation().replaceAll("\\n", "\n")}";
55+
});
56+
});
57+
58+
OneSignal.Notifications
59+
.setNotificationWillShowInForegroundHandler((OSNotificationReceivedEvent event) {
60+
print('FOREGROUND HANDLER CALLED WITH: ${event}');
61+
/// Display Notification, send null to not display
62+
event.complete(null);
6063

61-
// this.setState(() {
62-
// _debugLabelString =
63-
// "Notification received in foreground notification: \n${event.notification.jsonRepresentation().replaceAll("\\n", "\n")}";
64-
// });
65-
// });
64+
this.setState(() {
65+
_debugLabelString =
66+
"Notification received in foreground notification: \n${event.notification.jsonRepresentation().replaceAll("\\n", "\n")}";
67+
});
68+
});
6669

6770
// OneSignal.shared
6871
// .setInAppMessageClickedHandler((OSInAppMessageAction action) {
@@ -330,32 +333,16 @@ class _MyAppState extends State<MyApp> with OneSignalPushSubscriptionObserver {
330333
// OneSignal.shared.pauseInAppMessages(false);
331334
}
332335

333-
oneSignalOutcomeEventsExamples() async {
334-
// Await example for sending outcomes
335-
outcomeAwaitExample();
336+
oneSignalOutcomeExamples() async {
337+
338+
OneSignal.Session.addOutcome("normal_1");
339+
OneSignal.Session.addOutcome("normal_2");
336340

337-
// Send a normal outcome and get a reply with the name of the outcome
338-
// OneSignal.shared.sendOutcome("normal_1");
339-
// OneSignal.shared.sendOutcome("normal_2").then((outcomeEvent) {
340-
// print(outcomeEvent.jsonRepresentation());
341-
// });
342-
343-
// Send a unique outcome and get a reply with the name of the outcome
344-
// OneSignal.shared.sendUniqueOutcome("unique_1");
345-
// OneSignal.shared.sendUniqueOutcome("unique_2").then((outcomeEvent) {
346-
// print(outcomeEvent.jsonRepresentation());
347-
// });
348-
349-
// Send an outcome with a value and get a reply with the name of the outcome
350-
// OneSignal.shared.sendOutcomeWithValue("value_1", 3.2);
351-
// OneSignal.shared.sendOutcomeWithValue("value_2", 3.9).then((outcomeEvent) {
352-
// print(outcomeEvent.jsonRepresentation());
353-
// });
354-
}
341+
OneSignal.Session.addUniqueOutcome("unique_1");
342+
OneSignal.Session.addUniqueOutcome("unique_2");
355343

356-
Future<void> outcomeAwaitExample() async {
357-
// var outcomeEvent = await OneSignal.shared.sendOutcome("await_normal_1");
358-
// print(outcomeEvent.jsonRepresentation());
344+
OneSignal.Session.addOutcomeWithValue("value_1", 3.2);
345+
OneSignal.Session.addOutcomeWithValue("value_2", 3.9);
359346
}
360347

361348
void _handleOptIn() {

ios/Classes/OSFlutterNotifications.m

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ - (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
7575
[self removePermissionObserver:call withResult:result];
7676
else if ([@"OneSignal#initNotificationWillShowInForegroundHandlerParams" isEqualToString:call.method])
7777
[self initNotificationWillShowInForegroundHandlerParams];
78+
else if ([@"OneSignal#completeNotification" isEqualToString:call.method])
79+
[self completeNotification:call withResult:result];
7880
else if ([@"OneSignal#initNotificationOpenedHandlerParams" isEqualToString:call.method])
7981
[self initNotificationOpenedHandlerParams];
8082
else
@@ -134,6 +136,28 @@ - (void)handleNotificationWillShowInForeground:(OSNotification *)notification co
134136
[self.channel invokeMethod:@"OneSignal#handleNotificationWillShowInForeground" arguments:notification.toJson];
135137
}
136138

139+
- (void)completeNotification:(FlutterMethodCall *)call withResult:(FlutterResult)result {
140+
NSString *notificationId = call.arguments[@"notificationId"];
141+
BOOL shouldDisplay = [call.arguments[@"shouldDisplay"] boolValue];
142+
OSNotificationDisplayResponse completion = self.notificationCompletionCache[notificationId];
143+
144+
if (!completion) {
145+
// TODO: log
146+
//[OneSignal onesignalLog:ONE_S_LL_ERROR message:[NSString stringWithFormat:@"OneSignal (objc): could not find notification completion block with id: %@", notificationId]];
147+
return;
148+
}
149+
150+
if (shouldDisplay) {
151+
OSNotification *notification = self.receivedNotificationCache[notificationId];
152+
completion(notification);
153+
} else {
154+
completion(nil);
155+
}
156+
157+
[self.notificationCompletionCache removeObjectForKey:notificationId];
158+
[self.receivedNotificationCache removeObjectForKey:notificationId];
159+
}
160+
137161
#pragma mark Opened Notification
138162

139163
- (void)initNotificationOpenedHandlerParams {

ios/Classes/OSFlutterSession.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 OSFlutterSession : NSObject<FlutterPlugin>
33+
34+
@property (strong, nonatomic) FlutterMethodChannel *channel;
35+
36+
@end

ios/Classes/OSFlutterSession.m

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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 "OSFlutterSession.h"
29+
#import <OneSignalFramework/OneSignalFramework.h>
30+
#import "OSFlutterCategories.h"
31+
32+
@implementation OSFlutterSession
33+
+ (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar>*)registrar {
34+
OSFlutterSession *instance = [OSFlutterSession new];
35+
36+
instance.channel = [FlutterMethodChannel
37+
methodChannelWithName:@"OneSignal#session"
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#addOutcome" isEqualToString:call.method]) {
45+
[self addOutcome:call withResult:result];
46+
} else if ([@"OneSignal#addUniqueOutcome" isEqualToString:call.method]) {
47+
[self addUniqueOutcome:call withResult:result];
48+
} else if ([@"OneSignal#addOutcomeWithValue" isEqualToString:call.method]) {
49+
[self addOutcomeWithValue:call withResult:result];
50+
} else {
51+
result(FlutterMethodNotImplemented);
52+
}
53+
}
54+
55+
- (void)addOutcome:(FlutterMethodCall *)call withResult:(FlutterResult)result {
56+
NSString *name = call.arguments;
57+
[OneSignal.Session addOutcome:name];
58+
result(nil);
59+
}
60+
61+
- (void)addUniqueOutcome:(FlutterMethodCall *)call withResult:(FlutterResult)result {
62+
NSString *name = call.arguments;
63+
[OneSignal.Session addUniqueOutcome:name];
64+
result(nil);
65+
}
66+
67+
- (void)addOutcomeWithValue:(FlutterMethodCall *)call withResult:(FlutterResult)result {
68+
NSString *name = call.arguments[@"outcome_name"];
69+
NSNumber *value = call.arguments[@"outcome_value"];
70+
[OneSignal.Session addOutcomeWithValue:name value:value];
71+
result(nil);
72+
}
73+
74+
75+
@end

ios/Classes/OneSignalPlugin.m

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#import "OSFlutterDebug.h"
3131
#import "OSFlutterUser.h"
3232
#import "OSFlutterNotifications.h"
33+
#import "OSFlutterSession.h"
3334

3435

3536
@interface OneSignalPlugin ()
@@ -72,6 +73,7 @@ + (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar>*)registrar {
7273
[OSFlutterDebug registerWithRegistrar:registrar];
7374
[OSFlutterUser registerWithRegistrar:registrar];
7475
[OSFlutterNotifications registerWithRegistrar:registrar];
76+
[OSFlutterSession registerWithRegistrar:registrar];
7577

7678
}
7779

lib/onesignal_flutter.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@ import 'package:onesignal_flutter/src/utils.dart';
77
import 'package:onesignal_flutter/src/debug.dart';
88
import 'package:onesignal_flutter/src/user.dart';
99
import 'package:onesignal_flutter/src/notifications.dart';
10+
import 'package:onesignal_flutter/src/session.dart';
1011

1112
export 'src/permission.dart';
1213
export 'src/defines.dart';
1314
export 'src/pushsubscription.dart';
1415
export 'src/subscription.dart';
16+
export 'src/notification.dart';
1517

1618
class OneSignal {
1719
/// A singleton representing the OneSignal SDK.
@@ -22,6 +24,7 @@ class OneSignal {
2224
static OneSignalDebug Debug = new OneSignalDebug();
2325
static OneSignalUser User = new OneSignalUser();
2426
static OneSignalNotifications Notifications = new OneSignalNotifications();
27+
static OneSignalSession Session = new OneSignalSession();
2528

2629

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

lib/src/notification.dart

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -389,16 +389,14 @@ class OSNotificationReceivedEvent extends JSONStringRepresentable {
389389
notification = OSNotification(json);
390390
}
391391

392-
//TODO: remove
393-
394-
// void complete(OSNotification? notification) {
395-
// print('OSNotificationReceivedEvent complete with notification: $notification');
396-
// if (notification != null) {
397-
// OneSignal.shared.completeNotification(notification.notificationId, true);
398-
// } else {
399-
// OneSignal.shared.completeNotification(this.notification.notificationId, false);
400-
// }
401-
// }
392+
void complete(OSNotification? notification) {
393+
print('OSNotificationReceivedEvent complete with notification: $notification');
394+
if (notification != null) {
395+
OneSignal.Notifications.completeNotification(notification.notificationId, true);
396+
} else {
397+
OneSignal.Notifications.completeNotification(this.notification.notificationId, false);
398+
}
399+
}
402400

403401
String jsonRepresentation() {
404402
return convertToJsonString({

lib/src/notifications.dart

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,13 @@ class OneSignalNotifications {
101101
_channel.invokeMethod("OneSignal#initNotificationWillShowInForegroundHandlerParams");
102102
}
103103

104+
/// The notification foreground handler is called whenever a notification arrives
105+
/// and the application is in foreground
106+
void completeNotification(String notificationId, bool shouldDisplay) {
107+
_channel.invokeMethod("OneSignal#completeNotification",
108+
{'notificationId': notificationId, 'shouldDisplay': shouldDisplay});
109+
}
110+
104111

105112
/// The notification opened handler is called whenever the user opens a
106113
/// OneSignal push notification, or taps an action button on a notification.

lib/src/session.dart

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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 OneSignalSession {
7+
8+
// private channels used to bridge to ObjC/Java
9+
MethodChannel _channel = const MethodChannel('OneSignal#session');
10+
11+
/// Send a normal outcome event for the current session and notifications with the attribution window
12+
/// Counted each time sent successfully, failed ones will be cached and reattempted in future
13+
Future<void> addOutcome(String name) async {
14+
await _channel.invokeMethod("OneSignal#addOutcome", name);
15+
}
16+
17+
/// Send a unique outcome event for the current session and notifications with the attribution window
18+
/// Counted once per notification when sent successfully, failed ones will be cached and reattempted in future
19+
Future<void> addUniqueOutcome(String name) async {
20+
_channel.invokeMethod("OneSignal#addUniqueOutcome", name);
21+
}
22+
23+
/// Send an outcome event with a value for the current session and notifications with the attribution window
24+
/// Counted each time sent successfully, failed ones will be cached and reattempted in future
25+
Future<void> addOutcomeWithValue(String name, double value) async {
26+
_channel.invokeMethod("OneSignal#addOutcomeWithValue", {"outcome_name" : name, "outcome_value" : value});
27+
}
28+
}

0 commit comments

Comments
 (0)