0

我嘗試推送通知從PHP請求缺少的鑑別密鑰(FCM令牌)

$headers = array("Authorization"=>"key=xxxxxxxxxxxxxxxxxxx"); 
$data = array("to"=>"/topics/global", array ("message"=>"This is notification")); 

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, 'https://gcm-http.googleapis.com/gcm/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($data,JSON_UNESCAPED_SLASHES)); 
curl_exec($ch); 
curl_close($ch); 

我使用谷歌雲消息發送到Android手機應用程序,但在瀏覽器中我看到的錯誤:

該請求缺少認證密鑰(FCM令牌)。請參閱FCM文檔的「身份驗證」部分,網址爲https://firebase.google.com/docs/cloud-messaging/server。 錯誤401

另外,我可以從C#控制檯應用程序發送推送通知和收到我的手機上(和我認爲,認證密鑰是正確的):

using System; 
using System.Net.Http; 
using System.Net.Http.Headers; 
using System.Text; 
using System.Threading.Tasks; 
using Newtonsoft.Json.Linq; 

namespace MessageSender 
{ 
    class MessageSender 
    { 
    public const string API_KEY = "xxxxxxxx"; 
    public const string MESSAGE = "This is notification"; 

    static void Main(string[] args) 
    { 
     var jGcmData = new JObject(); 
     var jData = new JObject(); 

     jData.Add("message", MESSAGE); 
     jGcmData.Add("to", "/topics/global"); 
     jGcmData.Add("data", jData); 

     var url = new Uri("https://gcm-http.googleapis.com/gcm/send"); 
     try 
     { 
      using (var client = new HttpClient()) 
      { 
       client.DefaultRequestHeaders.Accept.Add(
        new MediaTypeWithQualityHeaderValue("appslication/json")); 

       client.DefaultRequestHeaders.TryAddWithoutValidation(
        "Authorization", "key= " + API_KEY); 

       Task.WaitAll(client.PostAsync(url, 
        new StringContent(jGcmData.ToString(), Encoding.Default, "application/json")) 
         .ContinueWith(response => 
         { 
          Console.WriteLine(response); 
          Console.WriteLine("Message sent: check the client device notification tray."); 
         })); 
      } 
     } 
     catch (Exception e) 
     { 
      Console.WriteLine("Unable to send GCM message:"); 
      Console.Error.WriteLine(e.StackTrace); 
     } 

     Console.ReadLine(); 
    } 
} 
} 

和問題:如何發送推送來自PHP的通知(使用GCM)正確?我的代碼有什麼問題?

+0

嗨。您可以在發送之前將生成的有效內容發佈到您的PHP腳本中嗎? –

+0

嗨。代碼:'$ data = array(「to」=>「/ topics/global」,「data」=> array(「message」=>「This is notification」)); echo json_encode($ data,JSON_UNESCAPED_SLASHES);' 在瀏覽器中:{「to」:「/ topics/global」,「data」:{「message」:「這是通知」}} –

+0

嗯。您可以使用[郵遞員]嘗試執行請求(http://stackoverflow.com/documentation/firebase-cloud-messaging/8242/firebase-cloud-messaging/26577/sending-downstream-messages-using-postman#t=201703010932122186642 )或者只是一個簡單的[cURL請求](http://stackoverflow.com/documentation/firebase-cloud-messaging/8242/firebase-cloud-messaging/26480/sending-downstream-messages-via-curl#t=201703010932129373534) ?看看它是相同的迴應? –

回答

1

你可以通過以下方式嘗試 -

$headers = array(
     'Authorization: key=' . GOOGLE_API_KEY, 
     'Content-Type: application/json' 
    ); 

嘗試發送與內容類型您的要求。

+0

我做到了,得到了同樣的錯誤 –

+0

我建議你使用FCM。無論如何,看看這個教程 - https://www.simplifiedcoding.net/android-push-notification-using-gcm-tutorial/ –