php firebase push notification ios : ERROR InvalidRegistration - php

$url = "https://fcm.googleapis.com/fcm/send";
$token = "xxx";
$serverKey = 'xxx';
$title = "Title";
$body = "Body of the message";
$notification = array('title' =>$title , 'text' => $body, 'body' => $body, 'sound' => 'default', 'badge' => '1');
$arrayToSend = array('to' => $token, 'notification' => $notification,'priority'=>'high', 'data' => $notification, 'content_available' => true);
$json = json_encode($arrayToSend);
$headers = array();
$headers[] = 'Content-Type: application/json';
$headers[] = 'Authorization: key='. $serverKey;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER,$headers);
$response = curl_exec($ch);
pre($response);
curl_close($ch);
I CAN'T SEND NOTIFICATION
{"multicast_id":2200169071930988341,"success":0,"failure":1,"canonical_ids":0,"results":[{"error":"InvalidRegistration"}]}

This means that the $token value you pass in is not registered on the current Firebase project. There are many reasons why this could happen, including that the token as expired and is no longer valid.
The usual way to handle this response is to remove the outdated token from your database, so that you don't pass it in calls to the FCM API in the future.

We provided API controls with firebase. There is no error in the token. Notification is sent via firebase with the same token.

Related

Receive Push Notification Twice firebase PHP Script

I'm new to Firebase Send Push Notification. I'm working on Send Push notification To multiple Users Using PHP Script.
PHP SCRIPT
function sendPushNotification($fb_key_array, $title) {
$finalPostArray = array('registration_ids' => $fb_key_array,
'notification' => array(
'title' => $title,
'sound' => 'default',
'badge' => '1'
),
'priority' => 'high',
);
$server_key = env("SERVER_KEY");
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://fcm.googleapis.com/fcm/send");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($finalPostArray)); //Post Fields
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Authorization: key=' . $server_key));
$server_output = curl_exec($ch);
print_r($server_output);
return $server_output;
curl_close($ch);
}
I'm send notification using Queue Jobs on Particular Time.
I have a problem when I send Three notification at same time than Notification receive Twice.
Response From This Curl
App\Jobs\PushNotification
<pre>{"multicast_id":4668247729883149905,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:1616386021446151%e609af1cf9fd7ecd"}]}[2021-03-22 04:07:01][261] Processed: App\Jobs\PushNotification
[2021-03-22 04:07:01][262] Processing: App\Jobs\PushNotification
<pre>{"multicast_id":4539206321688711164,"success":2,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:1616386022124068%e609af1cf9fd7ecd"},{"message_id":"0:1616386022123922%e609af1cf9fd7ecd"}]}[2021-03-22 04:07:01][262] Processed: App\Jobs\PushNotification
[2021-03-22 04:07:02][263] Processing: App\Jobs\PushNotification
<pre>{"multicast_id":1591082090876608595,"success":3,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:1616386023195991%e609af1cf9fd7ecd"},{"message_id":"0:1616386023196003%e609af1cf9fd7ecd"},{"message_id":"0:1616386023195724%e609af1cf9fd7ecd"}]}[2021-03-22 04:07:02][263] Processed: App\Jobs\PushNotification
And Receive 6 Notification .but I send Only Three Notification. Where is my Fault ?
Help Thank You
I am not so sure about that there is nothing wrong with your func but you can try this it's the same but little different if not work so that mean you have a loop somewhere in your code ..
$url = 'https://fcm.googleapis.com/fcm/send';
$fields = array (
'registration_ids' => $target,
// fix new json format
'notification' => array(
'body' => 'this is firebase test mesging',
'title' => 'hello from firebase',
'badge' => 1,
'sound' => 'default',
'priority' => 'high',
)
);
$fields = json_encode($fields);
$server_key = 'ur_server_kay';
// inherited key
$headers = array(
'Content-Type:application/json',
'Authorization:key='.$server_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);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
$result = curl_exec($ch);
var_dump($result);
if ($result === FALSE) {
die('Oops! FCM Send Error: ' . curl_error($ch));
}
curl_close($ch);

FCM Push for iOS error InvalidRegistration

I am trying to send push notifications through PHP script via Firebase Cloud Messaging to iOS App.
Currently I'm using following test PHP script.
$url = "https://fcm.googleapis.com/fcm/send";
$token = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
$serverKey = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
$title = "Title";
$body = "Body of the message";
$notification = array('title' =>$title , 'text' => $body, 'sound' => 'default', 'badge' => '1');
$arrayToSend = array('to' => $token, 'notification' => $notification,'priority'=>'high');
$json = json_encode($arrayToSend);
$headers = array();
$headers[] = 'Content-Type: application/json';
$headers[] = 'Authorization: key='. $serverKey;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST,"POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
curl_setopt($ch, CURLOPT_HTTPHEADER,$headers);
//Send the request
$response = curl_exec($ch);
//Close request
if ($response === FALSE) {
die('FCM Send Error: ' . curl_error($ch));
}
curl_close($ch);
However, I am receiving following error message when I try to run script via browser.
{"multicast_id":5417898622260949101,"success":0,"failure":1,"canonical_ids":0,"results":[{"error":"InvalidRegistration"}]}
But when I use the same device token from FCM console, it is working without any issues.
I have tried many PHP scripts on the internet, but end up with the same results, what would be the issue here ?
Any help would be highly appreciate.
The token you are using does not seem to be a valid registration token for FCM, hence the InvalidRegistration error.
$apiKey = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';
$title = 'Title';
$message = 'hello message';
$notification =[
"text" => "Test",
'badge' => "1",
'sound' => 'shutter.mp3',
"android_channel_id"=>"test01",
'body'=>$message,
'icon'=>'notif_icn',
'title'=>$title,
'priority'=>'high',
'vibrate'=> 1,
'alert'=> $message
];
$msg = [
'message'=> $message,
'title'=> $title
];
$fields =[
"content_available" => true,
"priority" => "high",
'registration_ids'=> $deviceToken,
'data'=> $msg
];
$headers = array('Authorization: key=' . $apiKey,'Content-Type: application/json');
if ($headers){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://fcm.googleapis.com/fcm/send");
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
$response = curl_exec($ch);
if ($response === FALSE) {
die('FCM Send Error: ' . curl_error($ch));
}
curl_close($ch);
}

PHP - How to get OAuth 2.0 Token

I want to connect to OAuth 2.0 authorization type (with password grant_type) to get token with POST method, it's ok to test from Postman but i couldn't connect through PHP...
here's my information:
request url: 'http://example.com/core/connect/token'
ClientName: 'something1'
ClientId: 'something2'
Secret: 'something3'
UserName: 'something4'
Password: 'something5'
Scope: 'something6'
could you please just give me an example to get token and use?
i've already tested:
$base_url = 'https://example.com/oauth/token';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $base_url);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, array(
'client_id' => YOUR-CLIENT-ID,
'client_secret' => YOUR-CLIENT-SECRET,
'username' => YOUR-USERNAME-OR-EMAIL,
'password' => YOUR-PASSWORD,
'grant_type' => 'password'
));
$data = curl_exec($ch);
$auth_string = json_decode($data, true);
and this
$api = "KEY GOES HERE";
$authurl = "http://example.com/core/connect/token";
$client_id = "ID GOES HERE";
$client_secret = "SECRET GOES HERE";
// Creating base 64 encoded authkey
$Auth_Key = $client_id.":".$client_secret;
$encoded_Auth_Key=base64_encode($Auth_Key);
$headers = array();
$headers['Authorization'] = "Basic ".$encoded_Auth_Key;
$headers['Content-Type'] = "application/x-www-form-urlencoded";
$data = array(
'grant_type' => 'password',
'scope' => 'read write',
'username' => $api,
'password' => $api,
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $authurl);
curl_setopt($ch, CURLOPT_POST, 1 );
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
$auth = curl_exec( $ch );
if ( curl_errno( $ch ) ){
echo 'Error: ' . curl_error( $ch );
}
curl_close($ch);
$secret = json_decode($auth);
$access_key = $secret->access_token;
echo $secret;
The first call seems OK for the password grant type. The scope parameter should be optional. So...
Add this to your code to know the response from the server and therefore know the missing parameter or setting.
curl_setopt($ch, CURLOPT_VERBOSE, true);
Check the status code and a possible body with an error message.

Sending Firebase notification with PHP

everyone! I am having a problem using PHP to send FIrebase notification. When I send it from Firebase console, I get the notification, but when I send it from PHP, I don't receive any notification.
DO you have any idea what is the problem?
Here is my PHP code:
<?php
$message = 'ojlaasdasdasd';
$title = 'ojla';
$path_to_fcm = 'https://fcm.googleapis.com/fcm/send'; $server_key='xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx5LnDZO2BpC2aoPMshfKwRbJAJfZL8C33qRxxxxxxxxxxxxL6';
$key = 'eqnlxIQ1SWA:APA91bGf1COAZamVzT4onl66_lEdE1nWfY7rIADcnt9gtNYnw7iWTwa7AYPYTHESFholkZ89ydCQS3QeL-lCIuiWTXiqoDREO0xhNdEYboPvqg8QsBYkrQVRlrCLewC4N-hHUja1NG4f';
$headers = array(
'Authorization:key=' .$server_key,
'Content-Type: application/json');
$fields = array
(
'to' => $key,
'notification' => array('title' => $title,'body' => $message)
);
$payload = json_encode($fields);
$ch = curl_init();
curl_setopt( $ch,CURLOPT_URL, '$path_to_fcm' );
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);
$result = curl_exec($ch);
echo $result;
curl_close($ch);
?>
I don't get any echo from the server
try this code.Its working fine for me.just change key and token
<?php
define('API_ACCESS_KEY','Api key from Fcm add here');
$fcmUrl = 'https://fcm.googleapis.com/fcm/send';
$token='235zgagasd634sdgds46436';
$notification = [
'title' =>'title',
'body' => 'body of message.',
'icon' =>'myIcon',
'sound' => 'mySound'
];
$extraNotificationData = ["message" => $notification,"moredata" =>'dd'];
$fcmNotification = [
//'registration_ids' => $tokenList, //multple token array
'to' => $token, //single token
'notification' => $notification,
'data' => $extraNotificationData
];
$headers = [
'Authorization: key=' . API_ACCESS_KEY,
'Content-Type: application/json'
];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$fcmUrl);
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($fcmNotification));
$result = curl_exec($ch);
curl_close($ch);
echo $result;
I use the following function to send a HTTP Request to Firebase:
function request($url, $body, $method = "POST", $header = "Content-type: application/x-www-form-urlencoded\r\n")
{
switch ($method) {
case 'POST':
case 'GET':
case 'PUT':
case 'DELETE':
break;
default:
$method = 'POST';
break;
}
$options = array(
'http' => array(
'header' => "$header",
'method' => "$method",
'content' => $body
)
);
$context = stream_context_create($options);
$data = file_get_contents($url, false, $context);
$data = json_decode($data, true);
return $data;
}
To execute the function I use the following code:
$fcm_key = "";
$body = array("data" => $data, "to" => "$fcm_token");
$body = json_encode($body);
$json = request("https://fcm.googleapis.com/fcm/send", $body, "POST", "Authorization: key=$fcm_key\r\nContent-Type:application/json");
For me, this code works without any problems. Be sure that you use the right FCM device token ($fcm_token) and set the right FCM key ($fcm_key).
$tokenID = $valueTwo["TokenID"];
$cmMessage = array();
$cmMessage["type"] = 3;
$cmMessage["receiverMail"] = $receiverMail;
$cmMessage["ownerMail"] = $ownerMail;
$cmMessage["jsonData"] = $jsonData;
$url = "https://fcm.googleapis.com/fcm/send";
$title = "";
$body = "";
//$notification = array('title' => $title, 'body' => null, 'sound' => 'default', 'badge' => '1');
$arrayToSend = array('to' => $tokenID, 'priority' => 'high', 'data' => $cmMessage);
$json = json_encode($arrayToSend);
$headers = array();
$headers[] = 'Content-Type: application/json';
$headers[] = 'Authorization: key=' . $serverKey;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
//Send the request
$response = curl_exec($ch);
//Close request
if ($response === FALSE) {
die('FCM Send Error: ' . curl_error($ch));
}
curl_close($ch);
Get your server key and token id to send write them... Dont use notification tag...Like I did in code... Cuz if you write notification tag, Application in background is not got receive msg Cuz notification tag executes on foreground and in background automatic notification is displayed..
Send firebase push notification via php to android, ios
function sendPushNotification($to = '', $data = array()){
$api_key = '';
$fields = array('to' => $to, 'notification' => $data);
$headers = array(
'Content-Type:application/json',
'Authorization:key='.$api_key
);
$url = 'https://fcm.googleapis.com/fcm/send';
$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_POSTFIELDS, json_encode($fields));
$result = curl_exec($ch);
if ($result === FALSE) {
die('FCM Send Error: ' . curl_error($ch));
}
curl_close($ch);
return $result;
}
$to = ''; //Device token
// You can get it from FirebaseInstanceId.getInstance().getToken();
$message = array(
'title' => 'Hai',
'body' => 'New Message');
echo sendPushNotification($to, $message);

Invalid Credentials error when trying to add post to blogger using API

I have trying to add a post to blogger using API since yesterday but no luck. Here is my code. When I run it, I get "Invalid Credentials" error.
I have also posted screenshot below which shows from where I am getting that key in Google Developer Console. May be I am picking up wrong key?
Code
<?php
$key = "J7c";
$blog_id = "123456";
$url = 'https://www.googleapis.com/blogger/v3/blogs/'.$blog_id.'/posts/';
$postData = array(
'kind' => 'blogger#post',
'blog' => array('id' => $blog_id),
'title' => 'This is title',
'content' => 'This is content'
);
$data_string = json_encode($postData);
$head = array();
$head[] = 'Authorization: '.$key;
$head[] = 'Content-Type: application/json';
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, $head);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
$rsp = curl_exec($ch);
$results = json_decode($rsp);
var_dump($rsp);
?>
Error
Google Developer Console

Categories