【问题标题】:The request was missing an Authentification Key (FCM Token)请求缺少身份验证密钥(FCM 令牌)
【发布时间】:2017-03-01 08:11:52
【问题描述】:

我尝试从 PHP 向 Android 手机应用发送推送通知

$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); 

我使用 Google Cloud Messaging,但在浏览器中我看到错误:

请求缺少验证密钥(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)发送推送通知?我的代码有什么问题?

【问题讨论】:

  • 嗨。您可以在发送之前将生成的有效负载发布到您的 PHP 脚本中吗?
  • 嗨。代码:'$data = array("to"=>"/topics/global", "data"=>array ("message"=>"这是通知"));回声 json_encode($data,JSON_UNESCAPED_SLASHES);'在浏览器中:{"to":"/topics/global","data":{"message":"这是通知"}}
  • 嗯。您可以尝试使用Postman 或仅使用简单的cURL request 来执行请求吗?看看是不是一样的反应?
  • 我收到了来自 Postman 的消息,之后我的 PHP 脚本就可以工作了。它的奇迹?
  • 哇。这很奇怪。

标签: c# php android google-cloud-messaging firebase-cloud-messaging


【解决方案1】:

你可以试试下面的方法吗-

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

尝试使用内容类型发送您的请求。

【讨论】:

猜你喜欢
  • 2017-04-05
  • 2017-12-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-20
  • 1970-01-01
  • 2019-02-21
  • 2016-02-29
相关资源
最近更新 更多