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);
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']);
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 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;
}
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");