Created
February 2, 2018 15:00
-
-
Save brownbl1/473f8b755885bbc673d3ac23c8adcd74 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Util, Permissions, Notifications, Constants } from 'expo' | |
export const 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 | |
} | |
let token = await Notifications.getExpoPushTokenAsync() | |
const zone = await Util.getCurrentTimeZoneAsync() | |
const res = await fetch( | |
'https://<my-url>.amazonaws.com/dev/subscribe', | |
{ | |
method: 'POST', | |
headers: { | |
Accept: 'application/json', | |
'Content-Type': 'application/json', | |
}, | |
body: JSON.stringify({ | |
token, | |
zone, | |
deviceId: Constants.deviceId, | |
deviceName: Constants.deviceName, | |
}), | |
} | |
) | |
console.log('registered!') | |
return await res.json() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment