[英]Sending notifications to Google Cloud Messaging with php gives me Unauthorized Error 401
[英]Getting Unauthorized Error 401 in Firebase Cloud Messaging in cURL command
我要發送Firebase雲消息傳遞,而我遇到的問題是我收到了一個未經授權的錯誤401。
我從Firebase網站獲得了安全密鑰,然后進行了設置。 設備令牌已經在數據庫中,我從數據庫讀取沒有問題。 到目前為止這是我的代碼:
<?php
function send_notification($tokens, $message)
{
$url = 'https://fcm.googleapis.com/fcm/send';
$fields = array('registration_ids' => $tokens, 'data' => $message);
$headers = array('Authorization: key=' . "My Firebase key", 'Content-Type: application/json');
echo "work well before init curl";
$ch = curl_init();
echo "init well";
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, $fields);
echo "init finishe well";
$result = curl_exec($ch);
echo "Execute to get result";
if ($result === false) {
die('Curl failed: ' . curl_error($ch));
}
curl_close($ch);
echo "function finished well";
return $result;
}
$conn = mysqli_connect("localhost", "root", "mysql_password", "FCM") or die("Error connecting");
$sql = " Select Token From users";
$result = mysqli_query($conn, $sql);
$tokens = array();
if (mysqli_num_rows($result) > 0) {
while ($row = mysqli_fetch_assoc($result)) {
$tokens[] = $row["Token"];
}
}
mysqli_close($conn);
$message = array("message" => " FCM PUSH NOTIFICATION TEST MESSAGE");
$message_status = send_notification($tokens, $message);
echo $message_status;
?>
使用FCM時,必須使用Firebase控制台的Cloud Messaging選項卡中顯示的服務器密鑰作為請求中的Authorization
值。
使用https://developers.google.com/instance-id/reference/server上的文檔,我想使用https://iid.googleapis.com/iid/info/IID_TOKEN獲取有關應用實例的信息。
在我的應用程序的代碼中,我使用它來獲取IDD_TOKEN
:
// Get token using example from https://github.com/firebase/quickstart-android/blob/master/messaging/app/src/main/java/com/google/firebase/quickstart/fcm/java/MainActivity.java.
// [START retrieve_current_token]
FirebaseInstanceId.getInstance().getInstanceId()
.addOnCompleteListener(new OnCompleteListener<InstanceIdResult>() {
@Override
public void onComplete(@NonNull Task<InstanceIdResult> task) {
if (!task.isSuccessful()) {
Log.w(TAG, "getInstanceId failed", task.getException());
return;
}
// Get new Instance ID token
String token = task.getResult().getToken();
System.out.println("task.getResult().getToken() = "+token);
// Log and toast
String msg = getString(R.string.msg_token_fmt, token);
Log.d(TAG, msg);
Toast.makeText(FirstActivity.this, msg, Toast.LENGTH_SHORT).show();
}
});
// [END retrieve_current_token]
然后在Android Studio日志中,我找到了令牌(我正在更改令牌的值,因為我不想在這里發布真實的令牌):
I/System.out: task.getResult().getToken() = bepGr4F2b0Q:APB91cEAKsN1bGjMm5xsobxBHxuYZLfioTPMEIN90njdiK5C2MnOYF5NcOy6ot6XFanMTBIoKRGcyev5RJuydGWt1XHwsniNZ6h3Pjvn9Fqth-Mqgj_2-YN9pv_nMAugG8blc5boeDyH
這意味着IDD_TOKEN
是bepGr4F2b0Q:APB91cEAKsN1bGjMm5xsobxBHxuYZLfioTPMEIN90njdiK5C2MnOYF5NcOy6ot6XFanMTBIoKRGcyev5RJuydGWt1XHwsniNZ6h3Pjvn9Fqth-Mqgj_2-YN9pv_nMAugG8blc5boeDyH
。 所以在我的PHP代碼中我使用了這個:
$curlUrl = "https://iid.googleapis.com/iid/info/bepGr4F2b0Q:APB91cEAKsN1bGjMm5xsobxBHxuYZLfioTPMEIN90njdiK5C2MnOYF5NcOy6ot6XFanMTBIoKRGcyev5RJuydGWt1XHwsniNZ6h3Pjvn9Fqth-Mqgj_2-YN9pv_nMAugG8blc5boeDyH?details=true";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $curlUrl);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
//turning off the server and peer verification(TrustManager Concept).
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization:key=[My server key]'));
//getting response from server
$response = curl_exec($ch);
print_r($response);
找到[My server key]
在哪里? 您可以在https://console.firebase.google.com/上找到它。 轉到Cloud Messaging
選項卡,在Project credentials
部分中,您將找到Server key
:
結果如下:
{“applicationVersion”:“50”,“connectDate”:“2019-05-09”,“attestatus”:“NOT_ROOTED”,“application”:“[我的包名]”,“范圍”:“*”,“ authorizedEntity“:”[我的Firebase發件人ID]“,”rel“:{”topics“:{”Toronto“:{”addDate“:”2019-05-09“}}},”appSigner“:”[我的appSigner ]”, “平台”: “ANDROID”}
那是正確的。 我可以說這些值對應於我連接的設備的IID_TOKEN
,我可以識別並且一切都是正確的。 我可以確認我訂閱了我的設備到該主題,一切正常! 我能夠使用https://iid.googleapis.com/iid/info/IID_TOKEN獲取有關我使用Firebase連接的Android設備的應用實例的相關信息。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.