ios: addEventListener('notification', ...) is not working - when tapping the notification while the app in the background
Bug
I’m trying to use the module and this is what happens:
- register event listener is working
PushNotificationIOS.addEventListener('register', token => {
storageManager.storeDeviceToken(token)
})
- getInitialNotification works for me too
PushNotificationIOS.getInitialNotification().then(payload => { ....
....
payload.finish(PushNotificationIOS.FetchResult)
}
})
3. ‘notification’ event listener is not working
PushNotificationIOS.addEventListener('notification', this.iOSNotificationReceived.bind(this))
...
...
iOSNotificationReceived(data) {
console.log(data)
}
I’m not receiving anything here, it is very strange since I implemented all the needed instructions in documentations and the register is working and I’m receiving notifications to my device when the device is killed using getInitialNotification.
Environment info
react-native info output:
“react”: “16.9.0”,
“react-native”: “0.61.5”,
// paste it here
Library version: x.x.x “@react-native-community/push-notification-ios”: “^1.1.1”
Steps To Reproduce
- register event listener is working
PushNotificationIOS.addEventListener('register', token => {
storageManager.storeDeviceToken(token)
})
- getInitialNotification works for me too
PushNotificationIOS.getInitialNotification().then(payload => { ....
....
payload.finish(PushNotificationIOS.FetchResult)
}
})
3. ‘notification’ event listener is not working
PushNotificationIOS.addEventListener('notification', this.iOSNotificationReceived.bind(this))
...
...
iOSNotificationReceived(data) {
console.log(data)
}
Describe what you expected to happen:
To receive notifications when the app is working on the background using PushNotificationIOS.addEventListener(‘notification’, …)
Reproducible sample code
About this issue
- Original URL
- State: open
- Created 4 years ago
- Reactions: 10
- Comments: 42
Solved my issue. didReceiveRemoteNotification was firing but didReceiveNotificationResponse of UserNotificationCenter delegate was never firing when tapping the notification. Note that it WAS firing for a local notification created by the app, but NOT a remote push notification.
I’ve just spent several days trying everything I can think of to figure out how to detect when that remote notification was tapped and get the payload. In the end what fixed it was changing this in AppDelegate.m:
To:
Basically setting center.delegate after requestAuthorizationWithOptions then changed the behaviour.
Now, didReceiveNotificationResponse fires when the remote notification is tapped. Somewhat counter intuitively it is treated as a localNotification (but I guess that makes sense) so I handle it in:
PushNotificationIOS.addEventListener('localNotification' ...Note that for didReceiveRemoteNotification to also fire, content-available: 1 must be set in the payload. Either way didReceiveNotificationResponse was not firing until making the above change.
Hope this helps someone. I had been pulling my hair out for about 4 days!
Thanks @dominiczaq
But no, I mean when I receive a notification by APNs while the app is running in the background and tap on the notification to open the app. When I tap on the notification, the app opens and the nothing happens then.
I tried to remove all the code inside the notification event listener and added an alert and console log to understand if it enters the scope or not, but unfortunately I don’t see anything in the terminal and no alert.
I hope this was clear.
storeIOSToken is working
PushNotificationIOS.getInitialNotification().then(payload => { is also working when the app is killed
PushNotificationIOS.addEventListener(‘notification’, function (payload) { ------> not working
Hi,
I struggled with this issue and found the solution for my app.
Actually the issue is that two different functions are triggered in the AppDelegate.m :
(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler(void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)(void))completionHandlerSo in my case I replaced :
(void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)(void))completionHandler { [RNCPushNotificationIOS didReceiveNotificationResponse:response]; }by
(void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)(void))completionHandler { NSMutableDictionary *userData = [NSMutableDictionary dictionaryWithDictionary:response.notification.request.content.userInfo]; [userData setObject:@YES forKey:@"userInteraction"]; [RNCPushNotificationIOS didReceiveRemoteNotification:userData]; }And the notification listener is called when notification is clicked and the app is in background.
After updating to 1.2.0 I have similar issues - not receiving notifications from ‘notification’ event.
I’ve tried listening on ‘localNotification’ event as @mtettmar suggested (https://github.com/react-native-community/push-notification-ios/issues/107#issuecomment-627365701) and behold, my notifications started appearing again.
After some digging I believe changes from https://github.com/react-native-community/push-notification-ios/commit/15d1085dc73d4defef2fcf1321db37fb67b78053#diff-501451de6931154229be18b351eb277d were the culprit. Now all notifications are coming through
didReceiveNotificationResponse:withCompletionHandlerwhich callslocalNotificationReceivedevent in RNCPushNotificationIOS.m (https://developer.apple.com/documentation/usernotifications/handling_notifications_and_notification-related_actions?language=objc - didReceiveNotificationResponse:withCompletionHandler is fired on push tap)So in summary I believe you would only receive notifications from ‘localNotification’ event for both remote and local pushes.
@codal-mpawar It all appears to work with just this code. Normally I would expect you to need to use get initial notification function but this appears to work and you get ALL the notification data.
@mtettmar your comment fixes the issue for me, however, this change results in my app requesting push notification permissions on app launch, rather than waiting until a user logs in (which is the expected behavior in the app). Is there a way to achieve the same result without forcing the request for push permissions on app launch? I’m not that familiar with iOS-specific code, so having a little trouble figuring it out. Seems like maybe
didRegisterForRemoteNotificationsWithDeviceTokenwould be the right place for this?@cogell not sure if that what I need.
The thing is - I need to have event handler when app in foreground and notification received. Now event fires only if I click on notification ( notification and localNotification handlers works well in this case).
Thanks for help!
@dominiczaq do you have any solution for the issue, it is happening when I tap the notification while the app in the background and then I just arrive to the home page and nothing happens. I worked in the react-native library before and was able to catch the payload when I tap on the notification inside the ‘notification’ handler.
Will be happy for your help
@dominiczaq yes, I know about this, I’m tapping in the notification and not receiving anything inside the ‘notification’ event listener, I added a console log to print the payload when tapping the notification and moving to foreground and nothing happened.