firebase messaging api php subscribe to topic 411 error - php

I am trying to implement notifications into my web app. I have this php file in which I send notifications:
<?php
function sendGCM($title,$message, $id) {
$url = 'https://fcm.googleapis.com/fcm/send';
$fields = array (
'registration_ids' => array (
$id
),
"notification" => array(
"title" => $title,
"body" => $message,
"click_action" => "https://google.com"
)
);
$fields = json_encode ( $fields );
$headers = array (
'Authorization: key=' . $MY_KEY,
'Content-Type: application/json'
);
$ch = curl_init ();
curl_setopt ( $ch, CURLOPT_URL, $url );
curl_setopt ( $ch, CURLOPT_POST, true );
curl_setopt ( $ch, CURLOPT_HTTPHEADER, $headers );
curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt ( $ch, CURLOPT_POSTFIELDS, $fields );
$result = curl_exec ( $ch );
echo $result;
curl_close ( $ch );
}
?>
Now what I want to do is to send a notification to a topic instead of individual ids. On this page it shows how you can subscribe an id to a topic. Here is the function I created to do that:
function createTopic($topic,$id) {
$url = 'https://iid.googleapis.com/iid/v1/' . $id . '/rel/topics/' . $topic;
$headers = array (
'Authorization: key=MY_KEY'
);
$ch = curl_init ();
curl_setopt ( $ch, CURLOPT_URL, $url );
curl_setopt ( $ch, CURLOPT_POST, true );
curl_setopt ( $ch, CURLOPT_HTTPHEADER, $headers );
curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, true );
$result = curl_exec ( $ch );
echo $result;
curl_close ( $ch );
}
I am getting this error from Google and I can't figure out what is going wrong:
That’s an error.
POST requests require a Content-length header. That’s all we know.
Any help?

The example in the documentation shows use of a Content-Length header:
https://iid.googleapis.com/iid/v1/nKctODamlM4:CKrh_PC8kIb7O...clJONHoA/rel/topics/movies
Content-Type:application/json
Content-Length: 0
Authorization:key=AIzaSyZ-1u...0GBYzPu7Udno5aA
Add that to your request:
$headers = array (
'Authorization: key=' . $MY_KEY,
'Content-Length: 0'
);

Related

Push notification from PHP to an android app with FCM

I am a beginner in PHP and I would like to make a program to send a simple notification (a title and a little message) from a localhost (in PHP) to an android app. For this, I use Firebase. For the moment, I can send a notification from FCM to android but I cannot do it from the PHP program to the android app. I tried a lot of different way and program to send this notification and it's always a failure.
This is the program I use :
<html>
<?php
$read = shell_exec ('gpio read 0');
$status = intval($read);
if ($status == 1) {
function sendFCM($mess,$id) {
$url = 'https://fcm.googleapis.com/fcm/send';
$fields = array (
'AAAAS4YkIPw:A.........rLQjQ' => $id,
'notification' => array (
"body" => $mess,
"Doorbell" => "Title",
// "icon" => "myicon"
)
);
$fields = json_encode ( $fields );
$headers = array (
'Authorization: key=' . "AIzaSy.....J_Hy0",
'Content-Type: application/json'
);
$ch = curl_init ();
curl_setopt ( $ch, CURLOPT_URL, $url );
curl_setopt ( $ch, CURLOPT_POST, true );
curl_setopt ( $ch, CURLOPT_HTTPHEADER, $headers );
curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt ( $ch, CURLOPT_POSTFIELDS, $fields );
$result = curl_exec ( $ch );
curl_close ( $ch );
}
print ("oui");
sleep(30);
header("Refresh:0");
}
else {
print ("non");
sleep(2);
header("Refresh:0");
}
?>
</html>
In each program I used, maybe I made the same mistake every time but I don't manage to find it. Just to explain, this program read a GPIO and as function of the result, he tell "oui" or "non" and send a notification wend it's "oui". The program I took on a website is between the line 6 and the line 30. All the rest work. My problem is must I don't receive notifications.
I change my program after I saw one error so this is the new one.
<html>
<head>
</head>
<?php
function sendFCM($mess,$id) {
$url = 'https://fcm.googleapis.com/fcm/send';
$fields = array (
'AAAAS4Yk.........KirLQjQ' => $id,
'notification' => array (
"body" => $mess,
"Doorbell" => "Title",
// "icon" => "myicon"
)
);
$fields = json_encode ( $fields );
$headers = array (
'Authorization: key=>' . "$ip",
'Content-Type: application/json'
);
$ch = curl_init ();
curl_setopt ( $ch, CURLOPT_URL, $url );
curl_setopt ( $ch, CURLOPT_POST, true );
curl_setopt ( $ch, CURLOPT_HTTPHEADER, $headers );
curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt ( $ch, CURLOPT_POSTFIELDS, $fields );
$result = curl_exec ( $ch );
curl_close ( $ch );
}
$read = shell_exec ('gpio read 0');
$status = intval($read);
if ($status == 1) {
sendFCM('Yolo','AIzaSy......xdJ_Hy0');
print ("oui");
sleep(30);
header("Refresh:0");
}
else {
print ("non");
sleep(2);
header("Refresh:0");
}
?>
</html>

InvalidRegistration for FCM in php?

I am receiving an [InvalidRegistration] error in send_fcm message
Following is the code that I have used:
<?php
define("GOOGLE_SERVER_KEY", "AAAAgTwiaRM:APA91bFyOiLtJKim7FIKpW...Mk6rR4xlTNDr1Ia0sJmerecuD6U1c6xsQh8PZdKz");
function send_fcm($message, $id) {
$url = 'https://fcm.googleapis.com/fcm/send';
$headers = array (
'Authorization: key='.GOOGLE_SERVER_KEY,
'Content-Type: application/json'
);
$fields = array (
'data' => array ("message" => $message),
'notification' => array ("body" => $message)
);
if(is_array($id)) {
$fields['registration_ids'] = $id;
} else {
$fields['to'] = $id;
}
$fields['priority'] = "high";
$fields = json_encode ($fields);
$ch = curl_init ();
curl_setopt ( $ch, CURLOPT_URL, $url );
curl_setopt ( $ch, CURLOPT_POST, true );
curl_setopt ( $ch, CURLOPT_HTTPHEADER, $headers );
curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt ( $ch, CURLOPT_POSTFIELDS, $fields );
$result = curl_exec ( $ch );
if ($result === FALSE) {
die('FCM Send Error: ' . curl_error($ch));
}
curl_close ( $ch );
return $result;
}
$message_status= send_fcm("test", "sketch_up");
echo $message_status;
?>
After I execute the above code, I find the error as mentioned below
{"multicast_id":7765951050747928269,"success":0,"failure":1,"canonical_ids":0,"results":[{"error":"InvalidRegistration"}]}
What could be the root cause - how may I fix?

Firebase Cloud Message not multicast_id received in response

I send notifications in my server with PHP to FCM with this function:
function sendGCM($ch1,$ch2, $message) {
$url = 'https://fcm.googleapis.com/fcm/send';
$fields = array (
'to' => $ch1
,
'data' => array (
"message" => $message
)
);
$fields = json_encode ( $fields );
$headers = array (
'Authorization: key=' . $appID,
'Content-Type: application/json'
);
$ch = curl_init ();
curl_setopt ( $ch, CURLOPT_URL, $url );
curl_setopt ( $ch, CURLOPT_POST, true );
curl_setopt ( $ch, CURLOPT_HTTPHEADER, $headers );
curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt ( $ch, CURLOPT_POSTFIELDS, $fields );
$result = curl_exec ( $ch );
curl_close ( $ch );
echo $result;
}
The response is this {"message_id":9191006033691432523}. In the documentation, https://firebase.google.com/docs/cloud-messaging/http-server-ref multicast_id, success, failure parameters are required in the response but.
What is the reason for this?
You are sending message to specified by $ch1 topic, if I understand ok, instead to multiple devices by their registrationIds.
So you are reciving Topic message HTTP response body (JSON) (table 6)
instead of Downstream HTTP messages
From what I've understood from FCM and your question is that you'll get the multicast_id in the response only when you are sending notifications to unique devices and not in case of topic notifications.

Firebase PHP don't work for IOS

I have a small problem
I use FireBase for push notification on iOS and Android.
On Android it work, on iOS it work only when i send message by the console when i send message with PHP it's not good
My php code
function sendGCM($message, $id) {
$url = 'https://fcm.googleapis.com/fcm/send';
$fields = array (
'registration_ids' => array (
$id
),
'data' => array (
"message" => $message
)
);
$fields = json_encode ( $fields );
$headers = array (
'Authorization: key=' . "*****",
'Content-Type: application/json'
);
$ch = curl_init ();
curl_setopt ( $ch, CURLOPT_URL, $url );
curl_setopt ( $ch, CURLOPT_POST, true );
curl_setopt ( $ch, CURLOPT_HTTPHEADER, $headers );
curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt ( $ch, CURLOPT_POSTFIELDS, $fields );
$result = curl_exec ( $ch );
curl_close ( $ch );
}
Do you know why ?
Thanks :)

Google Firebase iOS push works in console but not in API

I'm using https://github.com/arnesson/cordova-plugin-firebase/ to receive Google Firebase messages on a ionic based app.
After set certificates, install plugin and setup Firebase account I was able to receive notifications (on both android and ios devices) sended through the Firebase Console.
But when I send through the Firebase API (https://firebase.google.com/docs/cloud-messaging/http-server-ref) only android devices receive the notification. I'm using the following code:
$data = Array
(
[to] => <token>
[notification] => Array
(
[title] => My Title
[text] => Notification test
[sound] => default
[vibrate] => 1
[badge] => 0
)
)
$jsonData = json_encode($data);
$ch = curl_init("https://fcm.googleapis.com/fcm/send");
$header = array(
'Content-Type: application/json',
"Authorization: key=".$gcmApiKey
);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt( $ch,CURLOPT_SSL_VERIFYPEER, true );
curl_setopt( $ch,CURLOPT_RETURNTRANSFER, true );
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonData);
$result = curl_exec($ch);
curl_close($ch);
echo $result;
No errors are returned:
{"multicast_id":904572753471539870406,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:1477063422322568734%3d5c78243d5c7824"}]}
What can be wrong?
For iOS, try adding in parameter priority set to high and content_available set to true in your payload.
See the parameter details here.
try this code
function sendGCM($message, $id) {
$url = 'https://fcm.googleapis.com/fcm/send';
$fields = array (
'registration_ids' => array (
$id
),
'data' => array (
"message" => $message
)
);
$fields = json_encode ( $fields );
$headers = array (
'Authorization: key=' . "YOUR_KEY_HERE",
'Content-Type: application/json'
);
$ch = curl_init ();
curl_setopt ( $ch, CURLOPT_URL, $url );
curl_setopt ( $ch, CURLOPT_POST, true );
curl_setopt ( $ch, CURLOPT_HTTPHEADER, $headers );
curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt ( $ch, CURLOPT_POSTFIELDS, $fields );
$result = curl_exec ( $ch );
echo $result;
curl_close ( $ch );
}
?>
also try it in curl terminal
curl -X POST --header "Authorization: key=<API_ACCESS_KEY>" --Header "Content-Type: application/json" https://fcm.googleapis.com/fcm/send -d "{\"to\":\"<YOUR_DEVICE_ID_TOKEN>\",\"notification\":{\"body\":\"Yellow\"},\"priority":10}"
a little late but with working example,
I'm using the code below for ios and android push, you are missing the priority and content_available fields
example :
$url = 'https://fcm.googleapis.com/fcm/send';
$fields = array(
'to' => $token,
'notification' => array('body' => $message , "sound" => "default"),
'data' => $message,
"sound"=> "default",
'priority' => "high" ,
'content_available' => false
);
$headers = array(
'Authorization:key = your-key',
'Content-Type: application/json'
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
$result = curl_exec($ch);

Categories