"key=xxxxxxxxxxxxxxxxxxx"); $data -6ren">
gpt4 book ai didi

c# - 该请求缺少身份验证 key (FCM token )

转载 作者:行者123 更新时间:2023-11-30 00:41:22 25 4
gpt4 key购买 nike

我尝试从 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,但在浏览器中我看到错误:

请求缺少身份验证 key (FCM token )。请参阅 FCM 文档的“身份验证”部分,位于 https://firebase.google.com/docs/cloud-messaging/server .错误 401

此外,我可以从 c# 控制台应用程序发送推送通知并在我的手机上接收它(我认为,该身份验证 key 是正确的):

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)正确发送推送通知?我的代码有什么问题?

最佳答案

你能不能试试下面的方法-

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

尝试发送带有内容类型的请求。

关于c# - 该请求缺少身份验证 key (FCM token ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42526399/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com