react-native-notifications icon indicating copy to clipboard operation
react-native-notifications copied to clipboard

can't show notification banner in iOS when app foreground (app opening)

Open mrminhhuy opened this issue 7 years ago • 3 comments

With iOS >= 10, you can make same me:

  • Open AppDelegate.h, add import, also interface: #import <UIKit/UIKit.h> #import <UserNotifications/UNUserNotificationCenter.h> #import <UserNotifications/UNNotification.h> #import <UserNotifications/UNNotificationRequest.h> #import <UserNotifications/UNNotificationContent.h> @interface AppDelegate : UIResponder <UIApplicationDelegate, UNUserNotificationCenterDelegate> @property (nonatomic, strong) UIWindow *window; @end
  • Open AppDelegate.m
  • Add 2 lines in didFinishLaunchingWithOptions (add end function and before 'return YES;' command): UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter]; center.delegate = self;
  • Add 2 functions: `-(void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler{ completionHandler(UNAuthorizationOptionSound | UNAuthorizationOptionAlert | UNAuthorizationOptionBadge); }

// When open app

  • (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)(void))completionHandler { NSMutableDictionary *userData = [NSMutableDictionary dictionaryWithDictionary:response.notification.request.content.userInfo]; [userData setObject:@(1) forKey:@"openedInForeground"]; [RNNotifications didReceiveRemoteNotification:userData]; completionHandler(); }` After, check openedInForeground == 1 in Foreground function in JS code to work some things. Hope this solution can help you!

mrminhhuy avatar Mar 13 '19 10:03 mrminhhuy

I'm also seeing this issue, I checked the code and there appears to be a bug here: https://github.com/wix/react-native-notifications/blob/498dd34b70f4a4f4897d68a108ede51ccaaf9a5b/RNNotifications/RNNotifications.m#L236

The completion handler is passed the wrong enum values - these are the values for asking permission for alerts, sounds and badges, I believe it should be using the presentation values instead e.g.: UNNotificationPresentationOptionAlert

Possibly the enums actually match and this isn't the bug, but these aren't the intended values to use here in any case.

markwilcox avatar Apr 08 '19 12:04 markwilcox

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. If you believe the issue is still relevant, please test on the latest Detox and report back. Thank you for your contributions.

stale[bot] avatar Jul 24 '19 18:07 stale[bot]

Fixed in latest version. thanks

yogevbd avatar Jul 29 '19 11:07 yogevbd