Skip to content

Commit 36949fd

Browse files
authored
Merge pull request #649 from OneSignal/user_model/InAppMessages-namespace
[User Model] InAppMessages Namespace + iOS
2 parents fda094f + eee32e4 commit 36949fd

10 files changed

+452
-99
lines changed

example/lib/main.dart

Lines changed: 64 additions & 92 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,29 @@ 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-
// });
77-
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-
// });
72+
OneSignal.InAppMessages
73+
.setInAppMessageClickedHandler((OSInAppMessageAction action) {
74+
this.setState(() {
75+
_debugLabelString =
76+
"In App Message Clicked: \n${action.jsonRepresentation().replaceAll("\\n", "\n")}";
77+
});
78+
});
9679

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

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

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

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

11396
// iOS-only method to open launch URLs in Safari when set to false
11497
// OneSignal.shared.setLaunchURLsInApp(false);
@@ -135,19 +118,6 @@ class _MyAppState extends State<MyApp> with OneSignalPushSubscriptionObserver {
135118
print(stateChanges.jsonRepresentation());
136119
}
137120

138-
void _handleGetTags() {
139-
// OneSignal.shared.getTags().then((tags) {
140-
// if (tags == null) return;
141-
142-
// setState((() {
143-
// _debugLabelString = "$tags";
144-
// }));
145-
// }).catchError((error) {
146-
// setState(() {
147-
// _debugLabelString = "$error";
148-
// });
149-
// });
150-
}
151121

152122
void _handleSendTags() {
153123
print("Sending tags");
@@ -158,11 +128,17 @@ class _MyAppState extends State<MyApp> with OneSignalPushSubscriptionObserver {
158128
OneSignal.User.addTags(sendTags);
159129
}
160130

131+
void _handleRemoveTag() {
132+
print("Deleting tag");
133+
OneSignal.User.removeTag("test2");
134+
135+
print("Deleting tags array");
136+
OneSignal.User.removeTags(['test']);
137+
}
138+
161139
void _handlePromptForPushPermission() {
162140
print("Prompting for Permission");
163-
// OneSignal.shared.promptUserForPushNotificationPermission().then((accepted) {
164-
// print("Accepted permission: $accepted");
165-
// });
141+
OneSignal.Notifications.requestPermission(true);
166142
}
167143

168144
void _handleGetDeviceState() async {
@@ -225,13 +201,7 @@ class _MyAppState extends State<MyApp> with OneSignalPushSubscriptionObserver {
225201
OneSignal.Location.setShared(true);
226202
}
227203

228-
void _handleRemoveTag() {
229-
print("Deleting tag");
230-
OneSignal.User.removeTag("test2");
231-
232-
print("Deleting tags array");
233-
OneSignal.User.removeTags(['test']);
234-
}
204+
235205

236206
void _handleSetExternalUserId() {
237207
print("Setting external user ID");
@@ -304,33 +274,35 @@ class _MyAppState extends State<MyApp> with OneSignalPushSubscriptionObserver {
304274
}
305275

306276
oneSignalInAppMessagingTriggerExamples() async {
307-
// /// Example addTrigger call for IAM
308-
// /// This will add 1 trigger so if there are any IAM satisfying it, it
309-
// /// will be shown to the user
310-
// OneSignal.shared.addTrigger("trigger_1", "one");
311-
312-
// /// Example addTriggers call for IAM
313-
// /// This will add 2 triggers so if there are any IAM satisfying these, they
314-
// /// will be shown to the user
315-
// Map<String, Object> triggers = new Map<String, Object>();
316-
// triggers["trigger_2"] = "two";
317-
// triggers["trigger_3"] = "three";
318-
// OneSignal.shared.addTriggers(triggers);
319-
320-
// // Removes a trigger by its key so if any future IAM are pulled with
321-
// // these triggers they will not be shown until the trigger is added back
322-
// OneSignal.shared.removeTriggerForKey("trigger_2");
323-
324-
// // Get the value for a trigger by its key
325-
// Object? triggerValue = await OneSignal.shared.getTriggerValueForKey("trigger_3");
326-
// print("'trigger_3' key trigger value: ${triggerValue?.toString()}");
327-
328-
// // Create a list and bulk remove triggers based on keys supplied
329-
// List<String> keys = ["trigger_1", "trigger_3"];
330-
// OneSignal.shared.removeTriggersForKeys(keys);
331-
332-
// // Toggle pausing (displaying or not) of IAMs
333-
// OneSignal.shared.pauseInAppMessages(false);
277+
/// Example addTrigger call for IAM
278+
/// This will add 1 trigger so if there are any IAM satisfying it, it
279+
/// will be shown to the user
280+
OneSignal.InAppMessages.addTrigger("trigger_1", "one");
281+
282+
/// Example addTriggers call for IAM
283+
/// This will add 2 triggers so if there are any IAM satisfying these, they
284+
/// will be shown to the user
285+
Map<String, Object> triggers = new Map<String, Object>();
286+
triggers["trigger_2"] = "two";
287+
triggers["trigger_3"] = "three";
288+
OneSignal.InAppMessages.addTriggers(triggers);
289+
290+
// Removes a trigger by its key so if any future IAM are pulled with
291+
// these triggers they will not be shown until the trigger is added back
292+
OneSignal.InAppMessages.removeTrigger("trigger_2");
293+
294+
295+
// Create a list and bulk remove triggers based on keys supplied
296+
List<String> keys = ["trigger_1", "trigger_3"];
297+
OneSignal.InAppMessages.removeTriggers(keys);
298+
299+
OneSignal.InAppMessages.clearTriggers();
300+
301+
// Toggle pausing (displaying or not) of IAMs
302+
OneSignal.InAppMessages.paused(false);
303+
var arePaused = await OneSignal.InAppMessages.arePaused();
304+
print('Notifications paused ${arePaused}');
305+
334306
}
335307

336308
oneSignalOutcomeExamples() async {
@@ -370,10 +342,10 @@ class _MyAppState extends State<MyApp> with OneSignalPushSubscriptionObserver {
370342
new OneSignalButton(
371343
"Send Tags", _handleSendTags, !_enableConsentButton)
372344
]),
373-
// new TableRow(children: [
374-
// new OneSignalButton("Prompt for Push Permission",
375-
// _handlePromptForPushPermission, !_enableConsentButton)
376-
// ]),
345+
new TableRow(children: [
346+
new OneSignalButton("Prompt for Push Permission",
347+
_handlePromptForPushPermission, !_enableConsentButton)
348+
]),
377349
// new TableRow(children: [
378350
// new OneSignalButton(
379351
// "Print Device State",

ios/Classes/OSFlutterInAppMessages.h

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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+
+ (instancetype)sharedInstance;
37+
38+
@property (atomic) BOOL hasSetInAppMessageClickedHandler;
39+
/*
40+
Holds reference to any in app messages received before any click action
41+
occurs on the body, button or image elements of the in app message
42+
*/
43+
@property (strong, nonatomic) OSInAppMessageAction *inAppMessageClickedResult;
44+
45+
@end

0 commit comments

Comments
 (0)