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;
}
Related
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
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']);
i am trying to create a push notification application which will send messages from the server to the users device.
for now i get the registration ids but when i run the curl_exec function i get the following error:
Curl failed: couldn't connect to host
i don't know why is that and i made some checks and when i try to change the URL to "google.com" is pass.
from my browser if i try to reach the original URL its redirect me to :
"https://developers.google.com/cloud-messaging/"
this is the php code:
<?php
class GCM {
function __construct() {
}
/**
* Sending Push Notification
*/
public function send_notification($registatoin_ids, $message) {
// include config
include_once 'connection.php';
// 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_IPRESOLVE, CURL_IPRESOLVE_V4 );
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;
}
}
?>
i found that my server containing a firewall which blocking my outgoing connections. so i just open it and its working now.
this is the solution which helped me to solve the problem with link to the relevant answer.
CURL error code 7 (CURLE_COULDNT_CONNECT)
is very explicit ... it means Failed to connect() to host or proxy.
The following code would work on any system:
$ch = curl_init("http://google.com"); // initialize curl handle
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
$data = curl_exec($ch);
print($data);
If you can not see google page then .. your URL is wrong or you have some firewall or restriction issue
this link help me to resolve this issue
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);
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.