Skip to content

Commit f325fdb

Browse files
committed
Add IAM Lifecycle to iOS Flutter
* Add IAM Lifecycle methods to OneSignalPlugin.m * Add OSInAppMessageLifeCycleHandler to OneSignalPlugin declaration in OneSignalPlugin.h
1 parent 735becb commit f325fdb

File tree

2 files changed

+75
-1
lines changed

2 files changed

+75
-1
lines changed

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: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,12 @@ it will add the observers (ie. subscription)
5656
@property (strong, nonatomic) NSMutableDictionary* notificationCompletionCache;
5757
@property (strong, nonatomic) NSMutableDictionary* receivedNotificationCache;
5858

59+
@property (atomic) BOOL hasSetOnWillDisplayInAppMessageHandler;
60+
@property (atomic) BOOL hasSetOnDidDisplayInAppMessageHandler;
61+
@property (atomic) BOOL hasSetOnWillDismissInAppMessageHandler;
62+
@property (atomic) BOOL hasSetOnDidDismissInAppMessageHandler;
63+
64+
@property (strong, nonatomic) OSInAppMessage *inAppMessage;
5965
@end
6066

6167
@implementation OneSignalPlugin
@@ -70,6 +76,10 @@ + (instancetype)sharedInstance {
7076
sharedInstance.notificationCompletionCache = [NSMutableDictionary new];;
7177
sharedInstance.hasSetInAppMessageClickedHandler = false;
7278
sharedInstance.hasSetNotificationWillShowInForegroundHandler = false;
79+
sharedInstance.hasSetOnWillDisplayInAppMessageHandler = false;
80+
sharedInstance.hasSetOnDidDisplayInAppMessageHandler = false;
81+
sharedInstance.hasSetOnWillDismissInAppMessageHandler = false;
82+
sharedInstance.hasSetOnDidDismissInAppMessageHandler = false;
7383
});
7484
return sharedInstance;
7585
}
@@ -152,6 +162,14 @@ - (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
152162
[self initNotificationWillShowInForegroundHandlerParams];
153163
else if ([@"OneSignal#completeNotification" isEqualToString:call.method])
154164
[self completeNotification:call withResult:result];
165+
else if ([@"OneSignal#onWillDisplayInAppMessageHandlerParams" isEqualToString:call.method])
166+
[self onWillDisplayInAppMessageHandlerParams];
167+
else if ([@"OneSignal#onDidDisplayInAppMessageHandlerParams" isEqualToString:call.method])
168+
[self onDidDisplayInAppMessageHandlerParams];
169+
else if ([@"OneSignal#onWillDismissInAppMessageHandlerParams" isEqualToString:call.method])
170+
[self onWillDismissInAppMessageHandlerParams];
171+
else if ([@"OneSignal#onDidDismissInAppMessageHandlerParams" isEqualToString:call.method])
172+
[self onDidDismissInAppMessageHandlerParams];
155173
else
156174
result(FlutterMethodNotImplemented);
157175
}
@@ -160,6 +178,8 @@ - (void)setAppId:(FlutterMethodCall *)call withResult:(FlutterResult)result {
160178
[OneSignal setInAppMessageClickHandler:^(OSInAppMessageAction *action) {
161179
[self handleInAppMessageClicked:action];
162180
}];
181+
182+
[OneSignal setInAppMessageLifecycleHandler:self];
163183

164184
[OneSignal setAppId:call.arguments[@"appId"]];
165185

@@ -399,6 +419,60 @@ - (void)handleInAppMessageClicked:(OSInAppMessageAction *)action {
399419
[self.channel invokeMethod:@"OneSignal#handleClickedInAppMessage" arguments:action.toJson];
400420
}
401421

422+
#pragma mark In App Message lifecycle Handler
423+
- (void)onWillDisplayInAppMessageHandlerParams {
424+
self.hasSetOnWillDisplayInAppMessageHandler = YES;
425+
426+
if (self.inAppMessage) {
427+
[self onWillDisplayInAppMessage:self.inAppMessage];
428+
self.inAppMessage = nil;
429+
}
430+
}
431+
432+
- (void)onDidDisplayInAppMessageHandlerParams {
433+
self.hasSetOnDidDisplayInAppMessageHandler = YES;
434+
435+
if (self.inAppMessage) {
436+
[self onDidDisplayInAppMessage:self.inAppMessage];
437+
self.inAppMessage = nil;
438+
}
439+
}
440+
441+
- (void)onWillDismissInAppMessageHandlerParams {
442+
self.hasSetOnWillDismissInAppMessageHandler = YES;
443+
444+
if (self.inAppMessage) {
445+
[self onWillDismissInAppMessage:self.inAppMessage];
446+
self.inAppMessage = nil;
447+
}
448+
}
449+
450+
- (void)onDidDismissInAppMessageHandlerParams {
451+
self.hasSetOnDidDismissInAppMessageHandler = YES;
452+
453+
if (self.inAppMessage) {
454+
[self onDidDismissInAppMessage:self.inAppMessage];
455+
self.inAppMessage = nil;
456+
}
457+
}
458+
459+
#pragma mark OSInAppMessageLifeCycleHandler
460+
- (void)onWillDisplayInAppMessage:(OSInAppMessage *) result {
461+
[self.channel invokeMethod:@"OneSignal#onWillDisplayInAppMessage" arguments:result.toJson];
462+
}
463+
464+
- (void)onDidDisplayInAppMessage:(OSInAppMessage *) result {
465+
[self.channel invokeMethod:@"OneSignal#onDidDisplayInAppMessage" arguments:result.toJson];
466+
}
467+
468+
- (void)onWillDismissInAppMessage:(OSInAppMessage *) result {
469+
[self.channel invokeMethod:@"OneSignal#onWillDismissInAppMessage" arguments:result.toJson];
470+
}
471+
472+
- (void)onDidDismissInAppMessage:(OSInAppMessage *) result {
473+
[self.channel invokeMethod:@"OneSignal#onDidDismissInAppMessage" arguments:result.toJson];
474+
}
475+
402476
#pragma mark OSSubscriptionObserver
403477
- (void)onOSSubscriptionChanged:(OSSubscriptionStateChanges *)stateChanges {
404478
[self.channel invokeMethod:@"OneSignal#subscriptionChanged" arguments: stateChanges.toDictionary];

0 commit comments

Comments
 (0)