-
Notifications
You must be signed in to change notification settings - Fork 4.8k
/
NotificationScreen.tsx
240 lines (212 loc) · 7.68 KB
/
NotificationScreen.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
import { Subscription } from '@unimodules/core';
import * as Notifications from 'expo-notifications';
import * as Permissions from 'expo-permissions';
import React from 'react';
import { Alert, ScrollView, Platform } from 'react-native';
import registerForPushNotificationsAsync from '../api/registerForPushNotificationsAsync';
import HeadingText from '../components/HeadingText';
import ListButton from '../components/ListButton';
import MonoText from '../components/MonoText';
export default class NotificationScreen extends React.Component<
object,
{
lastNotifications?: Notifications.Notification;
}
> {
static navigationOptions = {
title: 'Notifications',
};
private _onReceivedListener: Subscription | undefined;
private _onResponseReceivedListener: Subscription | undefined;
constructor(props: object) {
super(props);
this.state = {};
}
componentDidMount() {
if (Platform.OS !== 'web') {
this._onReceivedListener = Notifications.addNotificationReceivedListener(
this._handelReceivedNotification
);
this._onResponseReceivedListener = Notifications.addNotificationResponseReceivedListener(
this._handelNotificationResponseReceived
);
}
}
componentWillUnmount() {
this._onReceivedListener?.remove();
this._onResponseReceivedListener?.remove();
}
render() {
console.log(this.state);
return (
<ScrollView style={{ padding: 10 }}>
<HeadingText>Local Notifications</HeadingText>
<ListButton
onPress={this._LEGACY_presentLocalNotificationAsync}
title="[Legacy] Present a notification immediately"
/>
<ListButton
onPress={this._presentLocalNotificationAsync}
title="Present a notification immediately"
/>
<ListButton
onPress={this._scheduleLocalNotificationAsync}
title="Schedule notification for 10 seconds from now"
/>
<ListButton
onPress={this._scheduleLocalNotificationAndCancelAsync}
title="Schedule notification for 10 seconds from now and then cancel it immediately"
/>
<ListButton
onPress={Notifications.cancelAllScheduledNotificationsAsync}
title="Cancel all scheduled notifications"
/>
<HeadingText>Push Notifications</HeadingText>
<ListButton onPress={this._sendNotificationAsync} title="Send me a push notification" />
<HeadingText>Badge Number</HeadingText>
<ListButton
onPress={this._incrementIconBadgeNumberAsync}
title="Increment the app icon's badge number"
/>
<ListButton onPress={this._clearIconBadgeAsync} title="Clear the app icon's badge number" />
<HeadingText>Dismissing notifications</HeadingText>
<ListButton
onPress={this._countPresentedNotifications}
title="Count presented notifications"
/>
<ListButton onPress={this._dismissSingle} title="Dismiss a single notification" />
<ListButton onPress={this._dismissAll} title="Dismiss all notifications" />
{this.state.lastNotifications && (
<MonoText containerStyle={{ marginBottom: 20 }}>
{JSON.stringify(this.state.lastNotifications, null, 2)}
</MonoText>
)}
</ScrollView>
);
}
_handelReceivedNotification = (notification: Notifications.Notification) => {
this.setState({
lastNotifications: notification,
});
};
_handelNotificationResponseReceived = (
notificationResponse: Notifications.NotificationResponse
) => {
console.log({ notificationResponse });
// Calling alert(message) immediately fails to show the alert on Android
// if after backgrounding the app and then clicking on a notification
// to foreground the app
setTimeout(() => Alert.alert('You clicked on the notification 🥇'), 1000);
};
_obtainUserFacingNotifPermissionsAsync = async () => {
let permission = await Permissions.getAsync(Permissions.USER_FACING_NOTIFICATIONS);
if (permission.status !== 'granted') {
permission = await Permissions.askAsync(Permissions.USER_FACING_NOTIFICATIONS);
if (permission.status !== 'granted') {
Alert.alert(`We don't have permission to present notifications.`);
}
}
return permission;
};
_obtainRemoteNotifPermissionsAsync = async () => {
let permission = await Permissions.getAsync(Permissions.NOTIFICATIONS);
if (permission.status !== 'granted') {
permission = await Permissions.askAsync(Permissions.NOTIFICATIONS);
if (permission.status !== 'granted') {
Alert.alert(`We don't have permission to receive remote notifications.`);
}
}
return permission;
};
_presentLocalNotificationAsync = async () => {
await this._obtainUserFacingNotifPermissionsAsync();
await Notifications.scheduleNotificationAsync({
content: {
title: 'Here is a scheduled notification!',
body: 'This is the body',
data: {
hello: 'there',
future: 'self',
},
sound: true,
},
trigger: null,
});
};
_LEGACY_presentLocalNotificationAsync = async () => {
await this._obtainUserFacingNotifPermissionsAsync();
await Notifications.presentNotificationAsync({
title: 'Here is a local notification!',
body: 'This is the body',
data: {
hello: 'there',
},
sound: true,
});
};
_scheduleLocalNotificationAsync = async () => {
await this._obtainUserFacingNotifPermissionsAsync();
await Notifications.scheduleNotificationAsync({
content: {
title: 'Here is a local notification!',
body: 'This is the body',
data: {
hello: 'there',
future: 'self',
},
sound: true,
},
trigger: {
seconds: 10,
},
});
};
_scheduleLocalNotificationAndCancelAsync = async () => {
await this._obtainUserFacingNotifPermissionsAsync();
const notificationId = await Notifications.scheduleNotificationAsync({
content: {
title: 'This notification should not appear',
body: 'It should have been cancelled. :(',
sound: true,
},
trigger: {
seconds: 10,
},
});
await Notifications.cancelScheduledNotificationAsync(notificationId);
};
_incrementIconBadgeNumberAsync = async () => {
const currentNumber = await Notifications.getBadgeCountAsync();
await Notifications.setBadgeCountAsync(currentNumber + 1);
const actualNumber = await Notifications.getBadgeCountAsync();
Alert.alert(`Set the badge number to ${actualNumber}`);
};
_clearIconBadgeAsync = async () => {
await Notifications.setBadgeCountAsync(0);
Alert.alert(`Cleared the badge`);
};
_sendNotificationAsync = async () => {
const permission = await this._obtainRemoteNotifPermissionsAsync();
if (permission.status === 'granted') {
registerForPushNotificationsAsync();
}
};
_countPresentedNotifications = async () => {
const presentedNotifications = await Notifications.getPresentedNotificationsAsync();
Alert.alert(`You currently have ${presentedNotifications.length} notifications presented`);
};
_dismissAll = async () => {
await Notifications.dismissAllNotificationsAsync();
Alert.alert(`Notifications dismissed`);
};
_dismissSingle = async () => {
const presentedNotifications = await Notifications.getPresentedNotificationsAsync();
if (!presentedNotifications.length) {
Alert.alert(`No notifications to be dismissed`);
return;
}
const identifier = presentedNotifications[0].request.identifier;
await Notifications.dismissNotificationAsync(identifier);
Alert.alert(`Notification dismissed`);
};
}