send other things alongside the data with GCM using php - php

I am sending android push notifications using php (curl).
Below is my class that sends the notification.
public function send_notification($registatoin_ids, $message)
{
include_once 'config.php';
$url = 'https://android.googleapis.com/gcm/send';
//issue here
$fields = array('registration_ids' => $registatoin_ids, 'data' => $message);
$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_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
$result = curl_exec($ch);
if ($result === FALSE)
{
die('Curl failed: ' . curl_error($ch));
}
Like this I am able to send the message to the desired recipient and I am succeeding.
However, I can only send messages like this.
Suppose I want to send additional information, such as a link of a site, or other data that won't be attached to the message.
Where would I add that ?
Are 'registration_ids' and 'data' unique values ?
androids documentation does not include php example.

The variable data can be an array. So your send the message like this.
$data = array("url" => "wwww.google.com", "message" => "Hello, Google");

Related

Sending push notification using GCM in PHP

I am using a following code to send push notification messages using GCM in PHP but I cannott get it working. I tried this:
/*------send_messages_gcm--------*/
private function send_messages_gcm(){
global $wpdb;
include_once './config.php';
$GOOGLE_API_KEY= "AIzaSyD-8NPnZ3WpdMIc-9TtKPCMRQtcliykc-s";
$registatoin_ids=$_REQUEST['rj_id'];
$message="hii anita";
// Set POST variables
$url = 'https://android.googleapis.com/gcm/send';
$fields = array(
'registration_ids' => $registatoin_ids,
'data' => $message,
);
// print_r($fields);
$headers = array(
'Authorization: key=' .$GOOGLE_API_KEY,
'Content-Type: application/json'
);
// Open connection
$ch = curl_init();
// Set the url, number of POST vars, POST data
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Disabling SSL Certificate support temporarly
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
// Execute post
$result = curl_exec($ch);
if ($result === FALSE) {
die('Curl failed: ' . curl_error($ch));
}
// Close connection
curl_close($ch);
echo $result;
}
When I run this function then it produces this result:
registration_ids" field is not a JSON array
I also tried curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode()); instead of curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields)), but it doesn't worked.
due to sending a message to multiple receivers:
registration_ids" field must be a JSON array
so
$registatoin_ids = array($_REQUEST['rj_id']);

show multiple lines of notification sent from GCM

I am using below PHP function to send notification to android device. I am getting notification in one line. How can I modify it to multiple line, if there is more content. There is no notification layout or anything in android app.
public function send_notification($registatoin_ids, $message) {
// Set POST variables
$url = 'https://android.googleapis.com/gcm/send';
$fields = array(
'registration_ids' => $registatoin_ids,
'data' => $message,
);
$headers = array(
'Authorization: key=' . GOOGLE_API_KEY,
'Content-Type: application/json'
);
// Open connection
$ch = curl_init();
// Set the url, number of POST vars, POST data
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Disabling SSL Certificate support temporarly
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
// Execute post
$result = curl_exec($ch);
if ($result === FALSE) {
die('Curl failed: ' . curl_error($ch));
}
// Close connection
curl_close($ch);
echo $result;
}
Set BigTextStyle to your builder.
NotificationCompat.Builder notification = new NotificationCompat.Builder(context)
....set other stuff ....
.setTicker(title)
.setContentText(message)
.setStyle(new NotificationCompat.BigTextStyle().bigText(message))
.setContentText(message);

Sending notifications to specific device using 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.

GCM plain text Push notification in PHP?

I am new In android and PHP. I am working on push Notification.
I have read many tutorials but all are for Json not for plain text.
For json code is here , but I want to send only plain text.
I have read this .
https://github.com/mattg888/GCM-PHP-Server-Push-Message/blob/master/GCMPushMessage.php
http://www.androidhive.info/2012/10/android-push-notifications-using-google-cloud-messaging-gcm-php-and-mysql/
I have also read this .
GCM Error=MissingRegistration sending messages via JSON
My code is here ,can some body help me in this
<?php
$gcm_regid = 'fkjfsdhfsuvgsdhfkfkjshdfuwfsdfh9wfjehf9ufwjfhu9erfjkhf9efiefhwodfh9'; // GCM Registration ID
$registatoin_ids = array($gcm_regid);
$message = array("message" => "Hello test");
// Set POST variables
$url = 'https://android.googleapis.com/gcm/send';
$fields = array(
'registration_ids' => $gcm_regid,
'data' => 'helloword',
);
$headers = array(
'Authorization: key=12dsdsvefdfdfgdfgdfgdfgfgfgfg',
'Content-Type: application/x-www-form-urlencoded'
);
// Open connection
$ch = curl_init();
// Set the url, number of POST vars, POST data
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Disabling SSL Certificate support temporarly
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
// Execute post
$result = curl_exec($ch);
if ($result === FALSE) {
die('Curl failed: ' . curl_error($ch));
}
// Close connection
curl_close($ch);
echo $result;
?>
If you try to do plain text, you should ignore the context-type in this block
$headers = array(
'Authorization: key=12dsdsvefdfdfgdfgdfgdfgfgfgfg',
'Content-Type: application/x-www-form-urlencoded'
);
Also, you should use for plain text the registration ID is passed in a parameter called registration_id instead of registration_ids array.

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