question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

iOS cannot receive first push notification when open in foreground

See original GitHub issue

Version: 3.1.2 RN version: 0.61.5

Using this version and lines from documentation

Notifications.events().registerNotificationReceivedForeground((notification: Notification, completion: (response: NotificationCompletion) => void) => {
      console.log("Notification Received - Foreground", notification.payload);

      // Calling completion on iOS with `alert: true` will present the native iOS inApp notification.
      completion({alert: true, sound: true, badge: false});
        });

I can only see the push notification alert starts from 2nd push notification, but not the first one.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:6
  • Comments:15

github_iconTop GitHub Comments

2reactions
focuxcommented, May 28, 2020

To fix this bug in RN notifications 3.1.4, go first to RNNotificationsStore.m, then find the initializer and apply this changes (you can also apply this patch using patch-package):

 diff --git a/node_modules/react-native-notifications/lib/ios/RNNotificationsStore.m b/node_modules/react-native-notifications/lib/ios/RNNotificationsStore.m
index d5e953a..18075da 100644
--- a/node_modules/react-native-notifications/lib/ios/RNNotificationsStore.m
+++ b/node_modules/react-native-notifications/lib/ios/RNNotificationsStore.m
@@ -16,8 +16,13 @@ + (instancetype)sharedInstance {
 
 - (instancetype)init {
     self = [super init];
-    _actionCompletionHandlers = [NSMutableDictionary new];
-    _presentationCompletionHandlers = [NSMutableDictionary new];
+    if (!_actionCompletionHandlers){
+        _actionCompletionHandlers = [NSMutableDictionary new];
+    }
+    
+    if (!_presentationCompletionHandlers) {
+        _presentationCompletionHandlers = [NSMutableDictionary new];
+    }
     return self;
 }

I won’t put together a PR because it seems that the maintainers are not accepting/reviewing PRs, however, if @yogevbd is willing to review it, I’ll be glad to do it.

2reactions
samchan1995commented, Feb 21, 2020

My current fix:

  1. Downgrade to 3.1.1
  2. In AppDelegate.m
  • make it conform to UNUserNotificationCenterDelegate
  • add a line [RNNotifications addNativeDelegate:self]; in didFinishLaunchingWithOptions
  • add a function:
- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler {
    completionHandler(UNNotificationPresentationOptionBadge | UNNotificationPresentationOptionSound | UNNotificationPresentationOptionAlert);
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

iOS: cannot receive first push notification when app opened in ...
I'm experiencing the same issue as well. Very first notification isn't received on iOS foreground but succeeding ones are received.
Read more >
Get push notification while App in foreground iOS
Apple says: If you receive local or remote notifications while your app is running in the foreground, you're responsible for passing the ...
Read more >
How to Get Push Notification while iOS App is in Foreground
By default, if an iOS app gets a notification while the app is in the Foreground, the notification banner won't show up.
Read more >
Push Notification Troubleshooting for iOS - Braze
Check that your app is prompting you to allow push notifications. Typically, this prompt will appear upon your first open of the app,...
Read more >
Notifications Not Shown - Mobile Push
Common reasons why Mobile Push notifications are not showing on your device.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found