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/ –