@@ -72,6 +72,8 @@ @interface OSMessagingController ()
7272
7373@property (nonatomic , nullable ) NSObject <OSInAppMessagePrompt>*currentPromptAction;
7474
75+ @property (nonatomic ) BOOL isAppInactive;
76+
7577@end
7678
7779@implementation OSMessagingController
@@ -138,6 +140,7 @@ - (instancetype)init {
138140 self.clickedClickIds = [[NSMutableSet alloc ] initWithSet: [standardUserDefaults getSavedSetForKey: OS_IAM_CLICKED_SET_KEY defaultValue: nil ]];
139141 self.impressionedInAppMessages = [[NSMutableSet alloc ] initWithSet: [standardUserDefaults getSavedSetForKey: OS_IAM_IMPRESSIONED_SET_KEY defaultValue: nil ]];
140142 self.currentPromptAction = nil ;
143+ self.isAppInactive = NO ;
141144 // BOOL that controls if in-app messaging is paused or not (false by default)
142145 [self setInAppMessagingPaused: false ];
143146 }
@@ -212,7 +215,12 @@ - (void)presentInAppMessage:(OSInAppMessage *)message {
212215 // Return early if an IAM is already showing
213216 if (self.isInAppMessageShowing )
214217 return ;
215-
218+ // Return early if the app is not active
219+ if ([[UIApplication sharedApplication ] applicationState ] != UIApplicationStateActive) {
220+ [OneSignal onesignal_Log: ONE_S_LL_VERBOSE message: @" Pause IAMs display due to app inactivity" ];
221+ _isAppInactive = YES ;
222+ return ;
223+ }
216224 [self displayMessage: message];
217225 };
218226}
@@ -355,7 +363,6 @@ - (void)setDataForRedisplay:(OSInAppMessage *)message {
355363 [self .impressionedInAppMessages removeObject: message.messageId];
356364 [message clearClickIds ];
357365 return ;
358-
359366 }
360367 }
361368}
@@ -439,7 +446,9 @@ - (void)messageViewControllerWasDismissed {
439446
440447 if (!_currentPromptAction) {
441448 [self evaluateMessageDisplayQueue ];
442- } // else do nothing prompt is handling the re-showing
449+ } else { // do nothing prompt is handling the re-showing
450+ [OneSignal onesignal_Log: ONE_S_LL_VERBOSE message: @" Stop evaluateMessageDisplayQueue because prompt is currently displayed" ];
451+ }
443452 }
444453}
445454
@@ -488,26 +497,25 @@ - (void)persistInAppMessageForRedisplay:(OSInAppMessage *)message {
488497 [OneSignal onesignal_Log: ONE_S_LL_VERBOSE message: [NSString stringWithFormat: @" persistInAppMessageForRedisplay: %@ \n redisplayedInAppMessages: %@ " , [message description ], [_redisplayedInAppMessages description ]]];
489498}
490499
491- - (void )handlePromptAction : (NSArray <NSObject <OSInAppMessagePrompt> *> *)promptActions {
500+ - (void )handlePromptActions : (NSArray <NSObject <OSInAppMessagePrompt> *> *)promptActions {
492501 for (NSObject <OSInAppMessagePrompt> *promptAction in promptActions) {
493- if (![promptAction didAppear ]) {
502+ // Don't show prompt twice
503+ if (!promptAction.hasPrompted ) {
494504 _currentPromptAction = promptAction;
505+ break ;
495506 }
496- break ;
497507 }
498508
499509 if (_currentPromptAction) {
500510 [OneSignal onesignal_Log: ONE_S_LL_VERBOSE message: [NSString stringWithFormat: @" IAM prompt to handle: %@ " , [_currentPromptAction description ]]];
501- _currentPromptAction.didAppear = YES ;
511+ _currentPromptAction.hasPrompted = YES ;
502512 [_currentPromptAction handlePrompt: ^(BOOL accepted) {
503513 _currentPromptAction = nil ;
504- // IAM dismissed by action
505- if (!_viewController) {
506- [OneSignal onesignal_Log: ONE_S_LL_VERBOSE message: @" IAM with prompt dismissed from actionTaken" ];
507- [self evaluateMessageDisplayQueue ];
508- }
509- // TODO: continue handling more than one prompt
514+ [self handlePromptActions: promptActions];
510515 }];
516+ } else if (!_viewController) { // IAM dismissed by action
517+ [OneSignal onesignal_Log: ONE_S_LL_VERBOSE message: @" IAM with prompt dismissed from actionTaken" ];
518+ [self evaluateMessageDisplayQueue ];
511519 }
512520}
513521
@@ -518,7 +526,7 @@ - (void)messageViewDidSelectAction:(OSInAppMessage *)message withAction:(OSInApp
518526 if (action.clickUrl )
519527 [self handleMessageActionWithURL: action];
520528
521- [self handlePromptAction : action.promptActions];
529+ [self handlePromptActions : action.promptActions];
522530
523531 if (self.actionClickBlock )
524532 self.actionClickBlock (action);
@@ -624,6 +632,16 @@ - (void)triggerConditionChanged {
624632 [self evaluateMessages ];
625633}
626634
635+ #pragma mark OSMessagingControllerDelegate Methods
636+ - (void )onApplicationDidBecomeActive {
637+ // To avoid excesive message evaluation
638+ // we should re-evaluate all in-app messages only if it was paused by inactive
639+ if (_isAppInactive) {
640+ [OneSignal onesignal_Log: ONE_S_LL_VERBOSE message: @" Evaluating messages due to inactive app" ];
641+ _isAppInactive = NO ;
642+ [self evaluateMessages ];
643+ }
644+ }
627645@end
628646
629647@implementation DummyOSMessagingController
0 commit comments