Рома Рома
Рома Рома

Reputation: 11

The request was missing an Authentification Key (FCM Token)

I try to send push notification to Android Phone App from PHP

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

I use Google Cloud Messaging, but in browser I see error:

The request was missing an Authentification Key (FCM Token). Please, refer to section "Authentification" of the FCM documentation, at https://firebase.google.com/docs/cloud-messaging/server. Error 401

Also, I can send push notification from c# Console Application and receive it on my phone (and I think, that authentification key is right) :

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();
    }
}
}

And question: How to send push notification from PHP (use GCM) correctly? What's wrong with my code?  

Upvotes: 1

Views: 1307

Answers (1)

Ambuj Kathotiya
Ambuj Kathotiya

Reputation: 369

Can you try in the following way -

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

Try sending your request with content-type.

Upvotes: 1

Related Questions