calcazar / react-native-push-notification-CE

The community edition of the push notification library (by Zo0r) for applications made with React Native
MIT License
52 stars 23 forks source link

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

Open avencat opened 6 years ago

avencat commented 6 years ago

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

Eyesonly88 commented 6 years ago

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.

ryanpatk commented 5 years ago

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

Eyesonly88 commented 5 years ago

^ nope. Still an issue.

intergy commented 5 years ago

Does this issue have any solution?

tolmekian1453 commented 4 years ago

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

tolmekian1453 commented 4 years ago

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.

leonid-shevtsov commented 4 years ago

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.