I want to send chrome push notifications to the registered user.I followed this link https://developers.google.com/web/fundamentals/getting-started/push-notifications/?hl=en to develop a basic push notification and it works fine.
For now, I am sending notification to users by running curl script with subscription id in the terminal. Using this I can send notification to a single user. I want to send to multiple users. How can I achieve this?
Try this :-
<?php
$data = ["registration_ids" => ["regid1","regid2",...,"regidn"]];
$dataString = json_encode($data);
$curl = curl_init('https://android.googleapis.com/gcm/send');
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($curl, CURLOPT_POSTFIELDS, $dataString);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, [
'Content-Type: application/json',
'Authorization: key=YOUR_API_KEY'
]);
$result = curl_exec($curl);
print_r($result);
?>
Related
I am using firebase notification for communicate with android applications with Laravel / PHP server,
My code for PHP :
$url = "https://fcm.googleapis.com/fcm/send";
$fields = array(
'registration_ids' => $fcm_id, /* array for fcm ids */
'data' => $data
);
$headers = array(
'Authorization: key=/*My 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_POSTFIELDS, json_encode($fields));
curl_exec($ch);
curl_close($ch);
This code is working fine but takes too much time when there is large numbers of registration ids, to overcome this problem i have to use background process for curl, so that i use timeout_ms in curl
like :
curl_setopt($ch, CURLOPT_TIMEOUT_MS, 10);
but when i use this line, notification not sent to android.
my question is why execution of fcm stops when i using this line, or is there any other way to do background process for FCM Notification?
I am developing the web server using other service by CodeIgniter.
So I am going to send the data to the service using api calling.
But I have a issue to make the request.
This is the data what I want to send.
$datastr = 'proc_name=customer_upd¶ms={
"proc_info":
{
"proc_division":"U"
},
"data":[{
"table_name":"Customer",
"rows":[ {
"itemId" : "12231551",
"lastName" : "ads",
"firstName" : "fds"
} ]
}]
}'
I made my code for this.
$curl = curl_init('https://webapi.ooo.com/access/');
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded;charset=UTF-8'));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $datastr);
curl_exec($curl);
But this is not work.
I am tested by postman, but it works well.
I think there is a issue to make the post request.
Please help me.
Thank you.
I think you are sending query string, instead, send only data in the key-value pair to the web service you are consuming. Check in which form web service is accepting the data. First, check web service from postman
Here is an example,
$data = array("key_1" =>value_1 , "key_2" =>"value_2" , "key_3" =>"value_3" , "key_4" =>"value_4" , "key_5" =>"value_5" );
function api($url,$api,$data)
{
$ch = curl_init();
$webservicelink = $url.$api;
curl_setopt($ch, CURLOPT_URL, $webservicelink);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json'));
$result = curl_exec($ch);
return json_decode($result, true);
}
I'm using push notification from onesignal platform in my cordova app. I have implemented it to send push notification from oneSignal dashboard but I want to send notification from my own search, I have stored device ids when user install the app but don't know how to send notifications to devices that's ids are stored in my database
Any help regarding this will be appreciated . Thanks if anyone has issue to understand my question can comment i will explain with my best capacity.
To handle this case, I also use one signal, but at the launch of my app, I send my id to my backend to be sure it will be registered to my db when I will need it. Just with a classic http post request.
There is no magic trick to achieve that, you don't have lot of choices here except save it to your db by a way or another.
You can use this function which is provided by OneSignal
function sendMessage(){
$content = array(
"en" => 'YOUR_CUSTOM_MESSAGE'
);
$fields = array(
'app_id' => "YOUR_APP_ID",
'include_player_ids' => array("YOUR_TARGET_ID"),
'contents' => $content
);
$fields = json_encode($fields);
print("\nJSON sent:\n");
print($fields);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://onesignal.com/api/v1/notifications");
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json; charset=utf-8',
'Authorization: Basic NGEwMGZmMjItY2NkNy0xMWUzLTk5ZDUtMDAwYzI5NDBlNjJj'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
$response = sendMessage();
$return["allresponses"] = $response;
$return = json_encode( $return);
print("\n\nJSON received:\n");
print($return);
print("\n");
I am developing php application for submitting my Employers data into bullhorn using rest api.
I have bullhorn account and client id and client secret
I read documentation and also see question in
[Insert candidate with categories + BullHorn
one of stack overflow but not find any solution
Can any one help me how to use api for php??
Thanks in advance
Below Code I used to Create an company in BullHorn CRM using RESTAPI
Here Use Company as ClientCorporation object.
$url = 'https://rest2.bullhornstaffing.com/rest-services/rggmp/entity/ClientCorporation?BhRestToken=09c4822e-04e7-40d8-9570-addc5c447aa7';
$params =array("name"=> "Maha","fax"=>231312313 );
$post_params = json_encode($params);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST,$method); // for Create -> "PUT" & Update ->"POST" //
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Content-Length: '.strlen($post_params));
curl_setopt($curl, CURLOPT_POSTFIELDS, $post_params);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$result = curl_exec($curl);
$response = json_decode($result, true);
print_r($response);
curl_close($curl);
Output :
(
[changedEntityType] => ClientCorporation
[changedEntityId] => 304
[changeType] => INSERT
)
I need to implement Pushwizard in PHP to send push notification.
I have implemented this php code
$api_key = "MY_API_KEY";
$msg = "Website PHP msg.";
$data = array("command" => "send", "message" => $msg);
$data_string = json_encode($data);
$ch = curl_init('https://pushwizard.com/api/'.$api_key.'/json');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array ( 'Content-Type: application/json', 'Content-Length: ' . strlen($data_string)));
$result = curl_exec($ch);
print $result;
But it gives me this error message.
{"error":"0002","error_msg":"app not found"}
I have check my configuration. From pushWizard admin panel I can able to send push notification, It works fine there. But from PHP api it is giving me an error.
I have googled it but not able to find any answer.
Does any own implemented it??
Thanks in advance.
You are probably trying to use the API with the App Key instead of the API Key.