diff --git a/README.md b/README.md index 580d1302f..7eaee9d7b 100644 --- a/README.md +++ b/README.md @@ -160,6 +160,30 @@ to: import PushNotificationIOS from '@react-native-community/push-notification-ios'; ``` +## How to determine push notification user click + +Receiving remote pushes has two common cases: user dismissed notification and user clicked notification. To have separate logic for each case you can use `notification.getData().userInteraction` to determine push notification user click: + +```js +export const App = () => { + const [permissions, setPermissions] = useState({}); + + useEffect(() => { + PushNotificationIOS.addEventListener('notification', onRemoteNotification); + }); + + const onRemoteNotification = (notification) => { + const isClicked = notification.getData().userInteraction === 1 + + if (isClicked) { + // Navigate user to another screen + } else { + // Do something else with push notification + } + }; +} +``` + # Reference ## Methods diff --git a/example/App.js b/example/App.js index 297ac07bf..1d371056b 100644 --- a/example/App.js +++ b/example/App.js @@ -84,6 +84,16 @@ export const App = () => { }); }; + const sendSilentNotification = () => { + DeviceEventEmitter.emit('remoteNotificationReceived', { + remote: true, + aps: { + category: 'REACT_NATIVE', + 'content-available': 1, + } + }); + }; + const sendLocalNotification = () => { PushNotificationIOS.presentLocalNotification({ alertTitle: 'Sample Title', @@ -122,27 +132,42 @@ export const App = () => { }; const onRemoteNotification = (notification) => { + const isClicked = notification.getData().userInteraction === 1 + const result = ` Title: ${notification.getTitle()};\n Message: ${notification.getMessage()};\n badge: ${notification.getBadgeCount()};\n sound: ${notification.getSound()};\n category: ${notification.getCategory()};\n - content-available: ${notification.getContentAvailable()}.`; + content-available: ${notification.getContentAvailable()};\n + Notification is clicked: ${isClicked}.`; - Alert.alert('Push Notification Received', result, [ - { - text: 'Dismiss', - onPress: null, - }, - ]); + if (notification.getTitle() == undefined) { + Alert.alert('Silent push notification Received', result, [ + { + text: 'Send local push', + onPress: sendLocalNotification, + }, + ]); + } else { + Alert.alert('Push Notification Received', result, [ + { + text: 'Dismiss', + onPress: null, + }, + ]); + } }; const onLocalNotification = (notification) => { + const isClicked = notification.getData().userInteraction === 1 + Alert.alert( 'Local Notification Received', `Alert title: ${notification.getTitle()}, - 'Alert message: ${notification.getMessage()}`, + 'Alert message: ${notification.getMessage()}, + Notification is clicked: ${isClicked}.`, [ { text: 'Dismiss', @@ -171,6 +196,8 @@ export const App = () => { label="Schedule fake local notification" /> +