从 Laravel 发送 Firebase 推送通知

问题描述 投票:0回答:1

我开发一个 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 swift laravel firebase firebase-cloud-messaging
1个回答
3
投票

经过一番研究,我找到了问题的解决方案。为了适用于 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
);
© www.soinside.com 2019 - 2024. All rights reserved.