How to handle Push Notifications when the app is closed?

Please provide the following:

  1. SDK Version: 38
  2. Platforms(Android/iOS/web/all): Android/iOS

I am looking at the push notification docs and I am using the code to register a handler when the user clicks on a push notification. We have some push notifications that we want to open a URL, so I handle this in the app with:

const useNotificationResponseReceived = () => {
	useEffect(() => {
		const handler = Notifications.addNotificationResponseReceivedListener(
			async ({ notification }) => {
				try {
					const { data } = notification.request.content

					const url = `${data.click_action}`
					// if url is a link and is not a deep link to the app from the website
					if (
						url.startsWith("https://") &&
						!url.includes("mywebsite.com")
					) {
						await Linking.openURL(url)
					}
				} catch {}
			}
		)
		return () => Notifications.removeNotificationSubscription(handler)
	}, [])
}

This works great when the app is open in the background. Although if I close the app, and trigger a push notification, then the handler above doesn’t fire. How can I get the app to handle the push notification in this scenario?

Hey @bitttttten,

Are you saying that the listener is not firing if the app is closed and the user selects the notification from the OS tray?

If that’s the case, we are aware of this and currently tracking it here: [notifications] selecting notification when app is killed should trigger NotificationResponseReceivedListener · Issue #6943 · expo/expo · GitHub

Cheers,
Adam

That’s it! I’ll follow the issue on Github then. Thanks!

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.