Skip to content

Commit 5bffbfe

Browse files
committed
InAppMessages namespace
1 parent fda094f commit 5bffbfe

File tree

7 files changed

+403
-45
lines changed

7 files changed

+403
-45
lines changed

example/lib/main.dart

Lines changed: 27 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ class _MyAppState extends State<MyApp> with OneSignalPushSubscriptionObserver {
4444
// Outcome Examples
4545
oneSignalOutcomeExamples();
4646

47+
OneSignal.shared.login("Testtesttest");
48+
4749
// OneSignal.shared.setRequiresUserPrivacyConsent(_requireConsent);
4850

4951
OneSignal.Notifications
@@ -67,48 +69,31 @@ class _MyAppState extends State<MyApp> with OneSignalPushSubscriptionObserver {
6769
});
6870
});
6971

70-
// OneSignal.shared
71-
// .setInAppMessageClickedHandler((OSInAppMessageAction action) {
72-
// this.setState(() {
73-
// _debugLabelString =
74-
// "In App Message Clicked: \n${action.jsonRepresentation().replaceAll("\\n", "\n")}";
75-
// });
76-
// });
72+
OneSignal.InAppMessages.pause(false);
7773

78-
// OneSignal.shared
79-
// .setSubscriptionObserver((OSSubscriptionStateChanges changes) {
80-
// print("SUBSCRIPTION STATE CHANGED: ${changes.jsonRepresentation()}");
81-
// });
82-
83-
// OneSignal.shared.setPermissionObserver((OSPermissionStateChanges changes) {
84-
// print("PERMISSION STATE CHANGED: ${changes.jsonRepresentation()}");
85-
// });
86-
87-
// OneSignal.shared.setEmailSubscriptionObserver(
88-
// (OSEmailSubscriptionStateChanges changes) {
89-
// print("EMAIL SUBSCRIPTION STATE CHANGED ${changes.jsonRepresentation()}");
90-
// });
91-
92-
// OneSignal.shared.setSMSSubscriptionObserver(
93-
// (OSSMSSubscriptionStateChanges changes) {
94-
// print("SMS SUBSCRIPTION STATE CHANGED ${changes.jsonRepresentation()}");
95-
// });
74+
OneSignal.InAppMessages
75+
.setInAppMessageClickedHandler((OSInAppMessageAction action) {
76+
this.setState(() {
77+
_debugLabelString =
78+
"In App Message Clicked: \n${action.jsonRepresentation().replaceAll("\\n", "\n")}";
79+
});
80+
});
9681

97-
// OneSignal.shared.setOnWillDisplayInAppMessageHandler((message) {
98-
// print("ON WILL DISPLAY IN APP MESSAGE ${message.messageId}");
99-
// });
82+
OneSignal.InAppMessages.setOnWillDisplayInAppMessageHandler((message) {
83+
print("ON WILL DISPLAY IN APP MESSAGE ${message.messageId}");
84+
});
10085

101-
// OneSignal.shared.setOnDidDisplayInAppMessageHandler((message) {
102-
// print("ON DID DISPLAY IN APP MESSAGE ${message.messageId}");
103-
// });
86+
OneSignal.InAppMessages.setOnDidDisplayInAppMessageHandler((message) {
87+
print("ON DID DISPLAY IN APP MESSAGE ${message.messageId}");
88+
});
10489

105-
// OneSignal.shared.setOnWillDismissInAppMessageHandler((message) {
106-
// print("ON WILL DISMISS IN APP MESSAGE ${message.messageId}");
107-
// });
90+
OneSignal.InAppMessages.setOnWillDismissInAppMessageHandler((message) {
91+
print("ON WILL DISMISS IN APP MESSAGE ${message.messageId}");
92+
});
10893

109-
// OneSignal.shared.setOnDidDismissInAppMessageHandler((message) {
110-
// print("ON DID DISMISS IN APP MESSAGE ${message.messageId}");
111-
// });
94+
OneSignal.InAppMessages.setOnDidDismissInAppMessageHandler((message) {
95+
print("ON DID DISMISS IN APP MESSAGE ${message.messageId}");
96+
});
11297

11398
// iOS-only method to open launch URLs in Safari when set to false
11499
// OneSignal.shared.setLaunchURLsInApp(false);
@@ -160,9 +145,7 @@ class _MyAppState extends State<MyApp> with OneSignalPushSubscriptionObserver {
160145

161146
void _handlePromptForPushPermission() {
162147
print("Prompting for Permission");
163-
// OneSignal.shared.promptUserForPushNotificationPermission().then((accepted) {
164-
// print("Accepted permission: $accepted");
165-
// });
148+
OneSignal.Notifications.requestPermission(true);
166149
}
167150

168151
void _handleGetDeviceState() async {
@@ -370,10 +353,10 @@ class _MyAppState extends State<MyApp> with OneSignalPushSubscriptionObserver {
370353
new OneSignalButton(
371354
"Send Tags", _handleSendTags, !_enableConsentButton)
372355
]),
373-
// new TableRow(children: [
374-
// new OneSignalButton("Prompt for Push Permission",
375-
// _handlePromptForPushPermission, !_enableConsentButton)
376-
// ]),
356+
new TableRow(children: [
357+
new OneSignalButton("Prompt for Push Permission",
358+
_handlePromptForPushPermission, !_enableConsentButton)
359+
]),
377360
// new TableRow(children: [
378361
// new OneSignalButton(
379362
// "Print Device State",

ios/Classes/OSFlutterInAppMessages.h

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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 <OneSignalFramework/OneSignalFramework.h>
32+
33+
@interface OSFlutterInAppMessages : NSObject<FlutterPlugin, OSInAppMessageLifecycleHandler>
34+
35+
@property (strong, nonatomic) FlutterMethodChannel *channel;
36+
@property (atomic) BOOL hasSetInAppMessageClickedHandler;
37+
38+
/*
39+
Holds reference to any in app messages received before any click action
40+
occurs on the body, button or image elements of the in app message
41+
*/
42+
@property (strong, nonatomic) OSInAppMessageAction *inAppMessageClickedResult;
43+
44+
@end

ios/Classes/OSFlutterInAppMessages.m

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
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 "OSFlutterDebug.h"
29+
#import <OneSignalFramework/OneSignalFramework.h>
30+
#import "OSFlutterCategories.h"
31+
32+
@implementation OSFlutterInAppMessages
33+
34+
+ (instancetype)sharedInstance {
35+
static OSFlutterInAppMessages *sharedInstance = nil;
36+
static dispatch_once_t onceToken;
37+
dispatch_once(&onceToken, ^{
38+
sharedInstance = [OSFlutterInAppMessages new];
39+
sharedInstance.hasSetInAppMessageClickedHandler = false;
40+
});
41+
return sharedInstance;
42+
}
43+
44+
+ (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar>*)registrar {
45+
OSFlutterInAppMessages *instance = [OSFlutterInAppMessages new];
46+
47+
instance.channel = [FlutterMethodChannel
48+
methodChannelWithName:@"OneSignal#inappmessages"
49+
binaryMessenger:[registrar messenger]];
50+
51+
[registrar addMethodCallDelegate:instance channel:instance.channel];
52+
53+
[OneSignal.InAppMessages setLifecycleHandler:^(OSInAppMessageAction *action) {
54+
[self handleInAppMessageClicked:action];
55+
}];
56+
}
57+
58+
- (void)handleMethodCall:(FlutterMethodCall *)call result:(FlutterResult)result {
59+
if ([@"OneSignal#addTrigger" isEqualToString:call.method]) {
60+
[self addTriggers:call withResult:result];
61+
} else if ([@"OneSignal#addTriggers" isEqualToString:call.method]) {
62+
[self addTriggers:call withResult:result];
63+
} else if ([@"OneSignal#removeTrigger" isEqualToString:call.method]) {
64+
[self removeTrigger:call withResult:result];
65+
} else if ([@"OneSignal#removeTriggers" isEqualToString:call.method]) {
66+
[self removeTriggers:call withResult:result];
67+
} else if ([@"OneSignal#clearTriggers" isEqualToString:call.method]) {
68+
result([OneSignal clearTriggers:call.arguments]);
69+
} else if ([@"OneSignal#paused" isEqualToString:call.method]) {
70+
[self paused:call withResult:result];
71+
} else if ([@"OneSignal#arePaused" isEqualToString:call.method]) {
72+
result(@([OneSignal paused];))
73+
} else if ([@"OneSignal#initInAppMessageClickedHandlerParams" isEqualToString:call.method])
74+
[self initInAppMessageClickedHandlerParams];{
75+
else
76+
result(FlutterMethodNotImplemented);
77+
}
78+
}
79+
80+
- (void)addTriggers:(FlutterMethodCall *)call withResult:(FlutterResult)result {
81+
NSDictionary *triggers = call.arguments;
82+
[OneSignal.InAppMessages addTriggers:triggers];
83+
result(nil);
84+
}
85+
86+
- (void)removeTrigger:(FlutterMethodCall *)call withResult:(FlutterResult)result {
87+
NSString *key = call.arguments;
88+
[OneSignal.InAppMessages removeTrigger:key];
89+
result(nil);
90+
}
91+
92+
- (void)removeTriggers:(FlutterMethodCall *)call withResult:(FlutterResult)result {
93+
NSArray *keys = call.arguments;
94+
[OneSignal.InAppMessages removeTriggersForKeys:keys];
95+
result(nil);
96+
}
97+
98+
- (void)clearTriggers:(FlutterMethodCall *)call withResult:(FlutterResult)result {
99+
[OneSignal.InAppMessages clearTriggers];
100+
result(nil);
101+
}
102+
103+
- (void)paused:(FlutterMethodCall *)call withResult:(FlutterResult)result {
104+
BOOL pause = [call.arguments boolValue];
105+
[OneSignal.InAppMessages paused:pause];
106+
result(nil);
107+
}
108+
109+
110+
#pragma mark In App Message Click
111+
- (void)initInAppMessageClickedHandlerParams {
112+
_hasSetInAppMessageClickedHandler = true;
113+
114+
if (self.inAppMessageClickedResult) {
115+
[self handleInAppMessageClicked:self.inAppMessageClickedResult];
116+
self.inAppMessageClickedResult = nil;
117+
}
118+
}
119+
120+
- (void)handleInAppMessageClicked:(OSInAppMessageAction *)action {
121+
if (!self.hasSetInAppMessageClickedHandler) {
122+
_inAppMessageClickedResult = action;
123+
return;
124+
}
125+
126+
[self.channel invokeMethod:@"OneSignal#handleClickedInAppMessage" arguments:action.toJson];
127+
}
128+
129+
#pragma mark OSInAppMessageLifeCycleHandler
130+
- (void)onWillDisplayInAppMessage:(OSInAppMessage *) result {
131+
[self.channel invokeMethod:@"OneSignal#onWillDisplayInAppMessage" arguments:result.toJson];
132+
}
133+
134+
- (void)onDidDisplayInAppMessage:(OSInAppMessage *) result {
135+
[self.channel invokeMethod:@"OneSignal#onDidDisplayInAppMessage" arguments:result.toJson];
136+
}
137+
138+
- (void)onWillDismissInAppMessage:(OSInAppMessage *) result {
139+
[self.channel invokeMethod:@"OneSignal#onWillDismissInAppMessage" arguments:result.toJson];
140+
}
141+
142+
- (void)onDidDismissInAppMessage:(OSInAppMessage *) result {
143+
[self.channel invokeMethod:@"OneSignal#onDidDismissInAppMessage" arguments:result.toJson];
144+
}
145+
146+
@end

ios/Classes/OneSignalPlugin.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ + (instancetype)sharedInstance {
6464
#pragma mark FlutterPlugin
6565
+ (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar>*)registrar {
6666

67-
[OneSignal setMSDKType:@"flutter"];
67+
// [OneSignal setMSDKType:@"flutter"];
6868

6969
OneSignalPlugin.sharedInstance.channel = [FlutterMethodChannel
7070
methodChannelWithName:@"OneSignal"

lib/onesignal_flutter.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,14 @@ import 'package:onesignal_flutter/src/user.dart';
99
import 'package:onesignal_flutter/src/notifications.dart';
1010
import 'package:onesignal_flutter/src/session.dart';
1111
import 'package:onesignal_flutter/src/location.dart';
12+
import 'package:onesignal_flutter/src/inappmessages.dart';
1213

1314
export 'src/permission.dart';
1415
export 'src/defines.dart';
1516
export 'src/pushsubscription.dart';
1617
export 'src/subscription.dart';
1718
export 'src/notification.dart';
19+
export 'src/inappmessage.dart';
1820

1921
class OneSignal {
2022
/// A singleton representing the OneSignal SDK.
@@ -27,6 +29,7 @@ class OneSignal {
2729
static OneSignalNotifications Notifications = new OneSignalNotifications();
2830
static OneSignalSession Session = new OneSignalSession();
2931
static OneSignalLocation Location = new OneSignalLocation();
32+
static OneSignalInAppMessages InAppMessages = new OneSignalInAppMessages();
3033

3134

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

lib/src/inappmessage.dart

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import 'package:onesignal_flutter/src/utils.dart';
2+
3+
/// When a click action is defined on an In App Message form the dashboard,
4+
/// the handler returns an OSInAppMessageAction object so the Dart code can act accordingly
5+
/// This allows for custom action events within Dart
6+
class OSInAppMessageAction extends JSONStringRepresentable {
7+
8+
// Name of the action event defined for the IAM element
9+
String? clickName;
10+
11+
// URL given to the IAM element defined in the dashboard
12+
String? clickUrl;
13+
14+
// Determines if a first click has occurred or not on the IAM element
15+
bool firstClick = false;
16+
17+
// Whether or not the click action should dismiss the IAM
18+
bool closesMessage = false;
19+
20+
OSInAppMessageAction(Map<String, dynamic> json) {
21+
this.clickName = json["click_name"];
22+
this.clickUrl = json["click_url"];
23+
this.firstClick = json["first_click"] as bool;
24+
this.closesMessage = json["closes_message"] as bool;
25+
}
26+
27+
String jsonRepresentation() {
28+
return convertToJsonString({
29+
'click_name': this.clickName,
30+
'click_url': this.clickUrl,
31+
'first_click': this.firstClick,
32+
'closes_message': this.closesMessage,
33+
});
34+
}
35+
36+
}
37+
38+
class OSInAppMessage extends JSONStringRepresentable {
39+
String? messageId;
40+
41+
OSInAppMessage(Map<String, dynamic> json) {
42+
this.messageId = json["message_id"];
43+
}
44+
45+
String jsonRepresentation() {
46+
return convertToJsonString({
47+
'message_id': this.messageId
48+
});
49+
}
50+
}

0 commit comments

Comments
 (0)