Skip to content

Commit cd764e4

Browse files
authored
Merge pull request #105 from OneSignal/in_app_messaging_and_bug_fixes
Flutter SDK updates for IAM and bug fixes
2 parents e0bab99 + c798909 commit cd764e4

File tree

7 files changed

+353
-92
lines changed

7 files changed

+353
-92
lines changed

android/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ android {
3434
}
3535

3636
dependencies {
37-
compile('com.onesignal:OneSignal:3.10.5')
37+
compile('com.onesignal:OneSignal:3.11.1')
3838
}
3939

4040
// Adds required manifestPlaceholders keys to allow mainifest merge gradle step to complete

android/src/main/java/com/onesignal/flutter/OneSignalPlugin.java

Lines changed: 192 additions & 69 deletions
Large diffs are not rendered by default.

android/src/main/java/com/onesignal/flutter/OneSignalSerializer.java

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
import java.util.Iterator;
1212
import java.util.List;
1313

14-
public class OneSignalSerializer {
15-
static private HashMap<String, Object> convertSubscriptionStateToMap(OSSubscriptionState state) {
14+
class OneSignalSerializer {
15+
private static HashMap<String, Object> convertSubscriptionStateToMap(OSSubscriptionState state) {
1616
HashMap<String, Object> hash = new HashMap<>();
1717

1818
hash.put("subscribed", state.getSubscribed());
@@ -23,15 +23,15 @@ static private HashMap<String, Object> convertSubscriptionStateToMap(OSSubscript
2323
return hash;
2424
}
2525

26-
static private HashMap<String, Object> convertPermissionStateToMap(OSPermissionState state) {
26+
private static HashMap<String, Object> convertPermissionStateToMap(OSPermissionState state) {
2727
HashMap<String, Object> permission = new HashMap<>();
2828

2929
permission.put("enabled", state.getEnabled());
3030

3131
return permission;
3232
}
3333

34-
static private HashMap<String, Object> convertEmailSubscriptionStateToMap(OSEmailSubscriptionState state) {
34+
private static HashMap<String, Object> convertEmailSubscriptionStateToMap(OSEmailSubscriptionState state) {
3535
HashMap<String, Object> hash = new HashMap<>();
3636

3737
hash.put("emailUserId", state.getEmailUserId());
@@ -41,7 +41,7 @@ static private HashMap<String, Object> convertEmailSubscriptionStateToMap(OSEmai
4141
return hash;
4242
}
4343

44-
static public HashMap<String, Object> convertPermissionSubscriptionStateToMap(OSPermissionSubscriptionState state) {
44+
static HashMap<String, Object> convertPermissionSubscriptionStateToMap(OSPermissionSubscriptionState state) {
4545
HashMap<String, Object> hash = new HashMap<>();
4646

4747
OSSubscriptionState subState = state.getSubscriptionStatus();
@@ -60,7 +60,7 @@ static public HashMap<String, Object> convertPermissionSubscriptionStateToMap(OS
6060
return hash;
6161
}
6262

63-
static public HashMap<String, Object> convertSubscriptionStateChangesToMap(OSSubscriptionStateChanges changes) {
63+
static HashMap<String, Object> convertSubscriptionStateChangesToMap(OSSubscriptionStateChanges changes) {
6464
HashMap<String, Object> hash = new HashMap<>();
6565

6666
hash.put("to", convertSubscriptionStateToMap(changes.getTo()));
@@ -69,7 +69,7 @@ static public HashMap<String, Object> convertSubscriptionStateChangesToMap(OSSub
6969
return hash;
7070
}
7171

72-
static public HashMap<String, Object> convertEmailSubscriptionStateChangesToMap(OSEmailSubscriptionStateChanges changes) {
72+
static HashMap<String, Object> convertEmailSubscriptionStateChangesToMap(OSEmailSubscriptionStateChanges changes) {
7373
HashMap<String, Object> hash = new HashMap<>();
7474

7575
hash.put("to", convertEmailSubscriptionStateToMap(changes.getTo()));
@@ -78,7 +78,7 @@ static public HashMap<String, Object> convertEmailSubscriptionStateChangesToMap(
7878
return hash;
7979
}
8080

81-
static public HashMap convertPermissionStateChangesToMap(OSPermissionStateChanges changes) {
81+
static HashMap convertPermissionStateChangesToMap(OSPermissionStateChanges changes) {
8282
HashMap<String, Object> hash = new HashMap<>();
8383

8484
hash.put("to", convertPermissionStateToMap(changes.getTo()));
@@ -87,7 +87,7 @@ static public HashMap convertPermissionStateChangesToMap(OSPermissionStateChange
8787
return hash;
8888
}
8989

90-
static private HashMap<String, Object> convertNotificationPayloadToMap(OSNotificationPayload payload) throws JSONException {
90+
private static HashMap<String, Object> convertNotificationPayloadToMap(OSNotificationPayload payload) throws JSONException {
9191
HashMap<String, Object> hash = new HashMap<>();
9292

9393
hash.put("notificationId", payload.notificationID);
@@ -140,7 +140,7 @@ static private HashMap<String, Object> convertNotificationPayloadToMap(OSNotific
140140
return hash;
141141
}
142142

143-
static public HashMap<String, Object> convertNotificationToMap(OSNotification notification) throws JSONException {
143+
static HashMap<String, Object> convertNotificationToMap(OSNotification notification) throws JSONException {
144144
HashMap<String, Object> hash = new HashMap<>();
145145

146146
hash.put("payload", convertNotificationPayloadToMap(notification.payload));
@@ -161,7 +161,7 @@ static public HashMap<String, Object> convertNotificationToMap(OSNotification no
161161
return hash;
162162
}
163163

164-
static public HashMap<String, Object> convertNotificationOpenResultToMap(OSNotificationOpenResult openResult) throws JSONException {
164+
static HashMap<String, Object> convertNotificationOpenResultToMap(OSNotificationOpenResult openResult) throws JSONException {
165165
HashMap<String, Object> hash = new HashMap<>();
166166

167167
hash.put("notification", convertNotificationToMap(openResult.notification));
@@ -170,7 +170,7 @@ static public HashMap<String, Object> convertNotificationOpenResultToMap(OSNotif
170170
return hash;
171171
}
172172

173-
static private HashMap<String, Object> convertNotificationActionToMap(OSNotificationAction action) {
173+
private static HashMap<String, Object> convertNotificationActionToMap(OSNotificationAction action) {
174174
HashMap<String, Object> hash = new HashMap<>();
175175

176176
hash.put("id", action.actionID);
@@ -186,7 +186,18 @@ static private HashMap<String, Object> convertNotificationActionToMap(OSNotifica
186186
return hash;
187187
}
188188

189-
static private HashMap<String, Object> convertAndroidBackgroundImageLayoutToMap(BackgroundImageLayout layout) {
189+
static HashMap<String, Object> convertInAppMessageClickedActionToMap(OSInAppMessageAction action) {
190+
HashMap<String, Object> hash = new HashMap<>();
191+
192+
hash.put("click_name", action.clickName);
193+
hash.put("click_url", action.clickUrl);
194+
hash.put("first_click", action.firstClick);
195+
hash.put("closes_message", action.closesMessage);
196+
197+
return hash;
198+
}
199+
200+
private static HashMap<String, Object> convertAndroidBackgroundImageLayoutToMap(BackgroundImageLayout layout) {
190201
HashMap<String, Object> hash = new HashMap<>();
191202

192203
hash.put("image", layout.image);
@@ -196,7 +207,7 @@ static private HashMap<String, Object> convertAndroidBackgroundImageLayoutToMap(
196207
return hash;
197208
}
198209

199-
static public HashMap<String, Object> convertJSONObjectToHashMap(JSONObject object) throws JSONException {
210+
static HashMap<String, Object> convertJSONObjectToHashMap(JSONObject object) throws JSONException {
200211
HashMap<String, Object> hash = new HashMap<>();
201212

202213
if (object == null || object == JSONObject.NULL)
@@ -224,7 +235,7 @@ static public HashMap<String, Object> convertJSONObjectToHashMap(JSONObject obje
224235
return hash;
225236
}
226237

227-
static private List<Object> convertJSONArrayToList(JSONArray array) throws JSONException {
238+
private static List<Object> convertJSONArrayToList(JSONArray array) throws JSONException {
228239
List<Object> list = new ArrayList<>();
229240

230241
for (int i = 0; i < array.length(); i++) {

example/lib/main.dart

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class _MyAppState extends State<MyApp> {
3939
OSiOSSettings.promptBeforeOpeningPushUrl: true
4040
};
4141

42-
OneSignal.shared.setNotificationReceivedHandler((notification) {
42+
OneSignal.shared.setNotificationReceivedHandler((OSNotification notification) {
4343
this.setState(() {
4444
_debugLabelString =
4545
"Received notification: \n${notification.jsonRepresentation().replaceAll("\\n", "\n")}";
@@ -54,6 +54,14 @@ class _MyAppState extends State<MyApp> {
5454
});
5555
});
5656

57+
OneSignal.shared
58+
.setInAppMessageClickedHandler((OSInAppMessageAction action) {
59+
this.setState(() {
60+
_debugLabelString =
61+
"In App Message Clicked: \n${action.jsonRepresentation().replaceAll("\\n", "\n")}";
62+
});
63+
});
64+
5765
OneSignal.shared
5866
.setSubscriptionObserver((OSSubscriptionStateChanges changes) {
5967
print("SUBSCRIPTION STATE CHANGED: ${changes.jsonRepresentation()}");
@@ -80,6 +88,9 @@ class _MyAppState extends State<MyApp> {
8088
this.setState(() {
8189
_enableConsentButton = requiresConsent;
8290
});
91+
92+
// Some examples of how to use In App Messaging public methods with OneSignal SDK
93+
oneSignalInAppMessagingTriggerExamples();
8394
}
8495

8596
void _handleGetTags() {
@@ -222,6 +233,38 @@ class _MyAppState extends State<MyApp> {
222233
});
223234
}
224235

236+
oneSignalInAppMessagingTriggerExamples() async {
237+
/// Example addTrigger call for IAM
238+
/// This will add 1 trigger so if there are any IAM satisfying it, it
239+
/// will be shown to the user
240+
OneSignal.shared.addTrigger("trigger_1", "value_1");
241+
242+
/// Example addTriggers call for IAM
243+
/// This will add 2 triggers so if there are any IAM satisfying these, they
244+
/// will be shown to the user
245+
Map<String, Object> triggers = new Map<String, Object>();
246+
triggers["trigger_2"] = "value_2";
247+
triggers["trigger_3"] = "value_3";
248+
OneSignal.shared.addTriggers(triggers);
249+
250+
// Removes a trigger by its key so if any future IAM are pulled with
251+
// these triggers they will not be shown until the trigger is added back
252+
OneSignal.shared.removeTriggerForKey("trigger_2");
253+
254+
// Get the value for a trigger by its key
255+
Object triggerValue = await OneSignal.shared.getTriggerValueForKey("trigger_3");
256+
print("'trigger_3' key trigger value: " + triggerValue);
257+
258+
// Create a list and bulk remove triggers based on keys supplied
259+
List<String> keys = new List<String>();
260+
keys.add("trigger_1");
261+
keys.add("trigger_3");
262+
OneSignal.shared.removeTriggerForKeys(keys);
263+
264+
// Toggle pausing (displaying or not) of IAMs
265+
OneSignal.shared.pauseInAppMessages(true);
266+
}
267+
225268
@override
226269
Widget build(BuildContext context) {
227270
return new MaterialApp(

ios/Classes/OneSignalPlugin.m

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,8 @@ - (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
124124
[self setEmail:call withResult:result];
125125
} else if ([@"OneSignal#logoutEmail" isEqualToString:call.method]) {
126126
[self logoutEmail:call withResult:result];
127-
} else if ([@"OneSignal#didSetNotificationOpenedHandler" isEqualToString:call.method]) {
128-
[self didSetNotificationOpenedHandler];
127+
} else if ([@"OneSignal#initNotificationOpenedHandlerParams" isEqualToString:call.method]) {
128+
[self initNotificationOpenedHandlerParams];
129129
} else if ([@"OneSignal#setExternalUserId" isEqualToString:call.method]) {
130130
[OneSignal setExternalUserId:call.arguments[@"externalUserId"]];
131131
} else if ([@"OneSignal#removeExternalUserId" isEqualToString:call.method]) {
@@ -229,7 +229,7 @@ - (void)oneSignalLog:(FlutterMethodCall *)call withResult:(FlutterResult)result
229229
[OneSignal onesignal_Log:(ONE_S_LOG_LEVEL)[call.arguments[@"logLevel"] integerValue] message:(NSString *)call.arguments[@"message"]];
230230
}
231231

232-
- (void)didSetNotificationOpenedHandler {
232+
- (void)initNotificationOpenedHandlerParams {
233233
if (self.coldStartOpenResult) {
234234
[self handleNotificationOpened:self.coldStartOpenResult];
235235
self.coldStartOpenResult = nil;

lib/onesignal.dart

Lines changed: 51 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,23 @@ import 'package:onesignal/src/defines.dart';
66
import 'package:onesignal/src/utils.dart';
77
import 'package:onesignal/src/notification.dart';
88
import 'package:onesignal/src/create_notification.dart';
9+
import 'package:onesignal/src/in_app_message.dart';
910

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

1618
// Handlers for various events
1719
typedef void ReceivedNotificationHandler(OSNotification notification);
1820
typedef void OpenedNotificationHandler(OSNotificationOpenedResult openedResult);
1921
typedef void SubscriptionChangedHandler(OSSubscriptionStateChanges changes);
2022
typedef void EmailSubscriptionChangeHandler(
21-
OSEmailSubscriptionStateChanges changes);
23+
OSEmailSubscriptionStateChanges changes);
2224
typedef void PermissionChangeHandler(OSPermissionStateChanges changes);
25+
typedef void InAppMessageClickedHandler(OSInAppMessageAction action);
2326

2427
class OneSignal {
2528
/// A singleton representing the OneSignal SDK.
@@ -38,6 +41,7 @@ class OneSignal {
3841
SubscriptionChangedHandler _onSubscriptionChangedHandler;
3942
EmailSubscriptionChangeHandler _onEmailSubscriptionChangedHandler;
4043
PermissionChangeHandler _onPermissionChangedHandler;
44+
InAppMessageClickedHandler _onInAppMessageClickedHandler;
4145

4246
// constructor method
4347
OneSignal() {
@@ -75,7 +79,7 @@ class OneSignal {
7579
/// OneSignal push notification, or taps an action button on a notification.
7680
void setNotificationOpenedHandler(OpenedNotificationHandler handler) {
7781
_onOpenedNotification = handler;
78-
_channel.invokeMethod("OneSignal#didSetNotificationOpenedHandler");
82+
_channel.invokeMethod("OneSignal#initNotificationOpenedHandlerParams");
7983
}
8084

8185
/// The subscription handler will be called whenever the user's OneSignal
@@ -99,6 +103,13 @@ class OneSignal {
99103
_onEmailSubscriptionChangedHandler = handler;
100104
}
101105

106+
/// The in app message clicked handler is called whenever the user clicks a
107+
/// OneSignal IAM button or image with an action event attacthed to it
108+
void setInAppMessageClickedHandler(InAppMessageClickedHandler handler) {
109+
_onInAppMessageClickedHandler = handler;
110+
_channel.invokeMethod("OneSignal#initInAppMessageClickedHandlerParams");
111+
}
112+
102113
/// Allows you to completely disable the SDK until your app calls the
103114
/// OneSignal.consentGranted(true) function. This is useful if you want
104115
/// to show a Terms and Conditions or privacy popup for GDPR.
@@ -274,6 +285,40 @@ class OneSignal {
274285
return await _channel.invokeMethod("OneSignal#removeExternalUserId");
275286
}
276287

288+
/// Adds a single key, value trigger, which will trigger an in app message
289+
/// if one exists matching the specific trigger added
290+
Future<void> addTrigger(String key, Object value) async {
291+
return await _channel.invokeMethod("OneSignal#addTrigger", {key : value});
292+
}
293+
294+
/// Adds one or more key, value triggers, which will trigger in app messages
295+
/// (one at a time) if any exist matching the specific triggers added
296+
Future<void> addTriggers(Map<String, Object> triggers) async {
297+
return await _channel.invokeMethod("OneSignal#addTriggers", triggers);
298+
}
299+
300+
/// Remove a single key, value trigger to prevent an in app message from
301+
/// showing with that trigger
302+
Future<void> removeTriggerForKey(String key) async {
303+
return await _channel.invokeMethod("OneSignal#removeTriggerForKey", key);
304+
}
305+
306+
/// Remove one or more key, value triggers to prevent any in app messages
307+
/// from showing with those triggers
308+
Future<void> removeTriggerForKeys(List<String> keys) async {
309+
return await _channel.invokeMethod("OneSignal#removeTriggerForKeys", keys);
310+
}
311+
312+
/// Get the trigger value associated with the key provided
313+
Future<Object> getTriggerValueForKey(String key) async {
314+
return await _channel.invokeMethod("OneSignal#getTriggerValueForKey", key);
315+
}
316+
317+
/// Toggles the showing of all in app messages
318+
Future<void> pauseInAppMessages(bool pause) async {
319+
return await _channel.invokeMethod("OneSignal#pauseInAppMessages", pause);
320+
}
321+
277322
// Private function that gets called by ObjC/Java
278323
Future<Null> _handleMethod(MethodCall call) async {
279324
if (call.method == 'OneSignal#handleReceivedNotification' &&
@@ -297,8 +342,11 @@ class OneSignal {
297342
this._onEmailSubscriptionChangedHandler(
298343
OSEmailSubscriptionStateChanges(
299344
call.arguments.cast<String, dynamic>()));
345+
} else if (call.method == 'OneSignal#handleClickedInAppMessage' &&
346+
this._onInAppMessageClickedHandler != null) {
347+
this._onInAppMessageClickedHandler(
348+
OSInAppMessageAction(call.arguments.cast<String, dynamic>()));
300349
}
301-
302350
return null;
303351
}
304352

lib/src/in_app_message.dart

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import 'package:onesignal/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;
16+
17+
// Whether or not the click action should dismiss the IAM
18+
bool closesMessage;
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+
}

0 commit comments

Comments
 (0)