@@ -44,10 +44,18 @@ - (void)didReceiveRemoteNotification:(NSDictionary *)userInfo
4444
4545-(void )didReceiveRemoteNotification : (NSDictionary *)userInfo withAction : (NSString *__nullable)action fetchCompletionHandler : (LeanplumFetchCompletionBlock __nullable)completionHandler
4646{
47+ if (@available (iOS 10 , *)) {
48+ if ([UNUserNotificationCenter currentNotificationCenter ].delegate != nil ) {
49+ if (UIApplication.sharedApplication .applicationState != UIApplicationStateBackground) {
50+ return ;
51+ }
52+ }
53+ }
54+
4755 [self .countAggregator incrementCount: @" did_receive_remote_notification" ];
4856
4957 // If app was inactive, then handle notification because the user tapped it.
50- if ([[UIApplication sharedApplication ] applicationState ] != UIApplicationStateActive ) {
58+ if ([[UIApplication sharedApplication ] applicationState ] == UIApplicationStateInactive ) {
5159 [self handleNotification: userInfo
5260 withAction: action
5361 appActive: NO
@@ -184,19 +192,26 @@ - (void)handleNotification:(NSDictionary *)userInfo
184192 if (messageId == nil ) {
185193 return ;
186194 }
187-
195+
188196 void (^onContent)(void ) = ^{
189197 if (completionHandler) {
190198 completionHandler (UIBackgroundFetchResultNewData);
191199 }
200+
192201 BOOL hasAlert = userInfo[@" aps" ][@" alert" ] != nil ;
193202 if (hasAlert) {
194- UIApplicationState appState = [[UIApplication sharedApplication ] applicationState ];
195- if (appState != UIApplicationStateBackground) {
196- [self maybePerformNotificationActions: userInfo action: action active: active];
197- }
203+ [self maybePerformNotificationActions: userInfo action: action active: active];
198204 }
199205 };
206+
207+ if (UIApplication.sharedApplication .applicationState == UIApplicationStateInactive && !self.appWasActivatedByReceivingPushNotification ) {
208+ [self setAppWasActivatedByReceivingPushNotification: NO ];
209+ onContent ();
210+ return ;
211+ }
212+ [self setAppWasActivatedByReceivingPushNotification: NO ];
213+
214+ NSLog (@" Push RECEIVED" );
200215
201216 [Leanplum onStartIssued: ^() {
202217 if ([self areActionsEmbedded: userInfo]) {
@@ -214,6 +229,11 @@ - (void)maybePerformNotificationActions:(NSDictionary *)userInfo
214229 action : (NSString *)action
215230 active : (BOOL )active
216231{
232+ // Do not perform the action if the app is in background
233+ if (UIApplication.sharedApplication .applicationState == UIApplicationStateBackground) {
234+ return ;
235+ }
236+
217237 // Don't handle duplicate notifications.
218238 if ([self isDuplicateNotification: userInfo]) {
219239 return ;
@@ -226,6 +246,7 @@ - (void)maybePerformNotificationActions:(NSDictionary *)userInfo
226246 }
227247 }
228248
249+ NSLog (@" Push OPENED" );
229250 LPLog (LPInfo, @" Handling push notification" );
230251 NSString *messageId = [[LPNotificationsManager shared ] messageIdFromUserInfo: userInfo];
231252 NSString *actionName;
@@ -389,10 +410,20 @@ - (void)handleNotificationResponse:(NSDictionary *)userInfo
389410 }
390411 BOOL hasAlert = userInfo[@" aps" ][@" alert" ] != nil ;
391412 if (hasAlert) {
392- [self maybePerformNotificationActions: userInfo action: nil active: NO ];
413+ BOOL active = UIApplication.sharedApplication .applicationState == UIApplicationStateActive;
414+ [self maybePerformNotificationActions: userInfo action: nil active: active];
393415 }
394416 };
395-
417+
418+ if (UIApplication.sharedApplication .applicationState == UIApplicationStateInactive || self.appWasActivatedByReceivingPushNotification ) {
419+ [self setAppWasActivatedByReceivingPushNotification: NO ];
420+ onContent ();
421+ return ;
422+ }
423+ [self setAppWasActivatedByReceivingPushNotification: NO ];
424+
425+ NSLog (@" Push RECEIVED" );
426+
396427 if ([[UIApplication sharedApplication ] applicationState ] != UIApplicationStateActive) {
397428 [Leanplum onStartIssued: ^() {
398429 if ([self areActionsEmbedded: userInfo]) {
@@ -408,6 +439,13 @@ - (void)handleNotificationResponse:(NSDictionary *)userInfo
408439 }
409440}
410441
442+ - (void )handleWillPresentNotification : (NSDictionary *)userInfo
443+ {
444+ if (@available (iOS 10 , *)) {
445+ [self handleWillPresentNotification: userInfo withCompletionHandler: nil ];
446+ }
447+ }
448+
411449// Will be called when user receives notification and app is in foreground iff UNUserNotificationCenterDelegate is implemented in UIApplicationDelegate.
412450// We need to check first whether the action is muted, and depending on it show or silence the action.
413451- (void )handleWillPresentNotification : (NSDictionary *)userInfo
@@ -425,9 +463,12 @@ - (void)handleWillPresentNotification:(NSDictionary *)userInfo
425463 }
426464 BOOL hasAlert = userInfo[@" aps" ][@" alert" ] != nil ;
427465 if (hasAlert) {
428- [self maybePerformNotificationActions: userInfo action: nil active: YES ];
466+ BOOL active = UIApplication.sharedApplication .applicationState == UIApplicationStateActive;
467+ [self maybePerformNotificationActions: userInfo action: nil active: active];
429468 }
430469 };
470+
471+ NSLog (@" Push RECEIVED" );
431472
432473 if (!userInfo[LP_KEY_PUSH_MUTE_IN_APP] && !userInfo[LP_KEY_PUSH_NO_ACTION_MUTE]) {
433474 [Leanplum onStartIssued: ^() {
0 commit comments