【问题标题】:Sending Firebase push notifications from Laravel从 Laravel 发送 Firebase 推送通知
【发布时间】:2019-11-11 13:17:48
【问题描述】:

我正在编写一个 iOS 应用程序,使用 laravel 的 API 和 google firebase 的推送通知。当我使用 firebase 云消息发送推送通知时,它会发送到我的设备。当我使用 laravel 发送推送通知时,它不会影响。这是我从 laravel 发送推送通知的脚本:

function sendNotification(Request $request)
{
    $friendToken = [];
    $usernames = $request->all()['friend_usernames'];
    $dialog_id = $request->all()['dialog_id'];
    foreach ($usernames as $username) {
        $friendToken[] = DB::table('users')->where('user_name', $username)
            ->get()->pluck('device_token')[0];
    }

    $url = 'https://fcm.googleapis.com/fcm/send';
    foreach ($friendToken as $tok) {
        $fields = array(
            'to' => $tok,
            'data' => $message = array(
                "message" => $request->all()['message'],
                "dialog_id" => $dialog_id
            )
        );
        $headers = array(
            'Authorization: key=*mykey*',
            'Content-type: Application/json'
        );
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
        curl_exec($ch);
        curl_close($ch);
    }

    $res = ['error' => null, 'result' => "friends invited"];

    return $res;
}

它返回一个成功的结果,但通知没有发送到 iOS 设备。

PS:它可以在 Android 设备上成功运行。

【问题讨论】:

  • 您确定要从设备获取 ios 令牌吗?
  • 我将 fcmTokens 保存在数据库中。
  • 你能告诉我在 Laravel 中如何以及在哪里存储 firebase 凭据 json 文件吗?

标签: ios swift laravel firebase firebase-cloud-messaging


【解决方案1】:

经过一些研究,我找到了解决问题的方法。为了适用于 iOS 平台,我们需要在字段中添加通知:

$notification = array('title' =>"" , 'text' => $request->all()['message']);
$fields = array(
    'to' => $tok,
    'data' => $message = array(
        "message" => $request->all()['message'],
        "dialog_id" => $dialog_id
    ),
    'notification' => $notification
);

【讨论】:

  • 只想知道如何将收到的令牌发送到 laravel 中的数据库
  • 只是一个提示。您可以使用$request->only('message') 而不是$request->all()['message'] 从请求中获取消息。 :)
猜你喜欢
  • 2019-03-18
  • 2018-02-17
  • 1970-01-01
  • 1970-01-01
  • 2017-12-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-07-16
相关资源
最近更新 更多