Skip to content

Commit

Permalink
Merge pull request #1278 from OneSignal/5.0.0/support_multiple_listeners
Browse files Browse the repository at this point in the history
[5.0.0] Support multiple IAM + Notification Lifecycle Listeners
  • Loading branch information
nan-li committed Oct 30, 2023
2 parents 4d0780a + cff82b4 commit 841a5d4
Show file tree
Hide file tree
Showing 11 changed files with 105 additions and 107 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "0820"
version = "1.7">
LastUpgradeVersion = "1410"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
buildImplicitDependencies = "YES">
Expand All @@ -20,50 +20,18 @@
ReferencedContainer = "container:OneSignalExample.xcodeproj">
</BuildableReference>
</BuildActionEntry>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "NO"
buildForProfiling = "NO"
buildForArchiving = "NO"
buildForAnalyzing = "NO">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "911E2CB91E398AB3003112A4"
BuildableName = "UnitTests.xctest"
BlueprintName = "UnitTests"
ReferencedContainer = "container:../OneSignalSDK/OneSignal.xcodeproj">
</BuildableReference>
</BuildActionEntry>
</BuildActionEntries>
</BuildAction>
<TestAction
buildConfiguration = "Debug 64 bit"
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES"
codeCoverageEnabled = "YES">
<AdditionalOptions>
<AdditionalOption
key = "MallocScribble"
value = ""
isEnabled = "YES">
</AdditionalOption>
</AdditionalOptions>
shouldUseLaunchSchemeArgsEnv = "YES">
<Testables>
<TestableReference
skipped = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "911E2CB91E398AB3003112A4"
BuildableName = "UnitTests.xctest"
BlueprintName = "UnitTests"
ReferencedContainer = "container:../OneSignalSDK/OneSignal.xcodeproj">
</BuildableReference>
</TestableReference>
</Testables>
</TestAction>
<LaunchAction
buildConfiguration = "Debug 64 bit"
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
launchStyle = "0"
Expand All @@ -82,26 +50,9 @@
ReferencedContainer = "container:OneSignalExample.xcodeproj">
</BuildableReference>
</BuildableProductRunnable>
<CommandLineArguments>
<CommandLineArgument
argument = "-FIRAnalyticsDebugEnabled"
isEnabled = "YES">
</CommandLineArgument>
</CommandLineArguments>
<AdditionalOptions>
<AdditionalOption
key = "MallocScribble"
value = ""
isEnabled = "YES">
</AdditionalOption>
</AdditionalOptions>
<LocationScenarioReference
identifier = "San Francisco, CA, USA"
referenceType = "1">
</LocationScenarioReference>
</LaunchAction>
<ProfileAction
buildConfiguration = "Release 64 bit"
buildConfiguration = "Release"
shouldUseLaunchSchemeArgsEnv = "YES"
savedToolIdentifier = ""
useCustomWorkingDirectory = "NO"
Expand All @@ -118,10 +69,10 @@
</BuildableProductRunnable>
</ProfileAction>
<AnalyzeAction
buildConfiguration = "Debug 64 bit">
buildConfiguration = "Debug">
</AnalyzeAction>
<ArchiveAction
buildConfiguration = "Release 64 bit"
buildConfiguration = "Release"
revealArchiveInOrganizer = "YES">
</ArchiveAction>
</Scheme>
6 changes: 5 additions & 1 deletion iOS_SDK/OneSignalSDK/OneSignalCore/Source/OSInAppMessages.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@

@property (strong, nonatomic, nonnull) NSString *messageId;

// Convert the object into a NSDictionary
// Dictionary of properties available on OSInAppMessage only
- (NSDictionary *_Nonnull)jsonRepresentation;

@end
Expand Down Expand Up @@ -78,18 +78,22 @@ typedef NS_ENUM(NSUInteger, OSInAppMessageActionUrlType) {

@interface OSInAppMessageWillDisplayEvent : NSObject
@property (nonatomic, readonly, nonnull) OSInAppMessage *message;
- (NSDictionary *_Nonnull)jsonRepresentation;
@end

@interface OSInAppMessageDidDisplayEvent : NSObject
@property (nonatomic, readonly, nonnull) OSInAppMessage *message;
- (NSDictionary *_Nonnull)jsonRepresentation;
@end

@interface OSInAppMessageWillDismissEvent : NSObject
@property (nonatomic, readonly, nonnull) OSInAppMessage *message;
- (NSDictionary *_Nonnull)jsonRepresentation;
@end

@interface OSInAppMessageDidDismissEvent : NSObject
@property (nonatomic, readonly, nonnull) OSInAppMessage *message;
- (NSDictionary *_Nonnull)jsonRepresentation;
@end

@interface OSInAppMessageClickEvent : NSObject
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ NS_ASSUME_NONNULL_BEGIN

- (void)addInAppMessageClickListener:(NSObject<OSInAppMessageClickListener> *_Nullable)listener;
- (void)removeInAppMessageClickListener:(NSObject<OSInAppMessageClickListener> *_Nullable)listener;
- (void)setInAppMessageDelegate:(NSObject<OSInAppMessageLifecycleListener> *_Nullable)delegate;
- (void)removeInAppMessageDelegate:(NSObject<OSInAppMessageLifecycleListener> *_Nullable)delegate;
- (void)addInAppMessageLifecycleListener:(NSObject<OSInAppMessageLifecycleListener> *_Nullable)listener;
- (void)removeInAppMessageLifecycleListener:(NSObject<OSInAppMessageLifecycleListener> *_Nullable)listener;

@end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,31 +39,63 @@
#import "OSSessionManager.h"

@implementation OSInAppMessageWillDisplayEvent

- (OSInAppMessageWillDisplayEvent*)initWithInAppMessage:(OSInAppMessage *)message {
_message = message;
return self;
}

- (NSDictionary *)jsonRepresentation {
NSMutableDictionary *json = [NSMutableDictionary new];
json[@"message"] = [self.message jsonRepresentation];
return json;
}

@end

@implementation OSInAppMessageDidDisplayEvent

- (OSInAppMessageDidDisplayEvent*)initWithInAppMessage:(OSInAppMessage *)message {
_message = message;
return self;
}

- (NSDictionary *)jsonRepresentation {
NSMutableDictionary *json = [NSMutableDictionary new];
json[@"message"] = [self.message jsonRepresentation];
return json;
}

@end

@implementation OSInAppMessageWillDismissEvent

- (OSInAppMessageWillDismissEvent*)initWithInAppMessage:(OSInAppMessage *)message {
_message = message;
return self;
}

- (NSDictionary *)jsonRepresentation {
NSMutableDictionary *json = [NSMutableDictionary new];
json[@"message"] = [self.message jsonRepresentation];
return json;
}

@end

@implementation OSInAppMessageDidDismissEvent

- (OSInAppMessageDidDismissEvent*)initWithInAppMessage:(OSInAppMessage *)message {
_message = message;
return self;
}

- (NSDictionary *)jsonRepresentation {
NSMutableDictionary *json = [NSMutableDictionary new];
json[@"message"] = [self.message jsonRepresentation];
return json;
}

@end

@interface OSMessagingController ()
Expand Down Expand Up @@ -91,7 +123,7 @@ @interface OSMessagingController ()

@property (nonatomic) NSMutableArray<NSObject<OSInAppMessageClickListener> *> *clickListeners;

@property (weak, nonatomic, nullable) NSObject<OSInAppMessageLifecycleListener> *inAppMessageDelegate;
@property (nonatomic) NSMutableArray<NSObject<OSInAppMessageLifecycleListener> *> *lifecycleListeners;

@property (strong, nullable) OSInAppMessageViewController *viewController;

Expand Down Expand Up @@ -169,6 +201,7 @@ - (instancetype)init {
[self initializeTriggerController];
self.messageDisplayQueue = [NSMutableArray new];
self.clickListeners = [NSMutableArray new];
self.lifecycleListeners = [NSMutableArray new];

let standardUserDefaults = OneSignalUserDefaults.initStandard;

Expand Down Expand Up @@ -309,43 +342,47 @@ - (void)removeInAppMessageClickListener:(NSObject<OSInAppMessageClickListener> *
[_clickListeners removeObject:listener];
}

- (void)setInAppMessageDelegate:(NSObject<OSInAppMessageLifecycleListener> *_Nullable)delegate {
_inAppMessageDelegate = delegate;
- (void)addInAppMessageLifecycleListener:(NSObject<OSInAppMessageLifecycleListener> *_Nullable)listener {
[_lifecycleListeners addObject:listener];
}

- (void)removeInAppMessageDelegate:(NSObject<OSInAppMessageLifecycleListener> *_Nullable)delegate {
_inAppMessageDelegate = nil;
- (void)removeInAppMessageLifecycleListener:(NSObject<OSInAppMessageLifecycleListener> *_Nullable)listener {
[_lifecycleListeners removeObject:listener];
}

- (void)onWillDisplayInAppMessage:(OSInAppMessageInternal *)message {
if (self.inAppMessageDelegate &&
[self.inAppMessageDelegate respondsToSelector:@selector(onWillDisplayInAppMessage:)]) {
OSInAppMessageWillDisplayEvent *event = [[OSInAppMessageWillDisplayEvent alloc] initWithInAppMessage:message];
[self.inAppMessageDelegate onWillDisplayInAppMessage:event];
for (NSObject<OSInAppMessageLifecycleListener> *listener in _lifecycleListeners) {
if ([listener respondsToSelector:@selector(onWillDisplayInAppMessage:)]) {
OSInAppMessageWillDisplayEvent *event = [[OSInAppMessageWillDisplayEvent alloc] initWithInAppMessage:message];
[listener onWillDisplayInAppMessage:event];
}
}
}

- (void)onDidDisplayInAppMessage:(OSInAppMessageInternal *)message {
if (self.inAppMessageDelegate &&
[self.inAppMessageDelegate respondsToSelector:@selector(onDidDisplayInAppMessage:)]) {
OSInAppMessageDidDisplayEvent *event = [[OSInAppMessageDidDisplayEvent alloc] initWithInAppMessage:message];
[self.inAppMessageDelegate onDidDisplayInAppMessage:event];
for (NSObject<OSInAppMessageLifecycleListener> *listener in _lifecycleListeners) {
if ([listener respondsToSelector:@selector(onDidDisplayInAppMessage:)]) {
OSInAppMessageDidDisplayEvent *event = [[OSInAppMessageDidDisplayEvent alloc] initWithInAppMessage:message];
[listener onDidDisplayInAppMessage:event];
}
}
}

- (void)onWillDismissInAppMessage:(OSInAppMessageInternal *)message {
if (self.inAppMessageDelegate &&
[self.inAppMessageDelegate respondsToSelector:@selector(onWillDismissInAppMessage:)]) {
OSInAppMessageWillDismissEvent *event = [[OSInAppMessageWillDismissEvent alloc] initWithInAppMessage:message];
[self.inAppMessageDelegate onWillDismissInAppMessage:event];
for (NSObject<OSInAppMessageLifecycleListener> *listener in _lifecycleListeners) {
if ([listener respondsToSelector:@selector(onWillDismissInAppMessage:)]) {
OSInAppMessageWillDismissEvent *event = [[OSInAppMessageWillDismissEvent alloc] initWithInAppMessage:message];
[listener onWillDismissInAppMessage:event];
}
}
}

- (void)onDidDismissInAppMessage:(OSInAppMessageInternal *)message {
if (self.inAppMessageDelegate &&
[self.inAppMessageDelegate respondsToSelector:@selector(onDidDismissInAppMessage:)]) {
OSInAppMessageDidDismissEvent *event = [[OSInAppMessageDidDismissEvent alloc] initWithInAppMessage:message];
[self.inAppMessageDelegate onDidDismissInAppMessage:event];
for (NSObject<OSInAppMessageLifecycleListener> *listener in _lifecycleListeners) {
if ([listener respondsToSelector:@selector(onDidDismissInAppMessage:)]) {
OSInAppMessageDidDismissEvent *event = [[OSInAppMessageDidDismissEvent alloc] initWithInAppMessage:message];
[listener onDidDismissInAppMessage:event];
}
}
}

Expand Down Expand Up @@ -565,10 +602,10 @@ - (void)setDataForRedisplay:(OSInAppMessageInternal *)message {
BOOL messageDismissed = [_seenInAppMessages containsObject:message.messageId];
let redisplayMessageSavedData = [_redisplayedInAppMessages objectForKey:message.messageId];

[OneSignalLog onesignalLog:ONE_S_LL_VERBOSE message:[NSString stringWithFormat:@"Redisplay dismissed: %@ and data: %@", messageDismissed ? @"YES" : @"NO", redisplayMessageSavedData.jsonRepresentation.description]];
[OneSignalLog onesignalLog:ONE_S_LL_VERBOSE message:[NSString stringWithFormat:@"Redisplay dismissed: %@ and data: %@", messageDismissed ? @"YES" : @"NO", redisplayMessageSavedData.jsonRepresentationInternal.description]];

if (messageDismissed && redisplayMessageSavedData) {
[OneSignalLog onesignalLog:ONE_S_LL_VERBOSE message:[NSString stringWithFormat:@"Redisplay IAM: %@", message.jsonRepresentation.description]];
[OneSignalLog onesignalLog:ONE_S_LL_VERBOSE message:[NSString stringWithFormat:@"Redisplay IAM: %@", message.jsonRepresentationInternal.description]];

message.displayStats.displayQuantity = redisplayMessageSavedData.displayStats.displayQuantity;
message.displayStats.lastDisplayTime = redisplayMessageSavedData.displayStats.lastDisplayTime;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ - (instancetype)initWithInAppMessage:(OSInAppMessageInternal *)message clickResu

- (NSDictionary *)jsonRepresentation {
let json = [NSMutableDictionary new];
json[@"message"] = self.message;
json[@"message"] = [self.message jsonRepresentation];
json[@"result"] = [self.result jsonRepresentation];
return json;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ NS_ASSUME_NONNULL_BEGIN
- (void)addClickId:(NSString *)clickId;

- (BOOL)isFinished;
// Dictionary of properties available on OSInAppMessageInternal
- (NSDictionary *_Nonnull)jsonRepresentationInternal;

@end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ + (instancetype)instancePreviewFromNotification:(OSNotification *)notification {
return message;
}

-(NSDictionary *)jsonRepresentation {
- (NSDictionary *)jsonRepresentationInternal {
let json = [NSMutableDictionary new];

json[@"messageId"] = self.messageId;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,12 @@ + (void)removeClickListener:(NSObject<OSInAppMessageClickListener> *_Nullable)li

+ (void)addLifecycleListener:(NSObject<OSInAppMessageLifecycleListener> *_Nullable)listener {
[OneSignalLog onesignalLog:ONE_S_LL_VERBOSE message:@"In app message lifecycle listener added successfully"];
[OSMessagingController.sharedInstance setInAppMessageDelegate:listener];
[OSMessagingController.sharedInstance addInAppMessageLifecycleListener:listener];
}

+ (void)removeLifecycleListener:(NSObject<OSInAppMessageLifecycleListener> *_Nullable)listener {
[OneSignalLog onesignalLog:ONE_S_LL_VERBOSE message:@"In app message lifecycle listener removed successfully"];
[OSMessagingController.sharedInstance removeInAppMessageDelegate:listener];
[OSMessagingController.sharedInstance removeInAppMessageLifecycleListener:listener];
}

+ (void)addTrigger:(NSString * _Nonnull)key withValue:(NSString * _Nonnull)value {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ NS_SWIFT_NAME(onClick(event:));
@interface OSNotificationsManager : NSObject <OSNotifications>

@property (class, weak, nonatomic, nullable) id<OneSignalNotificationsDelegate> delegate;
@property (class, weak, nonatomic, nullable) NSObject<OSNotificationLifecycleListener> *lifecycleListener;

+ (Class<OSNotifications> _Nonnull)Notifications;
+ (void)start;
Expand Down
Loading

0 comments on commit 841a5d4

Please sign in to comment.