import { useState, useEffect, useRef } from 'react';
import * as Notifications from 'expo-notifications';
import * as Device from 'expo-device';
import { Platform, Alert } from 'react-native';
import Constants from 'expo-constants';
function handleRegistrationError(errorMessage: string) {
throw new Error(`Push notification registration failed: ${errorMessage}`);
}
export async function registerForPushNotificationsAsync() {
if (Platform.OS === 'android') {
Notifications.setNotificationChannelAsync('default', {
name: 'default',
importance: Notifications.AndroidImportance.MAX,
vibrationPattern: [0, 250, 250, 250],
lightColor: '#FF231F7C',
});
}
if (Device.isDevice) {
const { status: existingStatus } =
await Notifications.getPermissionsAsync();
let finalStatus = existingStatus;
if (existingStatus !== 'granted') {
const { status } = await Notifications.requestPermissionsAsync();
finalStatus = status;
}
if (finalStatus !== 'granted') {
handleRegistrationError(
'Failed to get push token for push notification!'
);
return;
}
const projectId =
Constants?.expoConfig?.extra?.eas?.projectId ??
Constants?.easConfig?.projectId;
if (!projectId) {
handleRegistrationError(
'Project ID is not defined in the Expo configuration.'
);
}
try {
const token = (await Notifications.getExpoPushTokenAsync({
projectId,
})).data;
console.log('Push notification token:', token);
return token;
}catch (error) {
handleRegistrationError(
`Failed to get push token: ${error instanceof Error ? error.message : String(error)}`
);
}
}
}
< /code>
Однако, когда я создаю приложение для производства, используя EAS -сборку и устанавливаю его через Testflight, токен не возвращается. Вот что я подтвердил до сих пор: < /p>
{
"expo": {
"name": "test",
"slug": "test",
"version": "1.0.7",
"orientation": "portrait",
"icon": "./assets/icon.png",
"scheme": "app",
"userInterfaceStyle": "automatic",
"newArchEnabled": true,
"ios": {
"supportsTablet": false,
"bundleIdentifier": "com.test.mynewapp",
"entitlements": {
"com.apple.developer.push-notifications": true
},
"infoPlist": {
"ITSAppUsesNonExemptEncryption": false,
"UIDeviceFamily": [
1
],
"NSCameraUsageDescription": "This app does not use the camera. This is required for compliance due to included libraries."
}
},
"android": {
"icon": "./assets/icon.png",
"adaptiveIcon": {
"foregroundImage": "./assets/images/adaptive-icon.png",
"backgroundColor": "#ffffff"
},
"edgeToEdgeEnabled": true,
"package": "com.test.mynewapp"
},
"web": {
"bundler": "metro",
"output": "static",
"favicon": "./assets/images/favicon.png"
},
"plugins": [
"expo-router",
[
"expo-splash-screen",
{
"image": "./assets/icon.png",
"imageWidth": 200,
"resizeMode": "contain",
"backgroundColor": "#ffffff"
}
],
[
"expo-notifications",
{
"enableBackgroundRemoteNotifications": true
}
]
],
"experiments": {
"typedRoutes": true
},
"extra": {
"router": {},
"eas": {
"projectId": "8e56511a-ec55-7343-v31f-63g111019c98"
}
}
}
}
Подробнее здесь: https://stackoverflow.com/questions/796 ... rks-in-exp