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 2749007 commit 2c72d68
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 8 deletions.
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,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

Expand Down
42 changes: 34 additions & 8 deletions example/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,16 @@ export const App = () => {
});
};

const sendSilentNotification = () => {
DeviceEventEmitter.emit('remoteNotificationReceived', {
remote: true,
aps: {
category: 'REACT_NATIVE',
'content-available': 1,
}
});
};

const sendLocalNotification = () => {
PushNotificationIOS.presentLocalNotification({
alertBody: 'Sample local notification',
Expand Down Expand Up @@ -121,24 +131,38 @@ export const App = () => {
};

const onRemoteNotification = notification => {
const isClicked = notification.getData().userInteraction === 1

const result = `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 message: ' + notification.getMessage(),
'Alert message: ' + notification.getMessage() + `\nNotification is clicked: ${isClicked}.`,
[
{
text: 'Dismiss',
Expand Down Expand Up @@ -167,6 +191,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 2c72d68

Please sign in to comment.