Skip to content

Instantly share code, notes, and snippets.

@brownbl1
Created February 2, 2018 15:00
Show Gist options
  • Save brownbl1/473f8b755885bbc673d3ac23c8adcd74 to your computer and use it in GitHub Desktop.
Save brownbl1/473f8b755885bbc673d3ac23c8adcd74 to your computer and use it in GitHub Desktop.
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