Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add userInteraction attr for user opened push #122

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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: ${String(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: ${String(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
16 changes: 15 additions & 1 deletion ios/RNCPushNotificationIOS.m
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,20 @@ @implementation RNCPushNotificationIOS
return formattedNotification;
}

API_AVAILABLE(ios(10.0))
static NSDictionary *RCTFormatOpenedUNNotification(UNNotification *notification)
{
NSMutableDictionary *formattedNotification = [RCTFormatUNNotification(notification) mutableCopy];
UNNotificationContent *content = notification.request.content;

NSMutableDictionary *userInfo = [content.userInfo mutableCopy];
userInfo[@"userInteraction"] = [NSNumber numberWithInt:1];

formattedNotification[@"userInfo"] = RCTNullIfNil(RCTJSONClean(userInfo));

return formattedNotification;
}

#endif //TARGET_OS_TV / TARGET_OS_UIKITFORMAC

RCT_EXPORT_MODULE()
Expand Down Expand Up @@ -222,7 +236,7 @@ + (void)didReceiveNotificationResponse:(UNNotificationResponse *)response
API_AVAILABLE(ios(10.0)) {
[[NSNotificationCenter defaultCenter] postNotificationName:kLocalNotificationReceived
object:self
userInfo:RCTFormatUNNotification(response.notification)];
userInfo:RCTFormatOpenedUNNotification(response.notification)];
}

- (void)handleLocalNotificationReceived:(NSNotification *)notification
Expand Down