I am trying to send user-specific web notifications with onesignal and i solved everything except this variable null problem.
I use that variable multiple times and there is no problem except these lines:
<?php
if(isset($_POST["sub"]))
{
$my_variable= $_POST["t1"];
$SQL = "some sql here";
if (mysqli_query($db, $SQL)) {
echo $my_variable;
echo "<br>";
function sendMessage(){
$content = array(
"en" => 'test message'
);
$fields = array(
'app_id' => "5b0eacfc-3ac8-4dc6-891b-xxxxx",
'filters' => array(array("field" => "tag", "key" => "key", "relation" => "=", "value" => "$my_variable")),
'data' => array("foo" => "bar"),
'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 xxxxxxx'));
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);
The result is :
1 JSON sent: {"app_id":"5b0eacfc-3ac8-4dc6-891b-xxxxx","filters":[{"field":"tag","key":"key","relation":"=","value":"null"}],"data":{"foo":"bar"},"contents":{"en":"test message"}}
I tried so many variations with/without quotas , json_encode() function but couldn't pass that variable to that array.
Your variable is out of scope.
You define $my_variable outside of the function sendMessage(), but proceed to try and use it within the function, without passing it as a parameter.
This can be fixed with the following:
function sendMessage($filterValue)
{
$content = array(
"en" => 'test message'
);
$fields = array(
'app_id' => "5b0eacfc-3ac8-4dc6-891b-xxxxx",
'filters' => array(array("field" => "tag", "key" => "key", "relation" => "=", "value" => $filterValue)),
'data' => array("foo" => "bar"),
'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 xxxxxxx'));
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($my_variable);
$return["allresponses"] = $response;
$return = json_encode( $return);
Related
I`m trying to send push notifications to android users using the following script:
<?PHP
function sendMessage(){
$content = array(
"en" => 'Testing Message'
);
$fields = array(
'app_id' => ".....",
'included_segments' => array('All'),
'data' => array("foo" => "bar"),
'large_icon' =>"ic_launcher_round.png",
'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 .....'));
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");
?>
However json responds "false" when running it:
JSON sent
{
"app_id":".....",
"included_segments":["All"],
"data":{"foo":"bar"},
"large_icon":"ic_launcher_round.png",
"contents":{"en":"Testing Message"}
}
JSON received
{
"allresponses":false
}
What does that message mean? APP ID and REST API KEy are correct.
I am using following PHP script to send OneSignal push notifications to subscribed devices.
<?PHP
function sendMessage(){
$content = array(
"en" => 'Testing Message desde el backend small icon '
);
$fields = array(
'app_id' => "xxxx",
'filters' => array(array("field" => "tag", "key" => "correo", "relation" => "=", "value" => "xxx")),
'data' => array("foo" => "bar"),
'small_icon' =>"ic_push",
'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 xxxxx'));
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");
?>
This script is working fine, and the notifications are sent.
Now I need to add more filters, for example I need to add a filter to two other segments that I have created at the OneSignal dashboard.
The segments are "skateboard" and "administrators".
How can I add these two segment to the filters array?
You need to pass segments name array in fields like you are passing tags.
$content = array(
"en" => 'Notification Message Here..'
);
$heading = array(
"en" => 'Heading goes here..'
);
$fields = array(
'app_id' => 'XXXXXXXXXXXXXXXXXXXXX',
'contents' => $content,
'headings' => $heading,
'included_segments' => array('SegmentName1','SegmentName2'),
'tags' = array(array("key" => "state", "relation" => "=", "value" => 'Delhi'))
);
$fields = json_encode($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 XXXXXXXXXXXXXXXXXXX'));
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;
i have integrated one-signal in my web application, notifications are working fine but if considering web page title in push notification title.
i need to set it custom title in my push notification.
i need to set custom message in place of "Dashboard"
Here is my code:
$content = array(
"en" => 'Hello Hii..!!'
);
$fields = array(
'app_id' => 'APP_ID',
'include_player_ids' => ['ids'],
'data' => array("foo" => "bar"),
'url' => 'URL',
'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 AuthorizationKey';
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;
set heading in your fields array
$content = array(
"en" => 'Your message..!!'
);
$heading = array(
"en" => "Your custom title message"
);
$fields = array(
'app_id' => 'YOUR_APP_ID',
'include_player_ids' => [ids],
'data' => array("foo" => "bar"),
'url' => 'http://www.yoursite.com',
'contents' => $content,
'headings' => $heading
);
Use this one as mention bellow
public function sendPush($players_id,$massage,$data,$heading){
// $players_id your device id where you want to push
$data1[]=$players_id;
//message for your push
$content = array(
"en" => $massage
);
// if you want to send data in for of JSON or some values
$data_response=array(
"value" => $data
);
//you can add heading through this
$heading = array( "en" => $heading);
// print_r($cat_data);
$fields = array(
'app_id' => 'YOUR_APP_ID',
'include_player_ids' => $data1,
'contents' => $content,
'headings' => $heading,
'data' =>$data_response
);
$fields = json_encode($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 YOUR_REST_API_KEY'));
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;
}
I am using the following PHP Code to send push notification via One Signal:
function sendMessage(){
$content = array(
"en" => 'English Message'
);
$fields = array(
'app_id' => $appId,
'included_segments' => array('All'),
'data' => array("foo" => "bar"),
'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 '.$restKey));
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");
And I am getting the following response:
JSON sent: {"app_id":null,"included_segments":["All"],"data":{"foo":"bar"},"contents":{"en":"English Message"}} JSON received: "{\"allresponses\":\"{\\\"adm_big_picture\\\":null,\\\"adm_group\\\":null,\\\"adm_group_message\\\":null,\\\"adm_large_icon\\\":null,\\\"adm_small_icon\\\":null,\\\"adm_sound\\\":null,\\\"amazon_background_data\\\":false,\\\"android_accent_color\\\":null,\\\"android_group\\\":null,\\\"android_group_message\\\":null,\\\"android_led_color\\\":null,\\\"android_sound\\\":null,\\\"android_visibility\\\":null,\\\"app_id\\\":\\\"63c6c8f7-694b-4c68-abc1-d820d9bbbec1\\\",\\\"big_picture\\\":null,\\\"buttons\\\":null,\\\"canceled\\\":false,\\\"chrome_big_picture\\\":null,\\\"chrome_icon\\\":null,\\\"chrome_web_icon\\\":\\\"\\\",\\\"chrome_web_image\\\":\\\"\\\",\\\"content_available\\\":false,\\\"contents\\\":{\\\"en\\\":\\\"This is a new message.\\\"},\\\"converted\\\":0,\\\"data\\\":null,\\\"delayed_option\\\":\\\"immediate\\\",\\\"delivery_time_of_day\\\":\\\"4:00 PM\\\",\\\"errored\\\":0,\\\"excluded_segments\\\":[],\\\"failed\\\":0,\\\"firefox_icon\\\":\\\"\\\",\\\"headings\\\":{\\\"en\\\":\\\"New Message\\\"},\\\"id\\\":\\\"8d56f592-8f43-461a-94e8-2fe9922ba844\\\",\\\"include_player_ids\\\":null,\\\"included_segments\\\":[\\\"All\\\"],\\\"ios_badgeCount\\\":null,\\\"ios_badgeType\\\":null,\\\"ios_category\\\":null,\\\"ios_sound\\\":null,\\\"isAdm\\\":false,\\\"isAndroid\\\":false,\\\"isChrome\\\":false,\\\"isChromeWeb\\\":true,\\\"isFirefox\\\":true,\\\"isIos\\\":false,\\\"isSafari\\\":true,\\\"isWP\\\":false,\\\"isWP_WNS\\\":false,\\\"large_icon\\\":null,\\\"priority\\\":null,\\\"queued_at\\\":1492523636,\\\"remaining\\\":0,\\\"send_after\\\":1492523636,\\\"small_icon\\\":null,\\\"successful\\\":3,\\\"tags\\\":null,\\\"filters\\\":null,\\\"template_id\\\":null,\\\"ttl\\\":null,\\\"url\\\":\\\"\\\",\\\"web_buttons\\\":null,\\\"wp_sound\\\":null,\\\"wp_wns_sound\\\":null}\"}"
However the Push Notification is not appearing in the One Signal Dashboard and neither being received by those who subscribed.
Can someone help please :) ?
your mistake is here.
'Authorization: Basic '.$restKey));
Correct one is.....
'Authorization: Basic "'.$restKey."'"));
I am working this code and this is working fine
function SendOneSignalMessage($message,$empid){
// Your code here!
$fields = array(
'app_id' => 'xxxxxxxxxxxxxxxxxx',
'include_player_ids' => [$empid],
'contents' => array("en" =>$message),
'headings' => array("en"=>"etc"),
'largeIcon' => 'https://cdn4.iconfinder.com/data/icons/iconsimple-logotypes/512/github-512.png',
);
$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 xxxxxxxxxxxxx));
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);
}
I have this php script:
<?php
function sendMessage(){
$content = array(
"en" => 'text message test'
);
$fields = array(
'app_id' => "XXXXXX",
'included_segments' => array('All'),
'data' => array("foo" => "bar"),
'headings' => array("en" => "Test message!!"),
"isAndroid"=>true,
// 'android_group' => 'Tanks',
// 'android_group_message' => array("en" => "message"),
'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',
'Authorization: Basic xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'));
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();
?>
But is doesn't end notifactions to android users. Is there a example to send pushnotifications from php over OneSignal to android devies?
Well, i did not add my one sognal authentication code ;)