I’m struggling with getting notifications data when my app is killed on Android and the user taps a notification received for that app. I have tested it using my development build and works just as expected, but it doesn’t work in production app, tapping the notification with the app killed doesn’t get the notification data to the production app.
...
...
Notifications.setNotificationHandler({
handleNotification: async () => ({
shouldShowAlert: true,
shouldPlaySound: true,
shouldSetBadge: false,
}),
});
export default function setupNotifications() {
const {
lastNotificationResponse,
} = useAppContextStore(
({
lastNotificationResponse,
}) => ({
lastNotificationResponse,
}),
);
const notificationListener = useRef();
const responseListener = useRef();
useEffect(() => {
registerForPushNotificationsAsync()
.then(() => null)
.catch(() => null);
processLastNotificationResponse(lastNotificationResponse);
// This listener is fired whenever a notification is received while the app is foregrounded
notificationListener.current = Notifications.addNotificationReceivedListener((notification) => {
notificationReceivedListener(notification);
});
// This listener is fired whenever a user taps on or interacts with a notification (works when app is foregrounded, backgrounded, or killed)
responseListener.current = Notifications.addNotificationResponseReceivedListener((response) => {
notificationResponseReceivedListener(response);
});
return () => {
Notifications.removeNotificationSubscription(notificationListener.current);
Notifications.removeNotificationSubscription(responseListener.current);
};
}, [lastNotificationResponse]);
// This listener is fired whenever a user taps on or interacts with a notification (works when app is killed)
const processLastNotificationResponse = (response) => {
if (response && response.actionIdentifier === Notifications.DEFAULT_ACTION_IDENTIFIER) {
notificationResponseReceivedListener(response);
return;
}
};
// This listener is fired whenever a user taps on or interacts with a notification (works when app is foregrounded, backgrounded, or killed)
const notificationResponseReceivedListener = (response) => {
const message = {
msgId: response.notification.request.content.data.msgId,
title: response.notification.request.content.title,
message: response.notification.request.content.body,
link: response.notification.request.content.data.link,
read: false,
};
processNotification(message, true);
};
...
...
Hi @comfable it seems that the issue has been around for quite some time, fortunately, there’s a workaround until Expo gurus find a definitive solution. This piece of code works as expected on iOS and Android. I’ve tested it myself in my production apps.