I found a great code that sends push messages through the GCM.
The question is if it is possible to change the String "Google Cloud Messaging working well"
Address bar example I would write www.example.com/index.php?text=work
And the string will work.
Thanks
<?php
define("GOOGLE_API_KEY", "AIzaSyCJiVkatisdQ44rEM353PFGbia29mBVscA");
define("GOOGLE_GCM_URL", "https://android.googleapis.com/gcm/send");
function send_gcm_notify($reg_id, $message) {
$fields = array(
'registration_ids' => array( $reg_id ),
'data' => array( "message" => $message ),
);
$headers = array(
'Authorization: key=' . GOOGLE_API_KEY,
'Content-Type: application/json'
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, GOOGLE_GCM_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('Problem occurred: ' . curl_error($ch));
}
curl_close($ch);
echo $result;
}
$reg_id = "APA91bHuSGES.....nn5pWrrSz0dV63pg";
$msg = "Google Cloud Messaging working well";
send_gcm_notify($reg_id, $msg);
Replace
"Google Cloud Messaging working well";
with
$_GET['text'];
to have a nicer code you would use
filter_input (INPUT_GET, 'text', FILTER_SANITIZE_STRING);
Related
could not send the notification using topic messaging. Here i am attached the code. Please check it. it runs fine on the postman. But could not get the notification.
$topic = $_POST['topic'];
$headers = [
'Authorization: key=' . API_ACCESS_KEY,
'Content-Type: application/json'
];
if($_POST['send_to']=='topic'){
$fcmNotification = array(
'to' => '/topics/'.$topic,
'notification' => $dataToSentNotification,
);
}
$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));
$results = curl_exec($ch);
curl_close($ch);
Use the curl_errno function to get the specific error, code as follow:
$error_msg = NULL;
if (curl_errno($ch)) {
$error_msg = curl_error($ch);
}
curl_close($ch);
if ($error_msg !== NULL) {
print_r($error_msg);
}
As aynber says, add the whole curl sentences inside if or what do you expect if $fcmNotification is empty or null?
I am using php to send push notification using firebase to my Android app build using unity.
App is receiving notification and showing it in a tray, but not showing it as pop up notification like whats app or other apps do.
My php code is:
function send_notification ($tokens, $message){
$url = 'https://fcm.googleapis.com/fcm/send';
$notification= array('title' => 'Winner!!','body' => $message);
$data= array('custom_notification' => $custom_notification );
$fields = array(
'registration_ids' => $tokens,
'notification' => $notification,
);
$headers = array(
'Authorization:key = AAAA5C-Y_Ew:APA91bFD1g98n19DZD8FkaEYiME4tDsniePTKU1sGww1EAD7dtSKP6FEqlRGcTpB_MCD_7UM7ToWis1VnZO-dtDXMLFSZtaSIDAQ0fACbJ_SOFjWd93klckboahy3eZr93Z9M02LfP_s',
'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);
if ($result === FALSE) {
die('Curl failed: ' . curl_error($ch));
}
curl_close($ch);
return $result;
}
it also showing gray scaled icon instead of colored icon.
I want to show notification as pop up.
is there anyway to do so using FCM?
Instead of 'notification' => $notification use only data key while sending notification for android only.
iOS require only 'notification' => $notification however.
I am sending a message from my server to firebase for a specific topic, I get back an id, but it does not get a notification to the app when I do it from the console to that topic the notification arrives.
How should I do it for a specific topic but that is a notification?
PHP:
<i>
<?php
function send_notification ($message)
{
$url = 'https://fcm.googleapis.com/fcm/send';
$fields = array(
'to' => "/topics/news",
'data' => $message
);
$headers = array(
'Authorization: key=AAAApDF81wE:APA91bG7g.....',
'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);
if ($result === FALSE) {
die('Curl failed: ' . curl_error($ch));
}
curl_close($ch);
return $result;
}
$message = array("message" => " FCM PUSH NOTIFICATION TEST MESSAGE");
$message_status = send_notification($message);
echo $message_status;
?>
</i>
I just changed this
$fields = array(
'to' => "/topics/news",
'notification' => $message
);
i Was not doing proper handling in the app, i should handle the logic and code implementation on how to handle incoming messages within the onMessageReceived method.
when i trying to send multiple push notification that a time i m getting error like .
my PHP coding is :-
<?php
//Sending Push Notification
function send_push_notification($device_id, $msg)
{
//echo "fdff";
// Set POST variables
$url = 'https://android.googleapis.com/gcm/send';
//$message1 = array("header" => $msg);
$x = array($device_id);
$fields = array(
'registration_ids' => $x,
'data' => array("message" => $msg),
);
//print_r($fields);
$headers = array(
'Authorization: key=' . "AIzaSyAYmZljXmJWgIqD5sQ9_0d- jUVorm3ZnhA",
'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_POSTFIELDS, json_encode($fields));
$result = curl_exec($ch);
if ($result === FALSE) {
die('Curl failed: ' . curl_error($ch));
}
curl_close($ch);
return $result;
}
?>
and this is my error.
{"multicast_id":5911003352460212858,"success":0,"failure":1,"canonical_ids":0,"results":[{"error":"MismatchSenderId"}]}
{"multicast_id":5737426372728217758,"success":1,"failure":0,"canonical_ids":1,"results":[{"registration_id":"APA91bHa4mmnj2EZfiN7EVa5c_eCVKBkQJmImpfedThbwoxyzlxh3f_NGRJ8mVCp63JQnL8ZvKvtH6P_Nz5O_MKu1Kgy7d2oJhLt6-82xK499n1P_mTp6t4m11ERlaU6Oe6vEmqG6CBu","message_id":"0:1449254314294720%66e3e4c4f9fd7ecd"}]}
in this code i have success in two devices but in one device is not send.
so how this possible?
I'm trying send notifications to specific device using PHP. I created the project in google console and start GCM service. I can send notifications to all devices but I want send to specific device for example: 26093d6bbddgggg, 26093d6bbddzzzz
How can i do it ?
I'm trying this PHP script
<?php
define("GOOGLE_API_KEY", "AIzaSyCJiVkatisdQ44rEM353PFGbia29mBVscA");
define("GOOGLE_GCM_URL", "https://android.googleapis.com/gcm/send");
function send_gcm_notify($reg_id, $message) {
$fields = array(
'registration_ids' => array( $reg_id ),
'data' => array( "message" => $message ),
);
$headers = array(
'Authorization: key=' . GOOGLE_API_KEY,
'Content-Type: application/json'
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, GOOGLE_GCM_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('Problem occurred: ' . curl_error($ch));
}
curl_close($ch);
echo $result;
}
$reg_id = "APA91bHuSGES.....nn5pWrrSz0dV63pg";
$msg = "Google Cloud Messaging working well";
send_gcm_notify($reg_id, $msg);
I dont know about PHP. but when you are getting device Ids at that search device Id for specific device and then send.