无法使用EXPO发送推送通知

时间:2018-04-11 09:22:51

标签: react-native push-notification react-native-android expo

我正在尝试使用EXPO向我的本机应用程序发送推送通知,但出于某种原因,手机没有收到任何通知。

我的代码如下所示:

import React from 'react';
import {Button, StyleSheet, Text, View} from 'react-native';
import {StackNavigator} from 'react-navigation';
import Checklist from "./src/components/Checklist/Checklist";
import Main from "./src/components/Main/Main";
import Product from "./src/components/Product/Product";
import {Notifications,Permissions} from 'expo';

export default class AppContainer extends React.Component {
    state = {
        notification: {},
    };

    componentDidMount() {
        registerForPushNotificationsAsync();

        // Handle notifications that are received or selected while the app
        // is open. If the app was closed and then opened by tapping the
        // notification (rather than just tapping the app icon to open it),
        // this function will fire on the next tick after the app starts
        // with the notification data.
        this._notificationSubscription = Notifications.addListener(this._handleNotification);
    }

    _handleNotification = (notification) => {
        this.setState({notification: notification});
    };

    render() {
        return (
            <View style={{flex: 1, justifyContent: 'center', alignItems: 'center'}}>
                <Text>Origin: {this.state.notification.origin}</Text>
                <Text>Data: {JSON.stringify(this.state.notification.data)}</Text>
            </View>
        );
    }
}

const PUSH_ENDPOINT = 'https://your-server.com/users/push-token';

async function registerForPushNotificationsAsync() {
    const { status: existingStatus } = await Permissions.getAsync(
        Permissions.NOTIFICATIONS
    );
    let finalStatus = existingStatus;

    // 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();
    console.log("-------------------------------------------------------------------------------");
    console.log(token);

    // 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',
            },
        }),
    });
}

我正在尝试通过https://expo.io/dashboard/notifications发送通知用于测试目的,突然我读到了这个: 注意:仅在标准的Expo项目中支持Expo推送通知。分离的应用程序和create-react-native-app都不支持它们。

我想知道EXPO的推送通知是否不适用于使用 create-react-native-app 创建的分离和应用(在我的情况下),这个展会将起什么作用?

2 个答案:

答案 0 :(得分:1)

要使用推送通知,您需要一个Expo帐户并安装exp命令行。

请参阅:https://forums.expo.io/t/push-notifications-not-working-with-crna/858

要了解standard CRNA app的含义,请参阅此讨论:https://github.com/react-community/create-react-native-app/issues/153

答案 1 :(得分:-1)

对于启动项目,您必须从CMD转到proyect文件夹。 在我启动CMD的情况下,我看到C:\ Users \ Manuel。所以..我这样做Going to my proyect folder

For starting proyect do this

And for compiling do this