Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Push and local notifications not working when app is in foreground #129

Open
Abhishek-Sankey opened this issue Jun 4, 2020 · 10 comments
Open

Comments

@Abhishek-Sankey
Copy link

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...

@snamstorm
Copy link

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!

@natasha08n
Copy link

natasha08n commented Jun 4, 2020

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
Copy link

natasha08n commented Jun 4, 2020

Hey guys! I found the issue in my code, partially thanks to the issue #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

@Abhishek-Sankey
Copy link
Author

@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.

@natasha08n
Copy link

Did you add some lines to AppDelegate.h file?

@Abhishek-Sankey
Copy link
Author

`#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

@willnaoosmith
Copy link

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

@Abdelrahman-Nabil
Copy link

Did anyone manage to fix this ?

@andrew-stupchuk
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants
@snamstorm @natasha08n @Abdelrahman-Nabil @andrew-stupchuk @Abhishek-Sankey @willnaoosmith and others