-
Notifications
You must be signed in to change notification settings - Fork 133
/
Copy pathNotifeeApp.tsx
231 lines (216 loc) · 6.04 KB
/
NotifeeApp.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
/**
* Sample React Native App
* https://github.com/facebook/react-native
*
* @format
* @flow
*/
import React, {useEffect} from 'react';
import {Button, Platform, StyleSheet, Text, View} from 'react-native';
import notifee, {
AndroidLaunchActivityFlag,
AuthorizationStatus,
EventType,
} from '@notifee/react-native';
notifee.onBackgroundEvent(async ({type, detail}) => {
const {notification, pressAction} = detail;
console.log(
`[onBackgroundEvent] notification id: ${
notification !== undefined ? notification.id : 'undefined'
}, event type: ${EventType[type]}, press action: ${pressAction?.id}`,
);
});
async function createChannel() {
let channelId = 'default';
try {
channelId = await notifee.createChannel({
id: 'default',
name: 'Default Channel',
});
} catch (e) {
console.log('Unable to create a channel: ', JSON.stringify(e));
}
return channelId;
}
async function requestPermissions() {
try {
const settings = await notifee.requestPermission();
if (settings.authorizationStatus === AuthorizationStatus.DENIED) {
console.log('User denied permissions request');
} else if (
settings.authorizationStatus === AuthorizationStatus.AUTHORIZED
) {
console.log('User granted permissions request');
} else if (
settings.authorizationStatus === AuthorizationStatus.PROVISIONAL
) {
console.log('User provisionally granted permissions request');
}
} catch (e) {
console.log('Unable to request permissions: ', JSON.stringify(e));
}
}
async function onDisplayNotification() {
try {
const channelId = await createChannel();
await requestPermissions();
// Display a notification
await notifee.displayNotification({
title: 'Notification Title',
body: 'Main body content of the notification',
android: {
channelId,
pressAction: {
id: 'default',
launchActivity: 'default',
launchActivityFlags: [AndroidLaunchActivityFlag.SINGLE_TOP],
},
},
});
} catch (e) {
console.log('Failed to display notification?', e);
}
}
async function setCategories() {
await notifee.setNotificationCategories([
{
id: 'post',
actions: [
{
id: 'like',
title: 'Like Post',
},
{
id: 'dislike',
title: 'Dislike Post',
},
],
},
]);
}
async function onDisplayNotificationWithActions() {
// Create a channel
try {
const channelId = await createChannel();
await requestPermissions();
// Display a notification
await notifee.displayNotification({
title: 'New post from John',
body: 'Hey everyone! Check out my new blog post on my website.',
ios: {
categoryId: 'post',
},
android: {
channelId,
actions: [
{
title: 'Like',
pressAction: {
id: 'like',
launchActivity: 'default',
launchActivityFlags: [AndroidLaunchActivityFlag.SINGLE_TOP],
},
},
{
title: 'Dislike',
pressAction: {
id: 'dislike',
launchActivity: 'default',
launchActivityFlags: [AndroidLaunchActivityFlag.SINGLE_TOP],
},
},
],
pressAction: {
id: 'default',
launchActivity: 'default',
launchActivityFlags: [AndroidLaunchActivityFlag.SINGLE_TOP],
},
},
});
} catch (e) {
console.log('Failed to display notification?', e);
}
}
const instructions = Platform.select({
ios: 'Press Cmd+R to reload,\n' + 'Cmd+D or shake for dev menu',
android:
'Double tap R on your keyboard to reload,\n' +
'Shake or press menu button for dev menu',
});
export default function App() {
useEffect(() => {
setCategories();
return notifee.onForegroundEvent(({type, detail}) => {
const {notification, pressAction} = detail;
const pressActionLabel = pressAction
? `, press action: ${pressAction?.id}`
: '';
console.log(
`[onForegroundEvent] notification id: ${
notification !== undefined ? notification.id : 'undefined'
}, event type: ${EventType[type]}${pressActionLabel}`,
);
switch (type) {
case EventType.DISMISSED:
console.log(
'[onForegroundEvent] User dismissed notification',
notification,
);
break;
case EventType.PRESS:
console.log(
'[onForegroundEvent] User pressed notification',
notification,
);
break;
case EventType.ACTION_PRESS:
console.log(
'[onForegroundEvent] User pressed an action',
notification,
detail.pressAction,
);
// On Android the notification does not dismiss automatically if it was an interaction press, so we dismiss it ourselves
console.log(
'[onBackgroundEvent] ACTION_PRESS: cancelling notification',
);
if (notification !== undefined && notification.id !== undefined) {
notifee.cancelNotification(notification.id);
}
break;
}
});
}, []);
return (
<View style={styles.container}>
<Text style={styles.welcome}>Notifee React Native Demo</Text>
<Text style={styles.instructions}>To get started, edit App.js</Text>
<Text style={styles.instructions}>{instructions}</Text>
<Button
title="Display Notification"
onPress={() => onDisplayNotification()}
/>
<Button
title="Display Notification With Actions"
onPress={() => onDisplayNotificationWithActions()}
/>
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#F5FCFF',
},
welcome: {
fontSize: 20,
textAlign: 'center',
margin: 10,
},
instructions: {
textAlign: 'center',
color: '#333333',
marginBottom: 5,
},
});