【发布时间】: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