Please provide the following:
- SDK Version: 47
- Platforms(Android/iOS/web/all): Android
- Add the appropriate “Tag” based on what Expo library you have a question on. #expo-notifications
My Code:
// Push Notifications
const lastNotificationResponse = Notifications.useLastNotificationResponse();
Notifications.setNotificationHandler({
handleNotification: async () => ({
shouldShowAlert: true,
shouldPlaySound: true,
shouldSetBadge: true
})
});
useEffect(() => {
if (lastNotificationResponse && lastNotificationResponse.actionIdentifier === Notifications.DEFAULT_ACTION_IDENTIFIER )
{
dispatch(NotificationActions.getNotifications(undefined, false));
handleNotificationData(lastNotificationResponse);
console.log(‘lastNotificationResponse’, lastNotificationResponse);
}}, [lastNotificationResponse]);
useEffect(() => {
// when a notification is received or interacted with this two methods handle the data
// Will use there responses for future navigation
notificationListener.current = Notifications.addNotificationReceivedListener((notification) => {});
// when clicked on notification, this listner gets called
responseListener.current = Notifications.addNotificationResponseReceivedListener((response) => { handleNotificationData(response); });
return () => {
Notifications.removeNotificationSubscription(notificationListener); Notifications.removeNotificationSubscription(responseListener);
}; }, );
const handleNotificationData = (response: NotificationResponse) => {
const resp: NotificationResponse = response;
const request: NotificationRequest = resp.notification.request;
const content: NotificationContent = request.content;
const data: ContentData = content.data; const type = data?.type;
const profile = data?.profile;
const firstName = profile?.firstName;
const lastName = profile?.lastName;
const image = profile?.image;
const type=profile.type;
// data for user contact
var contact = {
firstName: firstName,
image: image,
lastName: lastName
type: type
};
// adding contact details in redux
dispatch(ContactsActions.setCurrentContact(contact));
if (type === NotificationType.REQUEST_SENT) {
// navigation to handle sent request
navigate.navigate(strings.contact_profile_modal_scene_name, {contact});
} else if (type === NotificationType.REQUEST_ACCEPTED && state === ContactState.SHARE) {
// navigation to handle accepted request from quick add
navigate.navigate(strings.contact_share_options_scene_name, {action: navigationActions.NAV_SHARE_PRESET});
} else if (type === NotificationType.REQUEST_ACCEPTED || type === NotificationType.BIRTHDAY) {
// navigation to handle accepted request
navigate.navigate(strings.contact_profile_page_scene_name, {contact});
}
};
Summary:
I have implemented functionality to handle the push notifications according to their type. Whenever the user receives a push notification and clicks on it, the listener gets triggered and according to the notification type, I am navigating the user to the different screens in the app.
This is working fine in cases when an app is in the foreground and opened in the background, I am receiving the push notifications, and navigation for it is also working correctly.
But in the case when the app is killed and I receive the notification, on click of it, it takes a user to the home/main screen in the app and not to the expected navigation screen as per the notification type.
Also, I noticed that none of the listeners added to handle the notification and its response gets triggered. And it is also not throwing any specific error.