could not send the notification using topic messaging. Here i am attached the code. Please check it. it runs fine on the postman. But could not get the notification.
$topic = $_POST['topic'];
$headers = [
'Authorization: key=' . API_ACCESS_KEY,
'Content-Type: application/json'
];
if($_POST['send_to']=='topic'){
$fcmNotification = array(
'to' => '/topics/'.$topic,
'notification' => $dataToSentNotification,
);
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$fcmUrl);
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($fcmNotification));
$results = curl_exec($ch);
curl_close($ch);
Use the curl_errno function to get the specific error, code as follow:
$error_msg = NULL;
if (curl_errno($ch)) {
$error_msg = curl_error($ch);
}
curl_close($ch);
if ($error_msg !== NULL) {
print_r($error_msg);
}
As aynber says, add the whole curl sentences inside if or what do you expect if $fcmNotification is empty or null?
Related
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.
I started developing an app that has push notifications in it. I want my notification object to hold a message and other extra string that I call "color".
Everything worked well until I added the row 'data'=>$color. Now my message field is empty (null) and I can only read the color string.
How to add more data field to the PHP fields array?
function send_notification ($tokens, $message, $color)
{
$url = 'https://fcm.googleapis.com/fcm/send';
$fields = array(
'registration_ids' => $tokens,
'data' => $message,
'data' => $color
);
$headers = array(
'Authorization:key = Not telling you me real 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;
}
You can either change the field name, so it will be like 'color' => $color, or you can append it to the data field. It will be something like this:
$data = $message . "\n";
$data .= $color;
Edit: I believe your $message variabile might be a json, so you should append the $color to that json.
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 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);