【问题标题】:Attempting to send push notification from my laravel server to expo not working尝试从我的 laravel 服务器发送推送通知到 expo 不起作用
【发布时间】:2020-05-08 01:23:05
【问题描述】:

这是我向 expo api 发出 post 请求时收到的 api 错误:

{"errors":[{"code":"API_ERROR","message":"child \"to\" fails because [\"to\" is required], \"value\" must be an
array."}]}

这是我想要提出的要求

$payload=json_encode([
       'to' => "ExponentPushToken[xxxxxxxxxxxxxxxx]",
       'title'=>'Hello',
       'body'=>'hello World',


   ]);

     $response = Http::withHeaders([
         'Host'=>'exp.host',
         'accept'=>'application/json',
         'Content-Type' => 'application/json',
         'accept-encoding'=>'grizp,deflate',
     ])->post('https://exp.host/--/api/v2/push/send', [
         'debug' => TRUE,
         'body' => $payload,
     ]);

但是当我使用 javascript 从我的 expo 应用程序进行 api 调用时,它工作正常

  sendPushNotification = async () => {
    const message = {
      to: this.state.expoPushToken,
      sound: 'default',
      title: 'Hello',
      body: 'Hello World',
      data: { data: 'goes here' },
      _displayInForeground: true,
    };

    const response = await fetch('https://exp.host/--/api/v2/push/send', {
      method: 'POST',
      headers: {
        Accept: 'application/json',
        'Accept-encoding': 'gzip, deflate',
        'Content-Type': 'application/json',
      },
      body: JSON.stringify(message),
    });
  };

【问题讨论】:

  • 尝试将to 作为'to' => ["ExponentPushToken"] 之类的数组发送?
  • @UzairRiaz 不幸的是这不起作用,同样的错误

标签: laravel react-native push-notification expo


【解决方案1】:

将您的 $payload 变量更改为这样的对象数组:

$payload = json_encode([
            [
                'to' => "ExponentPushToken[xxxxxxxxxxxxxxxx]",
                'title' => 'Hello',
                'body' => 'hello World',
            ]
        ]);

【讨论】:

  • 这对我也不起作用,谢谢您的建议,问题是当我从移动应用程序(expo 应用程序)拨打 api 电话时,它可以工作,但是当我从 laravel 拨打电话时服务器应用程序返回错误消息
  • 能否发布您从移动应用发送的有效负载?
  • 我编辑了问题并将我的移动应用程序中的 fetch javascript 代码添加到它
猜你喜欢
  • 2019-08-07
  • 1970-01-01
  • 1970-01-01
  • 2017-09-07
  • 1970-01-01
  • 2019-11-07
  • 1970-01-01
  • 2016-09-27
  • 1970-01-01
相关资源
最近更新 更多