addEventListener for notification not called
React Native Version: 0.73.6
Reproduction Steps:
- Background App
- Send push notification
- Tap push notification Using the following code, "entered" is never logged.
const onRemoteNotification = (notification) => {
console.log('entered')
// Use the appropriate result based on what you needed to do for this notification
const result = PushNotificationIOS.FetchResult.NoData;
notification.finish(result);
};
useEffect(() => {
const type = 'notification';
PushNotificationIOS.addEventListener(type, onRemoteNotification);
return () => {
PushNotificationIOS.removeEventListener(type);
};
});
Notes:
- Remote notification does show up on device; so I don't think it's a permission issue
- Tried with both type = "notification" and "localNotification"
the same here
onRemoteNotification should get triggered if/when the app is in foreground.
PushNotificationIOS.getInitialNotification() is what you're looking for (if you're coming from background i.e opening the app by tapping on the notification)
useEffect(() => {
// Check if the app was opened by a notification
PushNotificationIOS.getInitialNotification().then(notification => {
if (notification) {
const notificationData = notification.getData();
console.log(notificationData);
}
});
}, []);
I'm able to get that working when the app is launched (i.e. after I force close it) from the notification, but not when it's backgrounded.
I'm able to get that working when the app is launched (i.e. after I force close it) from the notification, but not when it's background.
just realized this as well today, its working fine on android in state Foreground, Killed, and Background but on iOS when app is in Background state, and user tap the notification, it just open the app and do nothing~
did found found any workaround on this @jzinrh @saibbyweb ?
I am facing a similar issue with setNotificationCategories notifications. Below is the code I am using to set reply notification, but I am able to get reply text in foreground only when the app is killed, and I reply from notification, then it does not log anything. Any help in this case would be appreciated. Thanks.
`if (Platform.OS == 'ios') {
PushNotificationIOS.setNotificationCategories([
{
id: 'INCOMING_SMS_CATEGORY',
actions: [
{id: 'open', title: 'Open', options: {foreground: true}},
{
id: 'ignore',
title: 'Desruptive',
options: {foreground: true, destructive: true},
},
{
id: 'text',
title: 'Text Input',
options: {foreground: true},
textInput: {buttonTitle: 'Send'},
},
],
},
]);
PushNotificationIOS.getInitialNotification().then(function (notification) {
logMessage(`We have received a push notification : 2222`, notification);
if (notification != null) {
logMessage('app open by notif tap ', notification)
if (notification && notification._data && notification._data.notification_type) {
let notificationType = notification._data.notification_type
if (notificationType == 'xmpp_push') {
CallSession.setState({ initialTabRoute: 'Chat' });
}else if(notificationType == "incoming_sms_push"){
CallSession.setState({ initialTabRoute: 'SMS' });
}
}
}
});
PushNotificationIOS.addEventListener("register", async (token) => {
logMessage('apns register token ', token);
await AsyncStorage.setItem("_apnsToken", token)
})
PushNotificationIOS.addEventListener("localNotification", (notification) => {
logMessage('local notification recieved ', notification.toString())
})
PushNotificationIOS.addEventListener("registrationError", (error) => {
logMessage("register error ", error.message.toString())
})
PushNotificationIOS.addEventListener("notification", async (notification) => {
logMessage(`We have received a push notification : `, notification);
notification.finish(PushNotificationIOS.FetchResult.NoData)
logMessage("notification_received_PNIOS => ", notification)
})
}`