从Laravel发送Firebase推送通知

莱万·卡拉纳泽(Levan Karanadze)

我正在编写一个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设备上成功运行。

莱万·卡拉纳泽(Levan Karanadze)

经过一些研究,我找到了解决我问题的方法。为了在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
);

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章