Skip to content

Commit 0d951d3

Browse files
authored
Add userInteraction attr for user opened push (#122)
1 parent e3375bf commit 0d951d3

File tree

3 files changed

+74
-9
lines changed

3 files changed

+74
-9
lines changed

README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,30 @@ to:
160160
import PushNotificationIOS from '@react-native-community/push-notification-ios';
161161
```
162162

163+
## How to determine push notification user click
164+
165+
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:
166+
167+
```js
168+
export const App = () => {
169+
const [permissions, setPermissions] = useState({});
170+
171+
useEffect(() => {
172+
PushNotificationIOS.addEventListener('notification', onRemoteNotification);
173+
});
174+
175+
const onRemoteNotification = (notification) => {
176+
const isClicked = notification.getData().userInteraction === 1
177+
178+
if (isClicked) {
179+
// Navigate user to another screen
180+
} else {
181+
// Do something else with push notification
182+
}
183+
};
184+
}
185+
```
186+
163187
# Reference
164188

165189
## Methods

example/App.js

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,16 @@ export const App = () => {
8484
});
8585
};
8686

87+
const sendSilentNotification = () => {
88+
DeviceEventEmitter.emit('remoteNotificationReceived', {
89+
remote: true,
90+
aps: {
91+
category: 'REACT_NATIVE',
92+
'content-available': 1,
93+
}
94+
});
95+
};
96+
8797
const sendLocalNotification = () => {
8898
PushNotificationIOS.presentLocalNotification({
8999
alertTitle: 'Sample Title',
@@ -122,27 +132,42 @@ export const App = () => {
122132
};
123133

124134
const onRemoteNotification = (notification) => {
135+
const isClicked = notification.getData().userInteraction === 1
136+
125137
const result = `
126138
Title: ${notification.getTitle()};\n
127139
Message: ${notification.getMessage()};\n
128140
badge: ${notification.getBadgeCount()};\n
129141
sound: ${notification.getSound()};\n
130142
category: ${notification.getCategory()};\n
131-
content-available: ${notification.getContentAvailable()}.`;
143+
content-available: ${notification.getContentAvailable()};\n
144+
Notification is clicked: ${String(isClicked)}.`;
132145

133-
Alert.alert('Push Notification Received', result, [
134-
{
135-
text: 'Dismiss',
136-
onPress: null,
137-
},
138-
]);
146+
if (notification.getTitle() == undefined) {
147+
Alert.alert('Silent push notification Received', result, [
148+
{
149+
text: 'Send local push',
150+
onPress: sendLocalNotification,
151+
},
152+
]);
153+
} else {
154+
Alert.alert('Push Notification Received', result, [
155+
{
156+
text: 'Dismiss',
157+
onPress: null,
158+
},
159+
]);
160+
}
139161
};
140162

141163
const onLocalNotification = (notification) => {
164+
const isClicked = notification.getData().userInteraction === 1
165+
142166
Alert.alert(
143167
'Local Notification Received',
144168
`Alert title: ${notification.getTitle()},
145-
'Alert message: ${notification.getMessage()}`,
169+
'Alert message: ${notification.getMessage()},
170+
Notification is clicked: ${String(isClicked)}.`,
146171
[
147172
{
148173
text: 'Dismiss',
@@ -171,6 +196,8 @@ export const App = () => {
171196
label="Schedule fake local notification"
172197
/>
173198

199+
<Button onPress={sendSilentNotification} label="Send fake silent notification" />
200+
174201
<Button
175202
onPress={() => PushNotificationIOS.setApplicationIconBadgeNumber(42)}
176203
label="Set app's icon badge to 42"

ios/RNCPushNotificationIOS.m

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,20 @@ @implementation RNCPushNotificationIOS
127127
return formattedNotification;
128128
}
129129

130+
API_AVAILABLE(ios(10.0))
131+
static NSDictionary *RCTFormatOpenedUNNotification(UNNotification *notification)
132+
{
133+
NSMutableDictionary *formattedNotification = [RCTFormatUNNotification(notification) mutableCopy];
134+
UNNotificationContent *content = notification.request.content;
135+
136+
NSMutableDictionary *userInfo = [content.userInfo mutableCopy];
137+
userInfo[@"userInteraction"] = [NSNumber numberWithInt:1];
138+
139+
formattedNotification[@"userInfo"] = RCTNullIfNil(RCTJSONClean(userInfo));
140+
141+
return formattedNotification;
142+
}
143+
130144
#endif //TARGET_OS_TV / TARGET_OS_UIKITFORMAC
131145

132146
RCT_EXPORT_MODULE()
@@ -222,7 +236,7 @@ + (void)didReceiveNotificationResponse:(UNNotificationResponse *)response
222236
API_AVAILABLE(ios(10.0)) {
223237
[[NSNotificationCenter defaultCenter] postNotificationName:kLocalNotificationReceived
224238
object:self
225-
userInfo:RCTFormatUNNotification(response.notification)];
239+
userInfo:RCTFormatOpenedUNNotification(response.notification)];
226240
}
227241

228242
- (void)handleLocalNotificationReceived:(NSNotification *)notification

0 commit comments

Comments
 (0)