Skip to content

Commit

Permalink
Update README and example app for userIntreaction
Browse files Browse the repository at this point in the history
  • Loading branch information
xvonabur committed Sep 11, 2020
1 parent b1a1c9e commit 09d5526
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 8 deletions.
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
43 changes: 35 additions & 8 deletions example/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -171,6 +196,8 @@ export const App = () => {
label="Schedule fake local notification"
/>

<Button onPress={sendSilentNotification} label="Send fake silent notification" />

<Button
onPress={() => PushNotificationIOS.setApplicationIconBadgeNumber(42)}
label="Set app's icon badge to 42"
Expand Down

0 comments on commit 09d5526

Please sign in to comment.