From 7628331ca5dfd7f76d99610a50f9e1454067ab53 Mon Sep 17 00:00:00 2001 From: Tuan Nguyen Date: Sat, 14 Dec 2024 10:16:38 +0700 Subject: [PATCH] Update events.md Add extra example of handling navigation on background --- docs-react-native/react-native/docs/events.md | 29 +++++++++++++------ 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/docs-react-native/react-native/docs/events.md b/docs-react-native/react-native/docs/events.md index 0b7727be0..36eca45c6 100644 --- a/docs-react-native/react-native/docs/events.md +++ b/docs-react-native/react-native/docs/events.md @@ -92,22 +92,33 @@ method should be registered as early on in your project as possible (e.g. the `i ```js // index.js -import { AppRegistry } from 'react-native'; +import { AppRegistry, Linking } from 'react-native'; import notifee, { EventType } from '@notifee/react-native'; import App from './App'; notifee.onBackgroundEvent(async ({ type, detail }) => { const { notification, pressAction } = detail; - + const actionId = pressAction.id // Check if the user pressed the "Mark as read" action - if (type === EventType.ACTION_PRESS && pressAction.id === 'mark-as-read') { - // Update external API - await fetch(`https://my-api.com/chat/${notification.data.chatId}/read`, { - method: 'POST', - }); + if (type === EventType.ACTION_PRESS) { + if (actionId === 'mark-as-read') { + // Update external API + await fetch(`https://my-api.com/chat/${notification.data.chatId}/read`, { + method: 'POST', + }); + + // Remove the notification + await notifee.cancelNotification(notification.id); + } - // Remove the notification - await notifee.cancelNotification(notification.id); + // you might want to open application and navigate to a specific screen + // deeplink will does this for you + const notificationId = notification?.data?.notificationId + let url = 'myapp://' + if (notificationId) { + url += `notifications/${notificationId}` + } + await Linking.openURL(url); } });