【问题标题】:Cannot send push notification using EXPO无法使用 EXPO 发送推送通知
【发布时间】:2018-09-21 02:45:56
【问题描述】:

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

我的代码如下:

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(在我的情况下)创建的分离应用程序和应用程序(在我的情况下)这个 expo 将适用于什么?

【问题讨论】:

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


    【解决方案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

    【讨论】:

    • 感谢您的回复,您知道我应该如何或编写什么命令来启动一个支持推送通知且不需要 android studio 或 xcode 将应用程序编译为 apk 的 react native 项目或ipa!
    【解决方案2】:

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

    For starting proyect do this

    And for compiling do this

    【讨论】:

    • 这个答案不能帮助用户解决推送通知的问题并且不相关。如果您有任何与推送通知相关的提示,请编辑您的问题以包含这些提示
    • 我的问题是回答这个问题:“你知道我应该如何或写什么命令来启动一个支持推送通知并且不需要 android studio 或 xcode 编译应用程序的 react native 项目作为 apk 或 ipa!”
    猜你喜欢
    • 2019-11-07
    • 1970-01-01
    • 1970-01-01
    • 2018-07-10
    • 1970-01-01
    • 1970-01-01
    • 2020-08-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多