-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathRootContainer.js
More file actions
223 lines (207 loc) · 6.38 KB
/
RootContainer.js
File metadata and controls
223 lines (207 loc) · 6.38 KB
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
import React, { Component } from "react"
import { View, Text, TouchableOpacity } from "react-native"
import {
notifications,
messages,
NotificationMessage,
Android
} from "react-native-firebase-push-notifications"
class RootContainer extends Component {
constructor(props) {
super(props)
this.state = {
token: "",
hasPermission: false
}
}
componentDidMount() {
this.onNotificationListener()
this.onNotificationOpenedListener()
this.getInitialNotification()
}
componentWillUnmount() {}
getToken = async () => {
//get the messeging token
const token = await notifications.getToken()
//you can also call messages.getToken() (does the same thing)
return token
}
getInitialNotification = async () => {
//get the initial token (triggered when app opens from a closed state)
const notification = await notifications.getInitialNotification()
console.log("getInitialNotification", notification)
return notification
}
onNotificationOpenedListener = () => {
//remember to remove the listener on un mount
//this gets triggered when the application is in the background
this.removeOnNotificationOpened = notifications.onNotificationOpened(
notification => {
console.log("onNotificationOpened", notification)
//do something with the notification
}
)
}
onNotificationListener = () => {
//remember to remove the listener on un mount
//this gets triggered when the application is in the forground/runnning
//for android make sure you manifest is setup - else this wont work
this.removeOnNotification = notifications.onNotification(notification => {
//do something with the notification
console.log("onNotification", notification)
})
}
onTokenRefreshListener = () => {
//remember to remove the listener on un mount
//this gets triggered when a new token is generated for the user
this.removeonTokenRefresh = messages.onTokenRefresh(token => {
//do something with the new token
})
}
setBadge = async number => {
//only works on iOS for now
return await notifications.setBadge(number)
}
getBadge = async () => {
//only works on iOS for now
return await notifications.getBadge()
}
hasPermission = async () => {
//only works on iOS
return await notifications.hasPermission()
}
requestPermission = async () => {
//only works on iOS
return await notifications.requestPermission()
}
componentWillUnmount() {
//remove the listener on unmount
if (this.removeOnNotificationOpened) {
this.removeOnNotificationOpened()
}
if (this.removeOnNotification) {
this.removeOnNotification()
}
if (this.removeonTokenRefresh) {
this.removeonTokenRefresh()
}
}
onTokenButtonPress = async () => {
const token = await this.getToken()
console.log("token : ", token)
this.setState({ token: token })
}
onTestHasPermission = async () => {
const has = await this.hasPermission()
console.log("Has", has)
this.setState({ hasPermission: has })
}
render() {
const { token, hasPermission } = this.state
return (
<View style={{ flex: 3 }}>
<View style={{ flex: 1, backgroundColor: "green" }}>
<TouchableOpacity
style={{
backgroundColor: "red",
flex: 1,
alignContent: "center",
alignItems: "center",
justifyContent: "center"
}}
onPress={() => this.onTokenButtonPress()}
>
<Text>Get Token</Text>
</TouchableOpacity>
</View>
<View
style={{
flex: 1,
alignContent: "center",
alignItems: "center",
justifyContent: "center",
backgroundColor: "blue"
}}
>
<Text>Token : {token}</Text>
</View>
<View style={{ flex: 1, backgroundColor: "pink" }}>
<Text>hasPermission: {hasPermission ? "Yes" : "No"}</Text>
<TouchableOpacity
style={{
backgroundColor: "red",
flex: 1,
alignContent: "center",
alignItems: "center",
justifyContent: "center"
}}
onPress={() => this.onTestHasPermission()}
>
<Text>Has permission</Text>
</TouchableOpacity>
<TouchableOpacity
style={{
backgroundColor: "green",
flex: 1,
alignContent: "center",
alignItems: "center",
justifyContent: "center"
}}
onPress={() => this.requestPermission()}
>
<Text>Get permission</Text>
</TouchableOpacity>
<TouchableOpacity
style={{
backgroundColor: "orange",
flex: 1,
alignContent: "center",
alignItems: "center",
justifyContent: "center"
}}
onPress={async () => {
const channel = new Android.Channel(
"test-channel",
"Test Channel",
Android.Importance.Max
).setDescription("My apps test channel")
// Create the channel
notifications.android().createChannel(channel)
const c = await notifications.displayNotification(
new NotificationMessage()
.setNotificationId("notification-id")
.setTitle("Notification title")
.setBody("Notification body")
.setData({
key1: "key1",
key2: "key2"
})
.android.setChannelId("test-channel")
)
}}
>
<Text>display notification</Text>
</TouchableOpacity>
</View>
</View>
)
return (
<>
<View style={{ flex: 1, backgroundColor: "blue" }}>
<TouchableOpacity
style={{ backgroundColor: "red" }}
onPress={() => this.onTokenButtonPress()}
>
<Text>Get Token</Text>
</TouchableOpacity>
</View>
<View
style={{ flex: 1, backgroundColor: "green", width: 100, height: 100 }}
>
<Text>Token : {token}</Text>
</View>
</>
)
}
}
export default RootContainer