react-native-push-notification-CE
react-native-push-notification-CE copied to clipboard
[iOS] onNotification not triggered when app was closed and come to foreground
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)
)
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:
- I'm testing on a real device running iOS 11.4
- I'm using
localNotification
to be scheduled 20 seconds after I open the app. - I open my app
- I press the Home button and wait for the notification to arrive.
- Notification arrives
- I press on the notification and it opens the app
-
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:
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 did you ever resolve this? I am facing this same exact issue.
^ nope. Still an issue.
Does this issue have any solution?
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)
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.
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.