-
Notifications
You must be signed in to change notification settings - Fork 0
/
notification.js
86 lines (72 loc) · 2.25 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
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
import React, { Component } from 'react';
import {
View
} from 'react-native';
import {
Card
} from 'react-native-elements';
import { Permissions, Notifications } from 'expo';
import registerToken from "./src/api/register/token";
import { connect } from 'react-redux';
async function registerForPushNotificationsAsync(token) {
const { status: existingStatus } = await Permissions.getAsync(
Permissions.NOTIFICATIONS
);
let finalStatus = existingStatus;
// only ask if permissions have not already been determined, because
// iOS won't necessarily prompt the user a second time.
console.log(existingStatus)
if (existingStatus !== 'granted') {
// Android remote notification permissions are granted during the app
// install, so this will only ask on iOS
try {
status = await Permissions.askAsync(Permissions.NOTIFICATIONS);
finalStatus = status;
} catch(err) {
console.log('ERROR GETTING THE TOKEN');
console.log(err);
}
}
// Stop here if the user did not grant permissions
if (finalStatus !== 'granted') {
return;
}
// Get the token that uniquely identifies this device
let pushToken;
try {
pushToken = await Notifications.getExpoPushTokenAsync();
} catch(err) {
console.log('Error on notification.getExpoPushTokenAsync');
console.log(err);
return;
}
// token = token.match(/4GuFH-PLGtIcTqAva45Lwk/)[0];
console.log('==========================token');
console.log(token);
// POST the token to your backend server from where you can retrieve it to send push notifications.
return registerToken({ token: pushToken }, token);
}
class Notification extends Component {
state = {
notification: {},
}
componentDidUpdate(prevProps) {
console.log('NOTIFICATION DID UPDATE');
if(!prevProps.token && this.props.token) {
registerForPushNotificationsAsync(this.props.token);
this._notificationSubscription = Notifications.addListener(this._handleNotification);
}
}
_handleNotification = (notification) => {
this.setState({ notification: notification });
};
render() {
return (
<View />
)
}
}
const mapStateToProps = ({ auth: { logIn }}) => ({
token: logIn.token
})
export default connect(mapStateToProps)(Notification);