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

[iOS] onNotification not triggered when app was closed and come to foreground

Open avencat opened this issue 6 years ago • 7 comments

Hi, I have an issue on iOS, indeed, the onNotification callback is not called when the app is launched and it was not on background or anything, just cold start with a click on a notification.

For information, I do have PushNotification.appStart() on my root componentDidMount method. And this is working when the app is in background and I click on a notification.

[EDIT] It works if I use a

setTimeout(() => {
  PushNotification.appStart();
}, 100);

on my componentDidMount (same for PushNotification.setApplicationIconBadgeNumber(0))

avencat avatar Jun 05 '18 14:06 avencat

I'm using Local Notifications and this is not working for me. onNotification is not called on iOS when the app is in the background (was opened then pressed home button). I'm using the latest version and I call appStart on root component. I also tried the setTimeout mentioned above and no luck.

Here's my scenario:

  1. I'm testing on a real device running iOS 11.4
  2. I'm using localNotification to be scheduled 20 seconds after I open the app.
  3. I open my app
  4. I press the Home button and wait for the notification to arrive.
  5. Notification arrives
  6. I press on the notification and it opens the app
  7. onNotification is not called

I event tried calling showInitialNotification when appState changes and this returns null for the notification. I also put a debug point on the native side to see if getInitialNotification has any data and it doesn't have any data (nil) for both initialNotification and initialLocalNotification.

Here's how I create a notification:

     const payload = {
        title: 'Some Test Title',
        message: 'Test Summary',
        userInfo: {
          target: 'Details',
          key: 'myKeyID',
          name: 'myName'
        };
     PushNotification.localNotificationSchedule({
       ...payload,
       date: new Date(Date.now() + (20 * 1000)) // in 20 secs
     });

The onNotification is called correctly when the app is killed and opened from a cold start (through a scheduled local notification). However, it is not called when the app is in the background (I assuming the app is in the background state when I press the home button and wait for 20 seconds).

I have also configured the Background modes as mentioned in the docs: image

I've spent the past few days debugging iOS notifications trying to solve this 😭 Would be great if @calcazar or anyone else has ideas I can try.

Eyesonly88 avatar Jun 06 '18 04:06 Eyesonly88

@Eyesonly88 did you ever resolve this? I am facing this same exact issue.

ryanpatk avatar Jun 25 '19 22:06 ryanpatk

^ nope. Still an issue.

Eyesonly88 avatar Jun 26 '19 01:06 Eyesonly88

Does this issue have any solution?

intergy avatar Oct 23 '19 06:10 intergy

avencat's workaround doesn't work for me. Here are more details:

  • app in foreground => notification is received
  • app in background (aka closed but not killed) => notification not received (I think this is expected)
  • app was just opened from background (not killed) by tapping a banner notification => notification not received (this is the problem)
  • app was just opened, killed before, by tapping a banner notification => notification is received (as expected)

tolmekian1453 avatar Dec 30 '19 05:12 tolmekian1453

This fixed it for me! https://github.com/zo0r/react-native-push-notification/issues/1272 Setting the activity launch mode like that seems to have wider implications, though. Wonder how safe this is.

tolmekian1453 avatar Dec 30 '19 05:12 tolmekian1453

Found the solution - fortunately, it is most straightforward.

This package depends on @react-native-community/push-notification-ios for the iOS implementation. And in the README to that package, there are clear instructions on adding event handlers to AppDelegate to receive notifications while the app is running. Following the instructions resolved this issue for me.

So what's missing is a link from this README to that README.

leonid-shevtsov avatar Feb 04 '20 06:02 leonid-shevtsov