-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.js
95 lines (87 loc) · 2.28 KB
/
App.js
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
import * as React from "react";
import { Text, View, StyleSheet } from "react-native";
import Constants from "expo-constants";
import * as Permissions from "expo-permissions";
import { Notifications } from "expo";
Notifications.createCategoryAsync("A_OR_B", [
{
actionId: "a",
buttonTitle: "A",
},
{
actionId: "b",
buttonTitle: "B",
},
]);
// Ex. token: `ExponentPushToken[MuUIsvLAyUcBoLne6F2EmX]`
export default class App extends React.Component {
state = {
token: null,
notification: null,
};
registerForPushNotificationsAsync = async () => {
const { status: existingStatus } = await Permissions.getAsync(
Permissions.NOTIFICATIONS
);
let finalStatus = existingStatus;
if (existingStatus !== "granted") {
const { status } = await Permissions.askAsync(Permissions.NOTIFICATIONS);
finalStatus = status;
}
if (finalStatus !== "granted") {
return;
}
try {
const token = await Notifications.getExpoPushTokenAsync();
console.log({ token });
this.setState({ token });
} catch (err) {
// couldn't get the token
}
};
componentDidMount() {
// Get the Expo push token
this.registerForPushNotificationsAsync();
// Add a listener
Notifications.addListener((notification) => {
this.setState({ notification });
});
}
render() {
return (
<View style={styles.container}>
<Text style={styles.heading}>Push Token</Text>
<Text style={styles.paragraph}>{this.state.token}</Text>
<Text style={styles.heading}>Latest Notification</Text>
<Text style={styles.paragraph}>
{JSON.stringify(this.state.notification)}
</Text>
<Text style={styles.heading}>actionId</Text>
<Text style={styles.paragraph}>
{this.state.notification ? this.state.notification.actionId : null}
</Text>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: "center",
paddingTop: Constants.statusBarHeight,
backgroundColor: "#ecf0f1",
padding: 8,
},
paragraph: {
margin: 10,
fontSize: 14,
fontWeight: "normal",
textAlign: "center",
},
heading: {
margin: 10,
fontSize: 18,
fontWeight: "bold",
textAlign: "center",
},
});