This repository has been archived by the owner on Dec 30, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
notification.js
53 lines (50 loc) · 1.81 KB
/
notification.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
import FCM, {FCMEvent, RemoteNotificationResult, WillPresentNotificationResult, NotificationType} from 'react-native-fcm';
export default class NotificationClient extends Component {
render(){
return null;
}
componentDidMount() {
FCM.requestPermissions();
FCM.getFCMToken().then(token => {
console.log("Got token",token)
this.props.onChangeToken(token);
});
this.notificationListener = FCM.on(FCMEvent.Notification, async (notif) => {
console.log('Got Notification',notif)
FCM.presentLocalNotification({
title: notif.title,
body: notif.body,
priority: "high",
click_action: notif.click_action,
show_in_foreground: true,
local: true
});
if(notif.local_notification){
//this is a local notification
}
if(notif.opened_from_tray){
//app is open/resumed because user clicked banner
}
});
this.refreshTokenListener = FCM.on(FCMEvent.RefreshToken, (token) => {
console.log('refreshTokenListener',token)
this.props.onChangeToken(token);
// fcm token may not be available on first load, catch it here
});
FCM.getInitialNotification().then(function(notif){
console.log('Got Notification',notif);
FCM.presentLocalNotification({
title: notif.title,
body: notif.body,
priority: "high",
click_action: notif.click_action,
show_in_foreground: true,
local: true
});
});
}
componentWillUnmount() {
this.notificationListener.remove();
this.refreshTokenListener.remove();
}
}