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.

onNotification not being called when app was killed on iOS

See original GitHub issue

Bug

I am sending local notifications using react-native-background-fetch. This works good. When the user clicks on a notification I redirect him to a certain screen. This always works in Android. For iOS it is working when the app is in foreground or background. When the app was killed and the user clicks on a notification, the app launches but the onNotification handler is not being called. I call PushNotification.configure from index.js, so timing should not be the issue.

I followed the fix here to get onNotification to work in foreground and background.

Environment info

react-native info output:

 info 
  React Native Environment Info:
    System:
      OS: macOS 10.15.5
      CPU: (12) x64 Intel(R) Core(TM) i9-8950HK CPU @ 2.90GHz
      Memory: 4.00 GB / 32.00 GB
      Shell: 5.7.1 - /bin/zsh
    Binaries:
      Node: 10.16.3 - ~/.nvm/versions/node/v10.16.3/bin/node
      npm: 6.13.6 - ~/.nvm/versions/node/v10.16.3/bin/npm
      Watchman: 4.9.0 - /usr/local/bin/watchman
    SDKs:
      iOS SDK:
        Platforms: iOS 13.5, DriverKit 19.0, macOS 10.15, tvOS 13.4, watchOS 6.2
      Android SDK:
        API Levels: 23, 26, 27, 28, 29
        Build Tools: 23.0.1, 26.0.3, 27.0.3, 28.0.3, 29.0.3
        System Images: android-29 | Google APIs Intel x86 Atom, android-29 | Google APIs Intel x86 Atom_64
    IDEs:
      Xcode: 11.5/11E608c - /usr/bin/xcodebuild
    npmPackages:
      react: 16.8.3 => 16.8.3 
      react-native: 0.59.5 => 0.59.5 
    npmGlobalPackages:
      react-native-cli: 2.0.1

Library version: the development branch of this repo for the latest Android notification action fixes

Steps To Reproduce

  1. Configure the onNotification handler in index.js
  2. Send a push notification while the app is killed
  3. Check if the onNotification handler was called when the notification is clicked

Describe what you expected to happen:

onNotification should be called when coming from Foreground/Background or when the app was killed

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:4
  • Comments:49

github_iconTop GitHub Comments

3reactions
oogushikuncommented, Dec 15, 2020

was this ever resolved? having this issue when I send a silent notification, the app wakes up from a kill state but doesn’t call onNotification. This works well in android

2reactions
sazzadiproliyacommented, Mar 5, 2021

Hi Friends, i have found solution. Please check below methods.

Add this code in below file. AppDelegate.m

  • (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { …

    if (launchOptions != nil) { NSDictionary* userInfo = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey]; if (userInfo != nil) { [self performSelector:@selector(callNotification:) withObject: userInfo afterDelay:10]; } }

… }

create new file methods in same class…

-(void) callNotification: (NSDictionary*) userInfo { [RNCPushNotificationIOS didReceiveRemoteNotification:userInfo]; }

Modify below File from framework RNCPushNotificationIOS.m

  • (void)handleRemoteNotificationReceived:(NSNotification *)notification { … remoteNotification[@“notificationData”] = notification.userInfo[@“notification”]; … }

Update above line on this methods.

Now Check, you will data on onNotification method…

Hope this work for you…

Read more comments on GitHub >

github_iconTop Results From Across the Web

onNotification() is not fired when app is killed - Stack Overflow
i'm using https://github.com/zo0r/react-native-push-notification to get push notifications and the onNotification is working as aspected when ...
Read more >
Handle remote notifications when app is killed
When the app is in the background (not killed), DidReceiveRemoteNotification() is also called, and we can process the notification.
Read more >
Notifications Not Shown - Mobile Push
Android,iOS: In your Device Settings > Notifications > Your App, you can turn on and off push notifications. Make sure push permissions are...
Read more >
Push notification doesn't open specific screen when app is ...
SDK 41. Android / iOS expo-notifications I have a problem where I need to redirect user to specific post when he taps on...
Read more >
IONIC 5 FCM Plugin onNotification not fired when app is killed ...
... FCM Plugin onNotification not fired when app is killed/closed (IOS ... notification body working fine in background and foreground (both ...
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