我正在编写一个iOS应用程序,使用laravel for 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设备上成功运行。
答案 0 :(得分:0)
经过研究,我找到了解决我问题的方法。为了在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
);