Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
// iOS won't necessarily prompt the user a second time.
if (existingStatus !== 'granted') {
// Android remote notification permissions are granted during the app
// install, so this will only ask on iOS
const { status } = await Permissions.askAsync(Permissions.NOTIFICATIONS)
finalStatus = status
}
// Stop here if the user did not grant permissions
if (finalStatus !== 'granted') {
console.log('Failed to grant')
return
}
// Get the token that uniquely identifies this device
let token = await Notifications.getExpoPushTokenAsync()
// POST the token to your backend server from where you can retrieve it to send push notifications.
return fetch(PUSH_ENDPOINT, {
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify({
token: {
value: token
},
user: {
username: 'Brent'
}
})
// only ask if permissions have not already been determined, because
// iOS won't necessarily prompt the user a second time.
if (existingStatus !== 'granted') {
// Android remote notification permissions are granted during the app
// install, so this will only ask on iOS
const { status } = await Permissions.askAsync(Permissions.NOTIFICATIONS);
finalStatus = status;
}
// Stop here if the user did not grant permissions
if (finalStatus !== 'granted') {
return;
}
// Get the token that uniquely identifies this device
let token = await Notifications.getExpoPushTokenAsync();
// POST the token to our backend so we can use it to send pushes from there
response = await fetchFromServer('users/'+ user_id +'/push_token/','POST',{
push_token: token,
},null);
if (response.status === 200){
user = await response.json();
return {user: user,error:'None'};
}
return {error:'Server Error'};
}
// only ask if permissions have not already been determined, because
// iOS won't necessarily prompt the user a second time.
if (existingStatus !== 'granted') {
// Android remote notification permissions are granted during the app
// install, so this will only ask on iOS
const { status } = await Permissions.askAsync(Permissions.NOTIFICATIONS);
finalStatus = status;
}
// Stop here if the user did not grant permissions
if (finalStatus !== 'granted') {
return null;
}
// Get the token that uniquely identifies this device
let token = await Notifications.getExpoPushTokenAsync();
return token;
}
// Remote notifications do not work in simulators, only on device
if (!Constants.isDevice) {
return;
}
// Android remote notification permissions are granted during the app
// install, so this will only ask on iOS
let { status } = await Permissions.askAsync(Permissions.NOTIFICATIONS);
// Stop here if the user did not grant permissions
if (status !== "granted") {
return;
}
// Get the token that uniquely identifies this device
let token = await Notifications.getExpoPushTokenAsync();
// POST the token to our backend so we can use it to send pushes from there
return fetch(PUSH_ENDPOINT, {
method: "POST",
headers: {
Accept: "application/json",
"Content-Type": "application/json"
},
body: JSON.stringify({
token: {
value: token
}
})
});
}
export default registerForPushNotificationsAsync
async registerForPushNotifications() {
const { status } = await Permissions.getAsync(Permissions.NOTIFICATIONS);
let finalStatus = status;
if (status !=='granted') {
const { status } = await Permissions.getAsync(Permissions.NOTIFICATIONS);
finalStatus = status;
}
if (finalStatus !== 'granted') {return;}
let token = await Notifications.getExpoPushTokenAsync();
console.log(token);
this.props.addPushToken(token);
this.setState({ isMount: false });
}
async function syncReminder(id, state) {
try {
const token = await Notifications.getExpoPushTokenAsync();
await api("sync-reminder", {
state,
id,
token
});
} catch (error) {
console.log(error);
}
}
export async function updateNotificationTokens() {
const settings = await withComputerConfig();
const response = await Permissions.getAsync(Permissions.NOTIFICATIONS);
if (!response.granted) {
await socket.registerPushNotificationToken(NotificationType.CLEAR, null);
await socket.registerPushNotificationToken(NotificationType.GAME_STARTED, null);
await socket.registerPushNotificationToken(NotificationType.READY_CHECK, null);
return;
}
const token = await Notifications.getExpoPushTokenAsync();
await socket.registerPushNotificationToken(NotificationType.CLEAR, token);
await socket.registerPushNotificationToken(
NotificationType.GAME_STARTED,
settings.gameStartNotificationsEnabled ? token : null
);
await socket.registerPushNotificationToken(
NotificationType.READY_CHECK,
settings.readyCheckNotificationsEnabled ? token : null
);
}
getToken: async ()=>{
const token = await Notifications.getExpoPushTokenAsync();
return token;
},