From 73e75faa93b5cd05d850d8e5e7f3faa8247390aa Mon Sep 17 00:00:00 2001 From: Javier Anton Date: Sun, 17 Jan 2021 22:39:32 +0000 Subject: [PATCH 1/4] Aligned iOS local fg notification behavior with Android (localNotificationReceived only triigers when tapped, not when shown) --- .../nativeSources/CodenameOne_GLAppDelegate.m | 21 ++++++++++++------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/Ports/iOSPort/nativeSources/CodenameOne_GLAppDelegate.m b/Ports/iOSPort/nativeSources/CodenameOne_GLAppDelegate.m index 4ce4cd9448..a081def459 100644 --- a/Ports/iOSPort/nativeSources/CodenameOne_GLAppDelegate.m +++ b/Ports/iOSPort/nativeSources/CodenameOne_GLAppDelegate.m @@ -394,8 +394,6 @@ - (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNot { CN1Log(@"Received local notification while running: %@", notification); - NSString* alertValue = [notification.request.content.userInfo valueForKey:@"__ios_id__"]; - com_codename1_impl_ios_IOSImplementation_localNotificationReceived___java_lang_String(CN1_THREAD_GET_STATE_PASS_ARG fromNSString(CN1_THREAD_GET_STATE_PASS_ARG alertValue)); if (completionHandler != nil) { if ([notification.request.content.userInfo valueForKey:@"foreground"] != NULL) { completionHandler(UNNotificationPresentationOptionAlert); @@ -419,8 +417,20 @@ - (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNot - (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler { +#ifdef CN1_INCLUDE_NOTIFICATIONS + UIApplicationState state = [[UIApplication sharedApplication] applicationState]; + if(state == UIApplicationStateActive) + { + if( [response.notification.request.content.userInfo valueForKey:@"__ios_id__"] != NULL) + { + CN1Log(@"Received local notification while running: %@", response.notification); - + NSString* alertValue = [response.notification.request.content.userInfo valueForKey:@"__ios_id__"]; + if ([response.notification.request.content.userInfo valueForKey:@"foreground"] != NULL) + com_codename1_impl_ios_IOSImplementation_localNotificationReceived___java_lang_String(CN1_THREAD_GET_STATE_PASS_ARG fromNSString(CN1_THREAD_GET_STATE_PASS_ARG alertValue)); + } + } +#endif #ifdef INCLUDE_CN1_PUSH NSLog( @"Handle push from background or closed" ); // if you set a member variable in didReceiveRemoteNotification, you will know if this is from closed or background @@ -589,11 +599,6 @@ -(void)application:(UIApplication*)application didChangeStatusBarFrame:(CGRect)o - (void)application:(UIApplication*)application didReceiveLocalNotification:(UILocalNotification*)notification { CN1Log(@"Received local notification while running: %@", notification); - if( [notification.userInfo valueForKey:@"__ios_id__"] != NULL) - { - NSString* alertValue = [notification.userInfo valueForKey:@"__ios_id__"]; - com_codename1_impl_ios_IOSImplementation_localNotificationReceived___java_lang_String(CN1_THREAD_GET_STATE_PASS_ARG fromNSString(CN1_THREAD_GET_STATE_PASS_ARG alertValue)); - } } #ifndef CN1_USE_ARC From e4090f9b82f173dbd4c3f701a14af0e729e8fac2 Mon Sep 17 00:00:00 2001 From: Javier Anton Date: Mon, 18 Jan 2021 00:46:54 +0000 Subject: [PATCH 2/4] Update No need to check for Active; that would prevent local foreground notifications being actionable when the app has gone back to background --- .../nativeSources/CodenameOne_GLAppDelegate.m | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/Ports/iOSPort/nativeSources/CodenameOne_GLAppDelegate.m b/Ports/iOSPort/nativeSources/CodenameOne_GLAppDelegate.m index a081def459..4526d9a69b 100644 --- a/Ports/iOSPort/nativeSources/CodenameOne_GLAppDelegate.m +++ b/Ports/iOSPort/nativeSources/CodenameOne_GLAppDelegate.m @@ -418,17 +418,12 @@ - (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNot - (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler { #ifdef CN1_INCLUDE_NOTIFICATIONS - UIApplicationState state = [[UIApplication sharedApplication] applicationState]; - if(state == UIApplicationStateActive) + if( [response.notification.request.content.userInfo valueForKey:@"__ios_id__"] != NULL) { - if( [response.notification.request.content.userInfo valueForKey:@"__ios_id__"] != NULL) - { - CN1Log(@"Received local notification while running: %@", response.notification); - - NSString* alertValue = [response.notification.request.content.userInfo valueForKey:@"__ios_id__"]; - if ([response.notification.request.content.userInfo valueForKey:@"foreground"] != NULL) - com_codename1_impl_ios_IOSImplementation_localNotificationReceived___java_lang_String(CN1_THREAD_GET_STATE_PASS_ARG fromNSString(CN1_THREAD_GET_STATE_PASS_ARG alertValue)); - } + CN1Log(@"Received local notification while running: %@", response.notification); + NSString* alertValue = [response.notification.request.content.userInfo valueForKey:@"__ios_id__"]; + if ([response.notification.request.content.userInfo valueForKey:@"foreground"] != NULL) + com_codename1_impl_ios_IOSImplementation_localNotificationReceived___java_lang_String(CN1_THREAD_GET_STATE_PASS_ARG fromNSString(CN1_THREAD_GET_STATE_PASS_ARG alertValue)); } #endif #ifdef INCLUDE_CN1_PUSH From 9252be8f382cb7bf0037b052e59bcb98ec0d9f7a Mon Sep 17 00:00:00 2001 From: Javier Anton Date: Mon, 18 Jan 2021 01:02:52 +0000 Subject: [PATCH 3/4] Edited log message --- Ports/iOSPort/nativeSources/CodenameOne_GLAppDelegate.m | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Ports/iOSPort/nativeSources/CodenameOne_GLAppDelegate.m b/Ports/iOSPort/nativeSources/CodenameOne_GLAppDelegate.m index 4526d9a69b..822ba719c4 100644 --- a/Ports/iOSPort/nativeSources/CodenameOne_GLAppDelegate.m +++ b/Ports/iOSPort/nativeSources/CodenameOne_GLAppDelegate.m @@ -420,7 +420,7 @@ - (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNoti #ifdef CN1_INCLUDE_NOTIFICATIONS if( [response.notification.request.content.userInfo valueForKey:@"__ios_id__"] != NULL) { - CN1Log(@"Received local notification while running: %@", response.notification); + CN1Log(@"Tapped on locally initiated notification: %@", response.notification); NSString* alertValue = [response.notification.request.content.userInfo valueForKey:@"__ios_id__"]; if ([response.notification.request.content.userInfo valueForKey:@"foreground"] != NULL) com_codename1_impl_ios_IOSImplementation_localNotificationReceived___java_lang_String(CN1_THREAD_GET_STATE_PASS_ARG fromNSString(CN1_THREAD_GET_STATE_PASS_ARG alertValue)); From aba6738a5a36e4740c984665563b2173e3cec09c Mon Sep 17 00:00:00 2001 From: Javier Anton Date: Wed, 20 Jan 2021 16:49:50 +0000 Subject: [PATCH 4/4] Applied suggested review changes --- Ports/iOSPort/nativeSources/CodenameOne_GLAppDelegate.m | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/Ports/iOSPort/nativeSources/CodenameOne_GLAppDelegate.m b/Ports/iOSPort/nativeSources/CodenameOne_GLAppDelegate.m index 822ba719c4..19d200403f 100644 --- a/Ports/iOSPort/nativeSources/CodenameOne_GLAppDelegate.m +++ b/Ports/iOSPort/nativeSources/CodenameOne_GLAppDelegate.m @@ -594,6 +594,11 @@ -(void)application:(UIApplication*)application didChangeStatusBarFrame:(CGRect)o - (void)application:(UIApplication*)application didReceiveLocalNotification:(UILocalNotification*)notification { CN1Log(@"Received local notification while running: %@", notification); + if( [notification.userInfo valueForKey:@"__ios_id__"] != NULL) + { + NSString* alertValue = [notification.userInfo valueForKey:@"__ios_id__"]; + com_codename1_impl_ios_IOSImplementation_localNotificationReceived___java_lang_String(CN1_THREAD_GET_STATE_PASS_ARG fromNSString(CN1_THREAD_GET_STATE_PASS_ARG alertValue)); + } } #ifndef CN1_USE_ARC