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?
Related
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>
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'
);
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 :)
function exe($content, $start, $end) {
if ($content && $start && $end) {
$r = explode ( $start, $content );
if (isset ( $r [1] )) {
$r = explode ( $end, $r [1] );
return $r [0];
}
return '';
}
}
function executeCurl($url, $postInfo, $type) {
$cookie_file_path = "./cookie.txt";
$ch = curl_init ();
curl_setopt ( $ch, CURLOPT_HEADER, false );
curl_setopt ( $ch, CURLOPT_NOBODY, false );
curl_setopt ( $ch, CURLOPT_URL, $url );
curl_setopt ( $ch, CURLOPT_SSL_VERIFYHOST, 0 );
curl_setopt ( $ch, CURLOPT_COOKIEJAR, $cookie_file_path );
curl_setopt ( $ch, CURLOPT_COOKIEFILE, $cookie_file_path );
curl_setopt ( $ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36" );
curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt ( $ch, CURLOPT_REFERER, 'https://www.prawo-jazdy-360.pl/logowanie' );
curl_setopt ( $ch, CURLOPT_SSL_VERIFYPEER, 0 );
curl_setopt ( $ch, CURLOPT_FOLLOWLOCATION, 1 );
curl_setopt ( $ch, CURLOPT_MAXREDIRS, 10 );
if ($type == "post") {
$headers = array (
"Content-Type:application/x-www-form-urlencoded",
"content-length: " . strlen ( $postInfo ) . ""
); // instead of 0, how could I get the length of the body from curl?
curl_setopt ( $ch, CURLOPT_HTTPHEADER, $headers );
curl_setopt ( $ch, CURLOPT_CUSTOMREQUEST, "POST" );
curl_setopt ( $ch, CURLOPT_POST, 1 );
curl_setopt ( $ch, CURLOPT_POSTFIELDS, $postInfo );
}
curl_setopt ( $ch, CURLOPT_NOSIGNAL, 1 );
curl_setopt ( $ch, CURLOPT_TIMEOUT_MS, 40000 );
$page = curl_exec ( $ch );
$curl_errno = curl_errno ( $ch );
/* Check for 404 (file not found). */
$info = curl_getinfo ( $ch );
if ($httpCode == 404 || $curl_errno > 0) {
return 0;
}
return $page;
return 1;
}
$username = "test1#o2.pl";
$password = "haslo123";
$page = executeCurl ( 'https://www.prawo-jazdy-360.pl/logowanie', '', 'get' );
$page = exe ( $page, 'login-left', 'przypomnienie-hasla' );
$token = exe ( $page, '__RequestVerificationToken', '>' );
$token = exe ( $token, 'value="', '"' );
$url = "https://www.prawo-jazdy-360.pl/logowanie";
$postInfo = '{"__RequestVerificationToken": ' . $token . ', "Email": ' . $username . ', "Password": ' . $password . ', "RememberMe": true }';
$page = executeCurl ( $url, $postInfo, "post" );
$url = "https://www.prawo-jazdy-360.pl/o-nas";
$postInfo = "";
$page = executeCurl ( $url, $postInfo, "get" );
I have problem to log in to this site.
don't set the Content-Type:application/x-www-form-urlencoded header manually, let curl do it for you. (see curl_setopt docs about CURLOPT_POSTFIELDS)
don't set the content-length header manually, let curl do it for you.
don't do curl_setopt ( $ch, CURLOPT_CUSTOMREQUEST, "POST" ); , just set CURLOPT_POST.
you set Content-Type:application/x-www-form-urlencoded , but your $postInfo is not even close to urlencoded format. in fact, it looks more like JSON encoding, but even if it was JSON, $username / $password is not properly json encoded (its doing like {username:foo} instead of {username:"foo"} ), so idk what encoding that is, but it is definitely not the encoding curl tell the server that it is (its NOT x-www-form-urlencoded format). PS, the easiest way to create a proper x-www-form-urlencoded string is by using the http_build_query function.
and there's a postfield called RememberMe that the login page sends, but your code doesn't. (it seems to be RememberMe=false )
fix those, and try again. and don't send it json encoded, send it as application/x-www-form-urlencoded , that's what the login page uses.
using hhb_curl from https://github.com/divinity76/hhb_.inc.php/blob/master/hhb_.inc.php , here's a working example code:
<?php
declare(strict_types = 1);
require_once ('hhb_.inc.php');
$hc = new hhb_curl ();
$hc->_setComfortableOptions ();
$hc->exec ( 'https://www.prawo-jazdy-360.pl/logowanie' );
$html = $hc->getResponseBody ();
$domd = #DOMDocument::loadHTML ( $html );
$inputs = array ();
// get __RequestVerificationToken and RememberMe and whatever else
foreach ( $domd->getElementById ( "login-form" )->getElementsByTagName ( "input" ) as $input ) {
$inputs [$input->getAttribute ( "name" )] = $input->getAttribute ( "value" );
}
// var_dump($inputs);
assert ( array_key_exists ( '__RequestVerificationToken', $inputs ) );
$inputs ['Email'] = 'test1#o2.pl';
$inputs ['Password'] = 'haslo123';
$hc->setopt_array ( array (
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => http_build_query ( $inputs ),
CURLOPT_URL => 'https://www.prawo-jazdy-360.pl/logowanie'
) );
$hc->exec ();
hhb_var_dump ( $hc->getResponseHeaders (), $hc->getResponseBody () );
it dumps the logged-in frontpage html on the last line, proving that it logged in correctly.
I tried this for sending FCM notification:
function sendFCM($title, $mess, $tk) {
//var_dump($title);
//var_dump($tk);
//exit;
$senderId = 'AAAAqYuVeLs:APA91bEXhJpHUJoHeoPvujhCKopbj5FO3xlLjppgUvbVEBwvXgXz1e6C9ZH93X0iEGfr21Gcu0qf2Mf0g4nHM7RT1kL77pG19wih6YgU6IFmZsWkpJQ2ws_aOKFS70tyXC74KYtV5KdJehM6dr77O0EVfn1SghbuFQ';
$url = 'https://fcm.googleapis.com/fcm/send';
$fields = array (
'to' => 'kr0xgwFrcHTTt7X6fsiz0zZ7Y2C2',
'data' => array (
"title" => $title,
"body" => $mess
)
);
$payload = json_encode ( $fields );
$headers = array (
'Authorization:key='.$senderId,
'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_VERIFYPEER, false );
curl_setopt ( $ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4 );
curl_setopt ( $ch, CURLOPT_POSTFIELDS, $payload );
enter code here
$result = curl_exec ( $ch );
echo json_encode($result);
// if ($result === FALSE) {
// die('FCM Send Error: ' . curl_error($ch));
// $json = array("status" => 0, "msg" => "Not Send!");
// echo json_encode($json);
// }else{
// $json = array("status" => 1, "msg" => "Send!");
// echo json_encode($json);
// }
curl_close ( $ch );
}
But I'm getting this response:
{"multicast_id":7532299715078966238,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:1481002800387502%00000000f9fd7ecd"}]}[]
The actual message is not sent. What do I do? It's not even visible in the Firebase Console sending message history.