-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathpush-notifications.js
46 lines (37 loc) · 1.03 KB
/
push-notifications.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
import { Permissions ,
Notifications } from 'expo';
// Example server, implemented in Rails: https://git.io/vKHKv
const api = {
doman : 'https://exponent-push-server.herokuapp.com' ,
path :'/tokens' ,
headers : {
Accept : 'application/json' ,
headers : {
'Content-Type' : 'application/json'
}
}
}
export default {
setup : async function () {
// Android remote notification permissions are granted during the app
// install, so this will only ask on iOS
let { status } = await Permissions.askAsync ( Permissions.REMOTE_NOTIFICATIONS ) ,
token;
// Stop here if the user did not grant permissions
if ( status !== 'granted ') {
return;
}
// Get the token that uniquely identifies this device
token = await Notifications.getExponentPushTokenAsync ();
// POST the token to our backend so we can use it to send pushes from there
return fetch ( api.domain + api.path , {
...headers ,
body : JSON.stringify ({
token : {
value : token
}
}) ,
method : 'POST'
});
}
};