Can't get the token with the getExpoPushTokenAsync() function on expo-notifications api. The function as follow is just identical to Expo documentation:
import Constants from "expo-constants";
import * as Notifications from "expo-notifications";
import * as Permissions from "expo-permissions";
async function registerForPushNotificationsAsync() {
let token;
if (Constants.isDevice) {
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') {
alert('Failed to get push token for push notification!');
return;
}
token = (await Notifications.getExpoPushTokenAsync()).data;
console.log(token);
} else {
alert('Must use physical device for Push Notifications');
}
if (Platform.OS === 'android') {
Notifications.setNotificationChannelAsync('default', {
name: 'default',
importance: Notifications.AndroidImportance.MAX,
vibrationPattern: [0, 250, 250, 250],
lightColor: '#FF231F7C',
});
}
return token;
}
Expo: ~37.0.3 App.json:
"expo": {
"android": {
"useNextNotificationsApi": true
}
}
Seems like when calling the function, got the next warning:
[Unhandled promise rejection: TypeError: undefined is not an object (evaluating '_PushTokenManager.default.getDevicePushTokenAsync')]
Make sure you import {Notifications} from 'expo' and not the other way from 'expo-notifications'. Was having the same problem but then tried importing it from the 'expo' and it worked. Check out this image
make sure you are in managed workflow, if you are in bare you should specify { experienceId}
code example from the docs:
import Constants from 'expo-constants';
import * as Notifications from 'expo-notifications';
export async function registerForPushNotificationsAsync(userId: string) {
let experienceId = undefined;
if (!Constants.manifest) {
// Absence of the manifest means we're in bare workflow
experienceId = '@username/example';
}
const expoPushToken = await Notifications.getExpoPushTokenAsync({
experienceId,
});
await fetch('https://example.com/', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
userId,
expoPushToken,
}),
});
}
experienceId (string) -- The ID of the experience to which the token should be attributed. Defaults to Constants.manifest.id exposed by expo-constants. In the bare workflow, you must provide a value which takes the shape @username/projectSlug, where username is the Expo account that the project is associated with, and projectSlug is your slug from app.json
for more info: https://docs.expo.io/versions/latest/sdk/notifications/#dailytriggerinput
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With