Sending notifications to specific device using PHP? - php

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.

Related

PHP Firebase notification shows in tray but not popping up

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.

how to send a notification for a theme from my server to firebase

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.

how send multiple push notification send in android

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?

Send push message from url to device GCM

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);

Sending different push notification to each device

I have set up php script which will send push notification. Everything works fine and it sends same message to each device in array. But what bugs me is, what if I want to send different message do each device(depending on device preferences for instance)?
What would be the right approach here? My first thought is to call pushNotification function for every device separately instead of calling it once and sending array of devices to GCM?
This is the code I'm using, so please advise...
function sendPushNotificationToGCM($registatoin_ids, $message) {
//Google cloud messaging GCM-API url
$url = 'https://android.googleapis.com/gcm/send';
$fields = array(
'registration_ids' => $registatoin_ids,
'data' => $message,
);
// Google Cloud Messaging GCM API Key
define("GOOGLE_API_KEY", "my_api_key");
$headers = array(
'Authorization: key=' . GOOGLE_API_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);
if ($result === FALSE) {
die('Curl failed: ' . curl_error($ch));
}
curl_close($ch);
return($result);
}
$devices = array();
while($results = $result->fetch_array()) {
$devices[] = $results['dev_reg_id'];
}
//return $devices;
$pushMessage = $message;
$message = array("m" => $pushMessage);
$pushStatus = sendPushNotificationToGCM($devices, $message);
You can't send different messages to different devices in the same HTTP request to GCM server. If each device requires a unique message, it would also require a unique call to your sendPushNotificationToGCM function. If some devices share the same message, you can send that message to them in the same function call.

Categories