Skip to content

Commit 3f03b5f

Browse files
authored
Merge pull request #531 from OneSignal/feature/IAM-lifecycle-methods
Add in app mesage lifecycle handler
2 parents 3289cea + 3af37db commit 3f03b5f

File tree

9 files changed

+154
-2
lines changed

9 files changed

+154
-2
lines changed

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
import com.onesignal.OSEmailSubscriptionObserver;
1111
import com.onesignal.OSEmailSubscriptionStateChanges;
1212
import com.onesignal.OSInAppMessageAction;
13+
import com.onesignal.OSInAppMessage;
14+
import com.onesignal.OSInAppMessageLifecycleHandler;
1315
import com.onesignal.OSNotification;
1416
import com.onesignal.OSNotificationOpenedResult;
1517
import com.onesignal.OSNotificationReceivedEvent;
@@ -61,6 +63,7 @@ public class OneSignalPlugin
6163
private boolean hasSetRequiresPrivacyConsent = false;
6264
private boolean waitingForUserPrivacyConsent = false;
6365

66+
6467
private final HashMap<String, OSNotificationReceivedEvent> notificationReceivedEventCache = new HashMap<>();
6568

6669
public OneSignalPlugin() {
@@ -204,6 +207,7 @@ private void setAppId(MethodCall call, Result reply) {
204207
OneSignal.setInAppMessageClickHandler(this);
205208
OneSignal.initWithContext(context);
206209
OneSignal.setAppId(appId);
210+
setInAppMessageLifecycleHandler();
207211

208212
if (hasSetRequiresPrivacyConsent && !OneSignal.userProvidedPrivacyConsent())
209213
this.waitingForUserPrivacyConsent = true;
@@ -430,6 +434,31 @@ public void inAppMessageClicked(OSInAppMessageAction action) {
430434
invokeMethodOnUiThread("OneSignal#handleClickedInAppMessage", OneSignalSerializer.convertInAppMessageClickedActionToMap(action));
431435
}
432436

437+
/* in app message lifecycle */
438+
public void setInAppMessageLifecycleHandler() {
439+
OneSignal.setInAppMessageLifecycleHandler(new OSInAppMessageLifecycleHandler() {
440+
@Override
441+
public void onWillDisplayInAppMessage(OSInAppMessage message) {
442+
invokeMethodOnUiThread("OneSignal#onWillDisplayInAppMessage", OneSignalSerializer.convertInAppMessageToMap(message));
443+
}
444+
445+
@Override
446+
public void onDidDisplayInAppMessage(OSInAppMessage message) {
447+
invokeMethodOnUiThread("OneSignal#onDidDisplayInAppMessage", OneSignalSerializer.convertInAppMessageToMap(message));
448+
}
449+
450+
@Override
451+
public void onWillDismissInAppMessage(OSInAppMessage message) {
452+
invokeMethodOnUiThread("OneSignal#onWillDismissInAppMessage", OneSignalSerializer.convertInAppMessageToMap(message));
453+
}
454+
455+
@Override
456+
public void onDidDismissInAppMessage(OSInAppMessage message) {
457+
invokeMethodOnUiThread("OneSignal#onDidDismissInAppMessage", OneSignalSerializer.convertInAppMessageToMap(message));
458+
}
459+
});
460+
}
461+
433462
@Override
434463
public void notificationWillShowInForeground(OSNotificationReceivedEvent notificationReceivedEvent) {
435464
if (!this.hasSetNotificationWillShowInForegroundHandler) {

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import com.onesignal.OSEmailSubscriptionState;
77
import com.onesignal.OSEmailSubscriptionStateChanges;
88
import com.onesignal.OSInAppMessageAction;
9+
import com.onesignal.OSInAppMessage;
910
import com.onesignal.OSNotification;
1011
import com.onesignal.OSNotificationAction;
1112
import com.onesignal.OSNotificationOpenedResult;
@@ -232,6 +233,14 @@ static HashMap<String, Object> convertInAppMessageClickedActionToMap(OSInAppMess
232233
return hash;
233234
}
234235

236+
static HashMap<String, Object> convertInAppMessageToMap(OSInAppMessage message) {
237+
HashMap<String, Object> hash = new HashMap<>();
238+
239+
hash.put("message_id", message.getMessageId());
240+
241+
return hash;
242+
}
243+
235244
static HashMap<String, Object> convertNotificationReceivedEventToMap(OSNotificationReceivedEvent notificationReceivedEvent) throws JSONException {
236245
return convertNotificationToMap(notificationReceivedEvent.getNotification());
237246
}

example/lib/main.dart

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,22 @@ class _MyAppState extends State<MyApp> {
8383
print("SMS SUBSCRIPTION STATE CHANGED ${changes.jsonRepresentation()}");
8484
});
8585

86+
OneSignal.shared.setOnWillDisplayInAppMessageHandler((message) {
87+
print("ON WILL DISPLAY IN APP MESSAGE ${message.messageId}");
88+
});
89+
90+
OneSignal.shared.setOnDidDisplayInAppMessageHandler((message) {
91+
print("ON DID DISPLAY IN APP MESSAGE ${message.messageId}");
92+
});
93+
94+
OneSignal.shared.setOnWillDismissInAppMessageHandler((message) {
95+
print("ON WILL DISMISS IN APP MESSAGE ${message.messageId}");
96+
});
97+
98+
OneSignal.shared.setOnDidDismissInAppMessageHandler((message) {
99+
print("ON DID DISMISS IN APP MESSAGE ${message.messageId}");
100+
});
101+
86102
// NOTE: Replace with your own app ID from https://www.onesignal.com
87103
await OneSignal.shared
88104
.setAppId("380dc082-5231-4cc2-ab51-a03da5a0e4c2");

ios/Classes/OSFlutterCategories.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@
4343
- (NSDictionary *)toJson;
4444
@end
4545

46+
@interface OSInAppMessage (Flutter)
47+
- (NSDictionary *)toJson;
48+
@end
49+
4650
@interface NSError (Flutter)
4751
- (FlutterError *)flutterError;
4852
@end

ios/Classes/OSFlutterCategories.m

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,16 @@ - (NSDictionary *)toJson {
9797
}
9898
@end
9999

100+
@implementation OSInAppMessage (Flutter)
101+
- (NSDictionary *)toJson {
102+
NSMutableDictionary *json = [NSMutableDictionary new];
103+
104+
json[@"message_id"] = self.messageId;
105+
106+
return json;
107+
}
108+
@end
109+
100110
@implementation NSError (Flutter)
101111
- (FlutterError *)flutterError {
102112
return [FlutterError errorWithCode:[NSString stringWithFormat:@"%i", (int)self.code] message:self.localizedDescription details:nil];

ios/Classes/OneSignalPlugin.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
#import <Flutter/Flutter.h>
2929
#import <OneSignal/OneSignal.h>
3030

31-
@interface OneSignalPlugin : NSObject<FlutterPlugin, OSSubscriptionObserver, OSPermissionObserver, OSEmailSubscriptionObserver, OSSMSSubscriptionObserver>
31+
@interface OneSignalPlugin : NSObject<FlutterPlugin, OSSubscriptionObserver, OSPermissionObserver, OSEmailSubscriptionObserver, OSSMSSubscriptionObserver, OSInAppMessageLifecycleHandler>
3232

3333
// Do NOT initialize instances of this class.
3434
// You must only reference the shared instance.

ios/Classes/OneSignalPlugin.m

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ it will add the observers (ie. subscription)
5555

5656
@property (strong, nonatomic) NSMutableDictionary* notificationCompletionCache;
5757
@property (strong, nonatomic) NSMutableDictionary* receivedNotificationCache;
58-
5958
@end
6059

6160
@implementation OneSignalPlugin
@@ -163,6 +162,8 @@ - (void)setAppId:(FlutterMethodCall *)call withResult:(FlutterResult)result {
163162

164163
[OneSignal setAppId:call.arguments[@"appId"]];
165164

165+
[OneSignal setInAppMessageLifecycleHandler: self];
166+
166167
// If the user has required privacy consent, the SDK will not
167168
// add these observers. So we should delay adding the observers
168169
// until consent has been provided.
@@ -399,6 +400,23 @@ - (void)handleInAppMessageClicked:(OSInAppMessageAction *)action {
399400
[self.channel invokeMethod:@"OneSignal#handleClickedInAppMessage" arguments:action.toJson];
400401
}
401402

403+
#pragma mark OSInAppMessageLifeCycleHandler
404+
- (void)onWillDisplayInAppMessage:(OSInAppMessage *) result {
405+
[self.channel invokeMethod:@"OneSignal#onWillDisplayInAppMessage" arguments:result.toJson];
406+
}
407+
408+
- (void)onDidDisplayInAppMessage:(OSInAppMessage *) result {
409+
[self.channel invokeMethod:@"OneSignal#onDidDisplayInAppMessage" arguments:result.toJson];
410+
}
411+
412+
- (void)onWillDismissInAppMessage:(OSInAppMessage *) result {
413+
[self.channel invokeMethod:@"OneSignal#onWillDismissInAppMessage" arguments:result.toJson];
414+
}
415+
416+
- (void)onDidDismissInAppMessage:(OSInAppMessage *) result {
417+
[self.channel invokeMethod:@"OneSignal#onDidDismissInAppMessage" arguments:result.toJson];
418+
}
419+
402420
#pragma mark OSSubscriptionObserver
403421
- (void)onOSSubscriptionChanged:(OSSubscriptionStateChanges *)stateChanges {
404422
[self.channel invokeMethod:@"OneSignal#subscriptionChanged" arguments: stateChanges.toDictionary];

lib/onesignal_flutter.dart

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ typedef void EmailSubscriptionChangeHandler(OSEmailSubscriptionStateChanges chan
2525
typedef void SMSSubscriptionChangeHandler(OSSMSSubscriptionStateChanges changes);
2626
typedef void PermissionChangeHandler(OSPermissionStateChanges changes);
2727
typedef void InAppMessageClickedHandler(OSInAppMessageAction action);
28+
typedef void OnWillDisplayInAppMessageHandler(OSInAppMessage message);
29+
typedef void OnDidDisplayInAppMessageHandler(OSInAppMessage message);
30+
typedef void OnWillDismissInAppMessageHandler(OSInAppMessage message);
31+
typedef void OnDidDismissInAppMessageHandler(OSInAppMessage message);
2832
typedef void NotificationWillShowInForegroundHandler(OSNotificationReceivedEvent event);
2933

3034
class OneSignal {
@@ -47,6 +51,10 @@ class OneSignal {
4751
SMSSubscriptionChangeHandler? _onSMSSubscriptionChangedHandler;
4852
PermissionChangeHandler? _onPermissionChangedHandler;
4953
InAppMessageClickedHandler? _onInAppMessageClickedHandler;
54+
OnWillDisplayInAppMessageHandler? _onWillDisplayInAppMessageHandler;
55+
OnDidDisplayInAppMessageHandler? _onDidDisplayInAppMessageHandler;
56+
OnWillDismissInAppMessageHandler? _onWillDismissInAppMessageHandler;
57+
OnDidDismissInAppMessageHandler? _onDidDismissInAppMessageHandler;
5058
NotificationWillShowInForegroundHandler? _onNotificationWillShowInForegroundHandler;
5159

5260
// constructor method
@@ -114,6 +122,34 @@ class OneSignal {
114122
_channel.invokeMethod("OneSignal#initInAppMessageClickedHandlerParams");
115123
}
116124

125+
/// The in app message will display handler is called whenever the in app message
126+
/// is about to be displayed
127+
void setOnWillDisplayInAppMessageHandler(
128+
OnWillDisplayInAppMessageHandler handler) {
129+
_onWillDisplayInAppMessageHandler = handler;
130+
}
131+
132+
/// The in app message did display handler is called whenever the in app message
133+
/// is displayed
134+
void setOnDidDisplayInAppMessageHandler(
135+
OnDidDisplayInAppMessageHandler handler) {
136+
_onDidDisplayInAppMessageHandler = handler;
137+
}
138+
139+
/// The in app message will dismiss handler is called whenever the in app message
140+
/// is about to be dismissed
141+
void setOnWillDismissInAppMessageHandler(
142+
OnWillDismissInAppMessageHandler handler) {
143+
_onWillDismissInAppMessageHandler = handler;
144+
}
145+
146+
/// The in app message did dismiss handler is called whenever the in app message
147+
/// is dismissed
148+
void setOnDidDismissInAppMessageHandler(
149+
OnDidDismissInAppMessageHandler handler) {
150+
_onDidDismissInAppMessageHandler = handler;
151+
}
152+
117153
/// The notification foreground handler is called whenever a notification arrives
118154
/// and the application is in foreground
119155
void setNotificationWillShowInForegroundHandler(NotificationWillShowInForegroundHandler handler) {
@@ -428,6 +464,22 @@ class OneSignal {
428464
this._onInAppMessageClickedHandler != null) {
429465
this._onInAppMessageClickedHandler!(
430466
OSInAppMessageAction(call.arguments.cast<String, dynamic>()));
467+
} else if (call.method == 'OneSignal#onWillDisplayInAppMessage' &&
468+
this._onWillDisplayInAppMessageHandler != null) {
469+
this._onWillDisplayInAppMessageHandler!(
470+
OSInAppMessage(call.arguments.cast<String, dynamic>()));
471+
} else if (call.method == 'OneSignal#onDidDisplayInAppMessage' &&
472+
this._onDidDisplayInAppMessageHandler != null) {
473+
this._onDidDisplayInAppMessageHandler!(
474+
OSInAppMessage(call.arguments.cast<String, dynamic>()));
475+
} else if (call.method == 'OneSignal#onWillDismissInAppMessage' &&
476+
this._onWillDismissInAppMessageHandler != null) {
477+
this._onWillDismissInAppMessageHandler!(
478+
OSInAppMessage(call.arguments.cast<String, dynamic>()));
479+
} else if (call.method == 'OneSignal#onDidDismissInAppMessage' &&
480+
this._onDidDismissInAppMessageHandler != null) {
481+
this._onDidDismissInAppMessageHandler!(
482+
OSInAppMessage(call.arguments.cast<String, dynamic>()));
431483
} else if (call.method == 'OneSignal#handleNotificationWillShowInForeground' &&
432484
this._onNotificationWillShowInForegroundHandler != null) {
433485
this._onNotificationWillShowInForegroundHandler!(

lib/src/in_app_message.dart

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,17 @@ class OSInAppMessageAction extends JSONStringRepresentable {
3434
}
3535

3636
}
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)