Sending push notification using GCM in PHP - 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']);

Related

Invalid registration while sending push notification in php

I am following this Firebase Cloud Messaging (FCM) Server Side Implementation. . but got an error of invalidregistrationid.
Did googling but found nothing.
I also found some related problem which already answered on StackOverflow. but got no solutions.
the following link I visited for the solution.
a link.
B link.
class abc{
function send_android_notification($registration_ids="2fa86bbc8bc4aee204255a0d84461c000a26442312d0a9b1df1664d461fd3b7d") {
$message = array('title' => 'CodeCastra', 'body' => "hi huye" ,'sound'=>'Default','image'=>'Notification Image' );
define("GOOGLE_API_KEY", "AIzaSyDDvpPcWfOTVkkvA-yQg4yTYzIuh0MNIRw"); //legacy server key
$fields = array(
'registration_ids' => (array)$registration_ids,
'notification' => $message, //note: body & title fileds must be specified for the message or your only get just the vibration but the notification
);
$headers = array(
'Authorization: key=' . GOOGLE_API_KEY, // FIREBASE_API_KEY_FOR_ANDROID_NOTIFICATION
'Content-Type: application/json'
);
//Open connection
$ch = curl_init();
//Set the url, number of POST vars, POST data
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_VERIFYHOST, 0);
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));
//echo json_encode($fields);
//print_r($headers);
//exit;
//Execute post
$result = curl_exec($ch);
if ($result === false) {
die('Curl failed:' . curl_errno($ch));
}
// Close connection
curl_close($ch);
return $result;
}
}
print_r((new abc)->send_android_notification());
die;
{"multicast_id":4707836736661373968,"success":0,"failure":1,"canonical_ids":0,"results":[{"error":"InvalidRegistration"}]}
Any help will be appreciated.
thanks
I am implementing your code.
Use firebase registration token it generated in device (ios/android)
instead of the Device token .
'registration_ids' => (array)$token,
Hope it's helpful

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

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.

CURL in CodeIgniter Unauthorized with Google Cloud Messages

I have a simple problem in here.
Im using GCM to send notifications from my server to android devices.
To send them Im using CURL to the Google CLoud.
However when I try to send Im getting Unauthorized error.
If I run the code as a simple php script without the CI it`s OK.
It seems to me that the problem is in CI configuration.
public function send_notification($registatoin_ids = 0, $message) {
// Set POST variables
$url = 'https://android.googleapis.com/gcm/send';
$fields = array(
'registration_ids' => array($registatoin_ids),
'data' => array($message),
);
$headers = array(
'Authorization: key=' . $this->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);
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);
$result;
}

send other things alongside the data with GCM using 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");

Categories