php push notification for android giving xml error - php

I am trying to send the push notification from php to android users. Using following code
<?php
// API access key from Google API's Console
define( 'API_ACCESS_KEY', 'access_key');
$registrationIds = array( "reg_id" );
// prep the bundle
$msg = array
(
'message' => 'here is a message. message',
'title' => 'This is a title. title',
'subtitle' => 'This is a subtitle. subtitle',
'tickerText' => 'Ticker text here...Ticker text here...Ticker text here',
'vibrate' => 1,
'sound' => 1,
'largeIcon' => 'large_icon',
'smallIcon' => 'small_icon'
);
$fields = array
(
'registration_ids' => $registrationIds,
'data' => $msg
);
$headers = array
(
'Authorization: key=' . API_ACCESS_KEY,
'Content-Type: application/json'
);
$ch = curl_init();
curl_setopt( $ch,CURLOPT_URL, 'https://android.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( $fields ) );
$result = curl_exec($ch );
curl_close( $ch );
echo $result;
?>
But getting following response
<HTML>
<HEAD>
<TITLE>Unauthorized</TITLE>
</HEAD>
<BODY BGCOLOR="#FFFFFF" TEXT="#000000">
<H1>Unauthorized</H1>
<H2>Error 401</H2>
</BODY>
</HTML>
I did some R&D, but everyone are getting error in json, but I am getting in xml format. I used postman for this. I also directly checked from browser by hitting the url for this script, but still returning the same error.
Unauthorized
Error 401
I checked on my local system and on server both, but same error.

First Check you have missed this line in your curl
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
and check your api key is valid or not.. Your function will work fine hopefully.

Related

I have implemented an FCM server side, Server side not delivering

I am trying to implement a php curl fcm topic broadcasting and it just displays {"message_id":4731721763997571462} and does not deliver.
I have gone through a lot of search and to no end, I can't seem to find the problem.
// API access key from Google API's Console
define( 'API_ACCESS_KEY', 'Legacy_server_key' );
// prep the bundle
$msg = array
(
'message' => 'here is a message. message',
'title' => 'This is a title. title',
);
$fields = array
(
'to' => "/topics/hello",
'notification' => $msg
);
$headers = array
(
'Authorization:key='. API_ACCESS_KEY,
'Content-Type: application/json'
);
$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_RETURNTRANSFER, true );
curl_setopt( $ch,CURLOPT_SSL_VERIFYPEER, false );
curl_setopt( $ch,CURLOPT_POSTFIELDS, json_encode( $fields ));
$result = curl_exec($ch );
curl_close( $ch );
echo $result;
I just expect to get a notification on the device and maybe there is a way I can trace successful notifications on the google console?
You need to add registration ids with field parameter
// API access key from Google API's Console
define( 'API_ACCESS_KEY', 'Legacy_server_key' );
$registrationIds = array( "/topics/obajemusagmailcom" );
// prep the bundle
$msg = array
(
'message' => 'here is a message. message',
'title' => 'This is a title. title',
);
$fields = array
(
//'to' => "/topics/hello",
'registration_ids' => $registrationIds,
'notification' => $msg
);
$headers = array
(
'Authorization:key='. API_ACCESS_KEY,
'Content-Type: application/json'
);
$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_RETURNTRANSFER, true );
curl_setopt( $ch,CURLOPT_SSL_VERIFYPEER, false );
curl_setopt( $ch,CURLOPT_POSTFIELDS, json_encode( $fields ));
$result = curl_exec($ch );
curl_close( $ch );
echo $result;
and $registrationIds = array( "/topics/obajemusagmailcom" ); is not registration id of any app, please correct it.
Finally after much horror and tries, turned out the array fields were incorrect and were meant to be
$msg = array
(
'body' => $body,
'title'=>$title
);
$data = array
(
'to' => "/topics/".$topic,
'notification' => $msg
);

api fcm push notifications using php Error : JSON_PARSING_ERROR: Unexpected token END OF FILE at position 0

Hi guys I push notifications fcm using php when i send title or body in Arabic Language The result error :
JSON_PARSING_ERROR: Unexpected token END OF FILE at position 0.
just in Arabic Language
this is my code:
<?php
define( 'API_ACCESS_KEY', 'my api server key');
$registrationIds = ('token')
$msg = array
(
'body' => 'هلاو',
'title' => 'مرحبا',
'icon' => 'myicon',/*Default Icon*/
'sound' => 'mySound'/*Default sound*/
);
$fields = array
(
'to' => $registrationIds,
'notification' => $msg
);
$headers = array
(
'Authorization: key=' . API_ACCESS_KEY,
'Content-Type: application/json',
);
#Send Reponse To FireBase Server
$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_RETURNTRANSFER, true );
//curl_setopt( $ch,CURLOPT_SSL_VERIFYPEER, false );
curl_setopt( $ch,CURLOPT_POSTFIELDS, json_encode( $fields ) );
$result = curl_exec($ch );
curl_close( $ch );
echo $result;
I need help may be the problem in encode
change your .php file encode to UTF-8.
from Notepad++ Encode -> Encode in UTF-8 then save file.

PHP firebase push-notification success but can't get in android phone

I have done below code for sending push-notification to the android phone.
<?php
// API access key from Google API's Console
define( 'API_ACCESS_KEY', '********' );
$registrationIds = array('reg_ids');
// google-site-verification="YOUR-VARIFICATION-KEY"
// prep the bundle
$msg = array
(
'message' => 'here is a message. message',
'title' => 'This is a title. title',
'subtitle' => 'This is a subtitle. subtitle',
'tickerText' => 'Ticker text here...Ticker text here...Ticker text here',
'vibrate' => 1,
'sound' => 1,
'largeIcon' => 'large_icon',
'smallIcon' => 'small_icon'
);
$fields = array
(
'registration_ids' => $registrationIds,
'data' => $msg
);
$headers = array
(
'Authorization: key=' . API_ACCESS_KEY,
'Content-Type: application/json'
);
$ch = curl_init();
curl_setopt( $ch,CURLOPT_URL, 'https://android.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( $fields ) );
$result = curl_exec($ch );
var_dump(curl_error($ch));
curl_close( $ch );
echo $result;
?>
Getting following output.
{"multicast_id":6906123147298299296,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:1500530376709090%0b4d5784f9fd7ecd"}]}
My problem is i get success=1 but can't get any push-notifications. I try many other GCM(Registration) id but never success.
So, how i solve/debug this issue ?
Thank you very much.

Internal Server Error using Google API on PHP

Running this code on Godaddy, Plesk account returns 'Internal Server Error'. Should I make any changes to code or any configuration to run this code?
Code ran perfectly in easyphp local server. Please help. Thank you.
sendPush($to,$title,$message);
function sendPush($to,$title,$message)
{
// API access key from Google API's Console
// replace API
define( 'API_ACCESS_KEY', 'AIzlkplhm-KrtLlR-p-Mg');
$registrationIds = array($to);
$msg = array
(
'message' => $message,
'title' => $title,
'vibrate' => 1,
'sound' => 1
// you can also add images, additionalData
);
$fields = array
(
'registration_ids' => $registrationIds,
'data' => $msg
);
$headers = array
(
'Authorization: key=' . API_ACCESS_KEY,
'Content-Type: application/json'
);
$ch = curl_init();
curl_setopt( $ch,CURLOPT_URL, 'https://android.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( $fields ) );
$result = curl_exec($ch );
curl_close( $ch );
echo $result;
}

Google Cloud Messaging returns Invalid Registration

When I am trying to send a gcm message through the below php, I get this error:
{"multicast_id":8958574426215974401,"success":0,"failure":1,"canonical_ids":0,"results":[{"error":"InvalidRegistration"}]}
However, the server API key is exactly what Google gave me to use, and the gcm_token is accurately being stored to my database. I do not know where the issue lies.
Here is my server side php:
<?php
require 'db_connect.php';
// API access key from Google API's Console
define( 'API_ACCESS_KEY', 'AIzaSyREST OF MY KEY' );
//insert the message into a database
$sql = "SELECT gcm_token FROM users WHERE user_id='1' LIMIT 1;";
// preform
if (!mysqli_query($Thesisdb, $sql)) {
printf("Errormessage: ", mysqli_error($Thesisdb));
mysqli_close($Thesisdb);
} else{
$results = mysqli_query($Thesisdb, $sql);
$registrationIds = array($results);
// prep the bundle
$msg = array
(
'message' => '$user',
'title' => 'This is a title. title',
'subtitle' => 'This is a subtitle. subtitle',
'tickerText' => 'Ticker text here...Ticker text here...Ticker text here',
'vibrate' => 1,
'sound' => 1,
'largeIcon' => 'large_icon',
'smallIcon' => 'small_icon'
);
$fields = array
(
'registration_ids' => $registrationIds,
'data' => $msg
);
$headers = array
(
'Authorization: key=' . API_ACCESS_KEY,
'Content-Type: application/json'
);
$ch = curl_init();
curl_setopt( $ch,CURLOPT_URL, 'https://android.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( $fields ) );
$result = curl_exec($ch );
curl_close( $ch );
echo $result;
}
?>
mysqli_query returns Boolen value (true or false) so this will not going to works directly.
we have to convert mysqli_query return values to array using fetch array, fetch assoc.
$results = mysqli_query($Thesisdb, $sql);
$registrationIds = array($results);
Code to combile all GCM Tokens.
registrationIds=array();
$sql="SELECT `user_id`,`push_notification_registration_id` FROM `tbl1` WHERE `user_status`='Active' AND `push_notification_registration_id`!=''";
$fire=mysqli_query($this->conn,$sql);
$users=array();
if( (isset($fire))&&(!empty($fire)) ){
while($result=mysqli_fetch_assoc($fire)){
array_push($users,$result);
}
}
foreach($users as $single_user){
$register_id=$single_user['push_notification_registration_id'];
array_push($registrationIds,$register_id);
}
$msg = array
(
'title' => $news_title,
'subtitle' => $news_description,
'tickerText' => 'Ticker Text',
'vibrate' => 1,
'sound' => 1,
'news_id' => $news_id,
'notification_type' => 'news'
);
$fields = array
(
'registration_ids' => $registrationIds,
'data' => $msg
);
$headers = array
(
'Authorization: key=' . YOUR_GOOGLE_API_KEY,
'Content-Type: application/json'
);
$ch = curl_init();
curl_setopt( $ch,CURLOPT_URL, 'https://android.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( $fields,true ) );
$result = curl_exec($ch );
curl_close( $ch );

Categories