首页 > 解决方案 > 如何从网络向 Expo 发送通知?

问题描述

我正在尝试从我的网站向我的应用程序的用户发送通知,但它不起作用。这就是我现在所拥有的:

  async function sendPushNotification(expoPushToken, title, description) {
    const message = {
      to: expoPushToken,
      sound: "default",
      title: title,
      body: description,
      badgecount: 1,
    };

    axios({
      method: "post",
      url: "https://exp.host/--/api/v2/push/send",
      headers: {
        Accept: "application/json",
        "Accept-encoding": "gzip, deflate",
        "Content-Type": "application/json",
      },
      body: message,
    }).then(function (response) {
      alert(JSON.stringify(response));
    });
  }

我试图用 Axios 来做,但没有运气。他们使用该方法解释的原始await fetch方法也不起作用。我根本没有得到回应。

任何帮助将非常感激。

标签: react-nativeexpo

解决方案


您可以查看他们的推送通知文档
示例代码

async function schedulePushNotification() { await Notifications.scheduleNotificationAsync({ content: { title: "You've got mail! ", body: 'Here is the notification body', data: { data: 'goes here' }, }, trigger: { seconds: 2 }, }); }


推荐阅读