react-native-push-notification icon indicating copy to clipboard operation
react-native-push-notification copied to clipboard

onNotification Not Trigerred

Open ElieSoft opened this issue 2 years ago • 13 comments

Dear Team,

We think that there is an issue within the react-native-push-notification library. Kindly read the case below.

I am using Local PushNotification in order to set alarms within my application. Everything works fine but there is a certain case where: 1 - The application is in "Killed" state. The notification is received. User clicks on notification. The app starts, go to Home screen, the onNotification function is trigerred and the app is redirected to the a specific screen. 2 - After that if the application is set in a "Background" state, and the notification is received, when the User clicks on that second notification the app is set in "Active" state in foreground but the onNotification function is not trigerred. The app stay at the Home screen and nothing happens.

Please your prompt action is really appreciated.

Best Regards, Eli

ElieSoft avatar Jul 31 '21 06:07 ElieSoft

@ElieSoft Did you find any work around ? We are also facing same issue. It was working couple days before.

rabindrachhachan avatar Aug 03 '21 11:08 rabindrachhachan

Hello Rabin Not yet. Someone told me that it's a bug related to the library react-native-push-notification.

What do you think ?

ElieSoft avatar Aug 03 '21 17:08 ElieSoft

Hi @ElieSoft This is probably related to your configuration. Please check the exemple project or provide a reproducible exemple. Regards.

Dallas62 avatar Aug 03 '21 18:08 Dallas62

Hi @Dallas62

Kindly could you plese provide me with the example project link ?

Best Regards,

ElieSoft avatar Aug 03 '21 19:08 ElieSoft

In the Example folder https://github.com/zo0r/react-native-push-notification/tree/master/example

Dallas62 avatar Aug 03 '21 19:08 Dallas62

Hi @Dallas62,

I too have been struggling with the exact issue @ElieSoft has reported for a few days now. I've had a read through of multiple threads including #1453 and nothing has worked.

My app delegate file looks like this:

/**
 * Copyright (c) 2015-present, Facebook, Inc.
 *
 * This source code is licensed under the MIT license found in the
 * LICENSE file in the root directory of this source tree.
 */

#import "AppDelegate.h"
#import "AVFoundation/AVFoundation.h"
#import <Firebase.h> // firebase
#import "RNFirebaseNotifications.h" // firebase notifications
#import <UserNotifications/UserNotifications.h>
#import <RNCPushNotificationIOS.h>
#import "ReactNativeConfig.h"
#import <React/RCTBridge.h>
#import <React/RCTBundleURLProvider.h>
#import <React/RCTRootView.h>
#import <React/RCTLinkingManager.h>
#import "redacted-Swift.h"

#ifdef FB_SONARKIT_ENABLED
#import <FlipperKit/FlipperClient.h>
#import <FlipperKitLayoutPlugin/FlipperKitLayoutPlugin.h>
#import <FlipperKitUserDefaultsPlugin/FKUserDefaultsPlugin.h>
#import <FlipperKitNetworkPlugin/FlipperKitNetworkPlugin.h>
#import <SKIOSNetworkPlugin/SKIOSNetworkAdapter.h>
#import <FlipperKitReactPlugin/FlipperKitReactPlugin.h>

static void InitializeFlipper(UIApplication *application) {
  FlipperClient *client = [FlipperClient sharedClient];
  SKDescriptorMapper *layoutDescriptorMapper = [[SKDescriptorMapper alloc] initWithDefaults];
  [client addPlugin:[[FlipperKitLayoutPlugin alloc] initWithRootNode:application withDescriptorMapper:layoutDescriptorMapper]];
  [client addPlugin:[[FKUserDefaultsPlugin alloc] initWithSuiteName:nil]];
  [client addPlugin:[FlipperKitReactPlugin new]];
  [client addPlugin:[[FlipperKitNetworkPlugin alloc] initWithNetworkAdapter:[SKIOSNetworkAdapter new]]];
  [client start];
}
#endif

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
  #ifdef FB_SONARKIT_ENABLED
    InitializeFlipper(application);
  #endif

  [FIRApp configure]; // firebase
  [RNFirebaseNotifications configure]; // firebase notifications

  // React Bridge and Root view setup
  RCTBridge *bridge = [[RCTBridge alloc] initWithDelegate:self launchOptions:launchOptions];
  RCTRootView *rootView = [[RCTRootView alloc] initWithBridge:bridge moduleName:@"redacted" initialProperties:nil];

  [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryAmbient error:nil];

  // Define UNUserNotificationCenter
  UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
  
  [center requestAuthorizationWithOptions:(UNAuthorizationOptionAlert + UNAuthorizationOptionSound + UNAuthorizationOptionBadge)completionHandler:^(BOOL granted, NSError * _Nullable error) {
    if (granted) {
      center.delegate = self; 
    }
  }];

  if (@available(iOS 13.0, *)) {
      rootView.backgroundColor = [UIColor systemBackgroundColor];
  } else {
      rootView.backgroundColor = [UIColor whiteColor];
  }

  self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];

  // View controllers initialising
  UIViewController *rootViewController = [UIViewController new];
  UINavigationController *rootNavController = [[UINavigationController alloc] initWithRootViewController:rootViewController];
  rootViewController.view = rootView;
  [rootNavController setNavigationBarHidden:YES];
  self.window.rootViewController = rootViewController;

  [self.window makeKeyAndVisible];

  return YES;
}

- (NSURL *)sourceURLForBridge:(RCTBridge *)bridge {
  #ifdef DEBUG
      return [[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:@"index" fallbackResource:nil];
  #else
      return [[NSBundle mainBundle] URLForResource:@"main" withExtension:@"jsbundle"];
  #endif
}

// PUSH NOTIFICATION CONFIG

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
    [RNCPushNotificationIOS didRegisterForRemoteNotificationsWithDeviceToken:deviceToken];
}

- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings {
    [RNCPushNotificationIOS didRegisterUserNotificationSettings:notificationSettings];
}

// Required for the notification event. You must call the completion handler after handling the remote notification.
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
  [RNCPushNotificationIOS didReceiveRemoteNotification:userInfo fetchCompletionHandler:completionHandler];
}

// Required for the registrationError event.
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error
{
 [RNCPushNotificationIOS didFailToRegisterForRemoteNotificationsWithError:error];
}

// Required for localNotification event
// - (void)userNotificationCenter:(UNUserNotificationCenter *)center
// didReceiveNotificationResponse:(UNNotificationResponse *)response
//          withCompletionHandler:(void (^)(void))completionHandler
// {
//   [RNCPushNotificationIOS didReceiveNotificationResponse:response];
//   completionHandler();
// }


// IOS 10+ Required for localNotification event
- (void)userNotificationCenter:(UNUserNotificationCenter *)center
didReceiveNotificationResponse:(UNNotificationResponse *)response
         withCompletionHandler:(void (^)(void))completionHandler
{
  [RNCPushNotificationIOS didReceiveNotificationResponse:response];
  completionHandler();
}


// IOS 4-10 Required for the localNotification event.
// - (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
// {
//  [RNCPushNotificationIOS didReceiveLocalNotification:notification];
// }


// Called when a notification is delivered to a foreground app.
-(void)userNotificationCenter:(UNUserNotificationCenter *)center
      willPresentNotification:(UNNotification *)notification
        withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler
{
  // Still call the JS onNotification handler so it can display the new message right away
  // NSDictionary *userInfo = notification.request.content.userInfo;
  // [RNCPushNotificationIOS didReceiveRemoteNotification:userInfo
  //                               fetchCompletionHandler:^void (UIBackgroundFetchResult result){}];

  // allow showing foreground notifications
  completionHandler(UNNotificationPresentationOptionSound | UNNotificationPresentationOptionAlert | UNNotificationPresentationOptionBadge);
  // or if you wish to hide all notification while in foreground replace it with
  // completionHandler(UNNotificationPresentationOptionNone);
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    [application registerUserNotificationSettings:
    [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert)
                                       categories:nil]];
    [application registerForRemoteNotifications];
}

// END NOTIFICATION CONFIG

// firebase notifications
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
  [[RNFirebaseNotifications instance] didReceiveLocalNotification:notification];
}

// Universal Links
- (BOOL)application:(UIApplication *)application
   openURL:(NSURL *)url
   options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options
{
  return [RCTLinkingManager application:application openURL:url options:options];
}

- (BOOL)application:(UIApplication *)application continueUserActivity:(NSUserActivity *)userActivity
 restorationHandler:(void (^)(NSArray * _Nullable))restorationHandler
{
 return [RCTLinkingManager application:application
                  continueUserActivity:userActivity
                    restorationHandler:restorationHandler];
}
// End Universal Links

@end

I've checked the example project and mapped all the required configuration to what I have in my project and it checks out so really don't know what I'm missing.

FYI, I'm currently testing remote push notifications by dragging an APNS file onto an IOS simulator. I'm quite new to mobile development, so was wondering if testing on a simulator is causing the issue (and what the general best practice is)?

Any help would be appreciated.

aliasmac avatar Aug 04 '21 11:08 aliasmac

@Dallas62 So I ran the example project and it works a dream. You have any ideas where in my configuration things might be breaking?

aliasmac avatar Aug 04 '21 15:08 aliasmac

Check the AndroidManifest and MainActivity

Dallas62 avatar Aug 04 '21 15:08 Dallas62

Check the AndroidManifest and MainActivity

Right but the issue is specifically for IOS, would also running firebase notifications (RNFirebaseNotifications.h) impact the behaviour of the lib?

aliasmac avatar Aug 04 '21 16:08 aliasmac

So check the AppDelegate. This package doesn't maintain the iOS version as mentioned in #1453.

Dallas62 avatar Aug 04 '21 16:08 Dallas62

Same happened with me , though i was able to resolve this by adding launchmode to singletask in android manifest

arunahuja94 avatar Aug 07 '21 09:08 arunahuja94

Check the AndroidManifest and MainActivity

@Dallas62 Please can you specify what I should exactly check within AndroidManifest and MainActivity. For example in my AndroidManifest, the following lines of code are missing:

action android:name="android.intent.action.QUICKBOOT_POWERON" action android:name="com.htc.intent.action.QUICKBOOT_POWERON"

ElieSoft avatar Aug 07 '21 22:08 ElieSoft

Hi @arunahuja94 , could you please share the code snippet ? I am not able to fix this issue. thank you

Robopicker avatar May 19 '22 07:05 Robopicker