为什么ExponentPush令牌为null我console.log令牌它显示为null
................................................... ................................................... ................................................... ................................................... ................................................... ................................................... .....................

见下图:



push_notifications.js



import {Permissions, Notifications} from 'expo';
import axios from 'axios';
import {AsyncStorage} from 'react-native';

const PUSH_ENDPOINT = 'http://rallycoding.herokuapp.com/api/tokens';
export default async () => {
    let previousToken = await AsyncStorage.getItem('pushtoken');
    console.log(previousToken);
    if (previousToken) {
        return;
    } else {
        let { status } = await Permissions.askAsync(Permissions.NOTIFICATIONS);
        if (status !== 'granted') {
            return;
        }
        let token = await Expo.Notifications.getExpoPushTokenAsync();
        await  axios.post(PUSH_ENDPOINT, {token: {token}});
        AsyncStorage.setItem('pushtoken', token);
    }

};

最佳答案

let previousToken = await AsyncStorage.getItem('pushtoken');
console.log(previousToken);


您正在尝试访问AsyncStorage以找到具有键pushtoken的项目。如果此应用程序首次运行,它将为null。您需要至少执行一次setItem

10-06 11:51