6

I have set up push notifications on my React-native app with zo0r / react-native-push-notification . It works on Android, and on iOS when the app is in background mode.

However, the notification does not display when the app is in foreground mode with iOS. I'm aware that I have to handle the notification when in foreground mode but I'd like to display them exactly the same way there are displayed when in background mode.

So I did the following:

import {PushNotificationIOS} from 'react-native';

PushNotification.configure({
...
   onNotification: function(notification) {
      if (notification.foreground) {
         PushNotification.localNotification(notification);
      }
      notification.finish(PushNotificationIOS.FetchResult.NoData);
   },
...
}

But nothing happens, the notification is still not displayed, what am I missing?

Simon
  • 6,025
  • 7
  • 46
  • 98
  • could you please provide sample implementation for this I am facing issue in `foreground` if I use `PushNotification.localNotification` it shows two (default and Locale) – Sagar Feb 23 '21 at 04:06
  • You got the problem on iOS, Android or both of them? – Simon Feb 23 '21 at 21:43
  • I got problem on android, channel (miscellaneous) is getting created in background notification only not on foreground. I need to keep in background to receive it for very first time cause channel getting created in background. – Sagar Feb 25 '21 at 03:34
  • I edited my post to show code that does work – Simon Mar 02 '21 at 21:27

2 Answers2

2

I had to add some configuration inside the ios file AppDelegate.m

Following this gist did make it work.

My code look like:

onNotification(notification) {
  if (isIos) {
    if (
      notification.foreground &&
      (notification.userInteraction || notification.remote)
    ) {
      PushNotification.localNotification(notification);
    }
    notification.finish(PushNotificationIOS.FetchResult.NoData);
  } else {
    if (notification.foreground) {
      PushNotification.localNotification(notification);
    }
  }
},

I also set popInitialNotification to true

Simon
  • 6,025
  • 7
  • 46
  • 98
1

i think you have missing the configuration in AppDelegate.mm

try to check this snippet.

#import <UserNotifications/UserNotifications.h>
#import <RNCPushNotificationIOS.h>
#import <Firebase.h>
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
..............
  [FIRApp configure];
  UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
  center.delegate = self;
...........
  return YES;
}
////Called when a notification is delivered to a foreground app.
-(void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler
{
  completionHandler(UNAuthorizationOptionSound | UNAuthorizationOptionAlert | UNAuthorizationOptionBadge);
}
Mirza Hayat
  • 284
  • 5
  • 11