提问人:Timothy 提问时间:3/31/2023 最后编辑:CBroeTimothy 更新时间:3/31/2023 访问量:139
向 firebase 发送请求时令牌无效
Token is invalid while sending request to firebase
问:
我正在Symfony网站上工作,并希望在生成令牌并向firebase服务器发送请求时推送通知。然后它说令牌无效。
我正在像这样生成令牌。
use Firebase\JWT\JWT;
use Doctrine\ORM\EntityManagerInterface;
use Kreait\Firebase\Factory;
use Kreait\Firebase\Messaging\CloudMessage;
use Kreait\Firebase\Messaging\Notification;
use Kreait\Firebase\ServiceAccount;
$driver_id = $driver->getId();
$serviceAccount = ServiceAccount::fromValue(__DIR__.'/../../../firebase_credentials.json');
$firebase = (new Factory)
->withServiceAccount($serviceAccount)
->createDatabase();
$payload = [
'iss' => $serviceAccount->getClientEmail(),
'sub' => $serviceAccount->getClientEmail(),
'aud' => 'https://identitytoolkit.googleapis.com/google.identity.identitytoolkit.v1.IdentityToolkit',
'iat' => time(),
'exp' => time() + 60 * 60, // Set the expiration time to one hour from now
'uid' => $driver_id, // Set the UID to the driver's ID
];
$privateKey = $serviceAccount->getPrivateKey();
$token = JWT::encode($payload, $privateKey, 'RS256');
// save the browser token to the database
$driver->setBrowserToken($token);
$token = $driver->getBrowserToken();
在这里,我调用了向火力基地发送请求的方法
$title = 'New trip assign to you.';
$message = $dispatcherName . ' is assign new trip to you.';
if (!empty($token)) {
$response = $this->forward('App\Controller\SendNotification::webNotification', [
'token' => $token,
'title' => $title,
'message' =>$message
]);
}
这是向服务器发送请求的方法
public function webNotification( $token, $title, $message)
{
$msg = array(
'title' => $title,
'body' => $message,
'icon' => 'myicon',/*Default Icon*/
'sound' => 'mySound',/*Default sound*/
'click_action' => 'FLUTTER_NOTIFICATION_CLICK', /*Action when notification is clicked*/
'data' => array(
'message' => $message,
'title' => $title
)
);
$fields = array(
'to' => array($token),
'data' => array('notification' => $msg)
);
$headers = array(
'Authorization: key=' . $_ENV['FIREBASE_SERVER_KEY']
);
try {
// Send notification to Firebase Cloud Messaging server
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://fcm.googleapis.com/fcm/send');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
$result = curl_exec($ch);
if ($result === false) {
throw new \Exception(curl_error($ch));
}
通过使用dd($result),我得到了这个结果
"{"multicast_id":3241703823291433054,"success":0,"failure":1,"canonical_ids":0,"results":[{"error":"InvalidRegistration"}]}"
答: 暂无答案
评论