ios icon indicating copy to clipboard operation
ios copied to clipboard

Push and local notifications not working when app is in foreground

Open Abhishek-Sankey opened this issue 4 years ago • 10 comments

Summary

After updating the library to 1.2, unable to get push or local notifications when app is in the foreground. It's working fine when app is inactive or in background. I think there is some change needed in 'AppDelegate.m' file, method name 'willPresentNotification' is not getting triggered when app receives any push/local notification. I've done some NSLogs in the same method but nothing gets printed on the console. For local notifications, i've implemented 'PushNotificationIOS.presentLocalNotification' but it's also not working.

Environment info

System:
    OS: macOS 10.15.2
    CPU: (4) x64 Intel(R) Core(TM) i5-5350U CPU @ 1.80GHz
    Memory: 46.98 MB / 8.00 GB
    Shell: 3.2.57 - /bin/bash
  Binaries:
    Node: 11.11.0 - /usr/local/bin/node
    npm: 6.7.0 - /usr/local/bin/npm
    Watchman: 4.9.0 - /usr/local/bin/watchman
  SDKs:
    iOS SDK:
      Platforms: iOS 13.2, DriverKit 19.0, macOS 10.15, tvOS 13.2, watchOS 6.1
    Android SDK:
      API Levels: 23, 24, 25, 26, 27, 28, 29
      Build Tools: 23.0.1, 27.0.3, 28.0.2, 28.0.3
      System Images: android-26 | Google APIs Intel x86 Atom
  IDEs:
    Xcode: 11.3/11C29 - /usr/bin/xcodebuild
  npmPackages:
    react: ^16.9.0 => 16.9.0 
    react-native: ^0.61.4 => 0.61.4

Library version: 1.2.0

AppDelegate.m file //Added these lines in didFinishLaunchingWithOptions method UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter]; center.delegate = self; //--------------------

-(void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler { completionHandler(UNAuthorizationOptionSound | UNAuthorizationOptionAlert | UNAuthorizationOptionBadge); }

// Required to register for notifications

  • (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings { [RNCPushNotificationIOS didRegisterUserNotificationSettings:notificationSettings]; } // Required for the register event.
  • (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken { [RNCPushNotificationIOS didRegisterForRemoteNotificationsWithDeviceToken:deviceToken]; } // 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]; } // IOS 4-10 Required for the localNotification event.
  • (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification { [RNCPushNotificationIOS didReceiveLocalNotification:notification]; } // IOS 10+ Required for localNotification event
  • (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)(void))completionHandler { [RNCPushNotificationIOS didReceiveNotificationResponse:response]; completionHandler(); }

On React side for local notifications if (Platform.OS === 'ios') { PushNotificationIOS.presentLocalNotification({ alertBody: message, alertTitle: title, userInfo: data }); } else { PushNotification.localNotification({ /* Android Only Properties */ ticker: "My Notification Ticker", // (optional) autoCancel: true, // (optional) default: true ....... )}; }

Thanks in advance...

Abhishek-Sankey avatar Jun 04 '20 05:06 Abhishek-Sankey

Am also facing this issue. Have followed the docs and triple checked everything.

Am able to receive push notifications when app is closed or app is in background. Not able to receive push notifications or local notification when app is on foreground.

Have tried all sorts of work around but nothing works.

Appreciate if someone has some insights on this. Thanks!

snamstorm avatar Jun 04 '20 06:06 snamstorm

I have the same situation. Local notifications don't appear when the app is in foreground. Will be so grateful to get some help on this. Thanks!

natasha08n avatar Jun 04 '20 12:06 natasha08n

Hey guys! I found the issue in my code, partially thanks to the issue https://github.com/react-native-community/push-notification-ios/issues/63

Let me share with you final results with working code:

What I added to AppDelegate.m

#import "RNNotifications.h"
#import <UserNotifications/UserNotifications.h>
#import <RNCPushNotificationIOS.h>
...

In didFinishLaunchingWithOptions added

// Define UNUserNotificationCenter
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
    center.delegate = self;

And at the end of the file

// Required to register for notifications
- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings
{
 [RNCPushNotificationIOS didRegisterUserNotificationSettings:notificationSettings];
}

// Required for the register event.
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
 [RNCPushNotificationIOS didRegisterForRemoteNotificationsWithDeviceToken:deviceToken];
}

// 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];
}

// 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];
}

// Manage notifications while app is in the foreground
-(void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler
{
  completionHandler(UNAuthorizationOptionSound | UNAuthorizationOptionAlert | UNAuthorizationOptionBadge);
}

Please let me know if it helps you! Now everything works on my side. I have installed this package + package from wix just to choose

natasha08n avatar Jun 04 '20 13:06 natasha08n

@natasha08n thanks for your help but I've implemented the same methods as you can see in the issue details and it's not working for me.

Abhishek-Sankey avatar Jun 05 '20 03:06 Abhishek-Sankey

Did you add some lines to AppDelegate.h file?

natasha08n avatar Jun 05 '20 18:06 natasha08n

`#import <UserNotifications/UNUserNotificationCenter.h> #import <UIKit/UIKit.h>

@interface AppDelegate : UIResponder <UIApplicationDelegate, UNUserNotificationCenterDelegate> @property (nonatomic, strong) UIWindow *window;

@end`

@natasha08n this is my AppDelegate.h file

Abhishek-Sankey avatar Jun 08 '20 03:06 Abhishek-Sankey

Same here, but only the second time i open the app. (When i kill the app and re-open)

willnaoosmith avatar Jun 09 '20 14:06 willnaoosmith

Did anyone manage to fix this ?

Abdelrahman-Nabil avatar Jan 04 '21 16:01 Abdelrahman-Nabil

In my case foreground notifications doesn't work due to data i passed to userInfo prop. If you pass whole notification.data prop, you need to omit additional fields, that passed by fcm (e.x. 'gcm.message_id', 'google.c.a.e'). Just comment userInfo prop to check if you have the same problem as mine

andrew-stupchuk avatar Jan 20 '21 13:01 andrew-stupchuk

The local pushnotifications in iOS background not working on my side too. Forground local notifications working fine. Have you ever tested on simulator? On simulator the both foreground and background working fine. Who can figure out this? Background notifications on real phone, not working :((

chriswilsondev910 avatar May 21 '21 13:05 chriswilsondev910