Custom iOS payload with Urban airship in PHP - php

I am trying to send some custom data with the payload to iOS devices. I use the following code to successfully send pushes to all devices but I need to send some additional data. I couldn't find any documentation for urban airship and everything I tried just gives me errors.
Code:
$contents = array();
$contents['badge'] = "+1";
$contents['alert'] = $message;
$contents['sound'] = "cat.caf";
$notification = array();
$notification['ios'] = $contents;
$platform = array();
array_push($platform, "ios");
$push = array("audience"=>"all", "notification"=>$notification, "device_types"=>$platform);
$json = json_encode($push);
$session = curl_init(PUSHURL);
curl_setopt($session, CURLOPT_USERPWD, APPKEY . ':' . PUSHSECRET);
curl_setopt($session, CURLOPT_POST, True);
curl_setopt($session, CURLOPT_POSTFIELDS, $json);
curl_setopt($session, CURLOPT_HEADER, False);
curl_setopt($session, CURLOPT_RETURNTRANSFER, True);
curl_setopt($session, CURLOPT_HTTPHEADER, array('Content-Type:application/json', 'Accept: application/vnd.urbanairship+json; version=3;'));
$content = curl_exec($session);

After some research I came to this. In the 'contents' section of your code add this line:
$contents['extra'] = array("message"=>"another message");
This should enable you to send additional custom data along with your push.

Related

How to find my android device ID to use with php notification script using Firebase?

I'm trying to use a script i found on SO, it's a php script that pushes notifications using Firebase, in my app i'm trying to push notification to my Android device but i don't know how to identify my device's ID/Key, the script is the following:
$server_key=""; // get this from Firebase project settings->Cloud Messaging
$user_token=""; // Token generated from Android device after setting up firebase
$title="New Message";
$n_msg="The is a message";
$ndata = array('title'=>$title,'body'=>$n_msg);
$url = 'https://fcm.googleapis.com/fcm/send';
$fields = array();
$fields['data'] = $ndata;
$fields['to'] = $user_token;
$headers = array(
'Content-Type:application/json',
'Authorization:key='.$server_key
);
$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('FCM Send Error: ' . curl_error($ch));
}
curl_close($ch);
I'm trying to identify the $user_token so i can use this script to serve my purpose.

PHP - Receive JSON from Dialogflow Parse to URL

Trying to create a php script which will receive JSON from Dialogflow webhook.
The data then needs to be parsed and
<?php
$input = json_decode(file_get_contents('php://input'), true);
$messageId = $input["resolvedQuery"];
$text = print_r($input,true);
file_put_contents('output.txt', var_export($text, TRUE));
$url = "https://autoremotejoaomgcd.appspot.com/sendmessage?key=APAue83jLrt7xFeQoGjgKq&message=" . $messageId;
$data = array("result" => "resolvedQuery");
$ch = curl_init($url);
$data_string = json_encode($messageId);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, array("resolvedQuery"=>$data_string));
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER,
array('Content-Type:application/json',
'Content-Length: ' . strlen($data_string))
);
$result = curl_exec($ch);
curl_close($ch);
?>
The data i need is from resolvedQuery but im not getting any output.
Would someone point me in the right direction please
So from my understanding, you want resolvedQuery from api.ai in your webhook. All you need to do is change:
$messageId = $input["resolvedQuery"];
to
$messageId = $input['result']['resolvedQuery'];
Check and tell me if it's working. I did skype, fb, and google integrations in php so if you need any help feel free to ask.

Facebook Messenger bot is not sending message to the sender

I was following this Tutorial to create A Facebook Messenger bot using php
Everything is working fine, except sender don't receive my message.
there is my code
<?php
$access_token =""; //Token
$verify_token = ""; //Verify Token
$hub_verify_token = null;
if(isset($_REQUEST['hub_challenge'])) {
$challenge = $_REQUEST['hub_challenge'];
$hub_verify_token = $_REQUEST['hub_verify_token'];
}
if ($hub_verify_token === $verify_token) {
echo $challenge;
}
$input = json_decode(file_get_contents('php://input'), true);
$sender = $input['entry'][0]['messaging'][0]['sender']['id'];
$message = $input['entry'][0]['messaging'][0]['message']['text'];
$message_to_reply = "Huh? You are talking to me?";
$url = 'https://graph.facebook.com/v2.6/me/messages?access_token='.$access_token;
//Initiate cURL.
$ch = curl_init($url);
//The JSON data.
$jsonData = '{
"recipient":{
"id":"'.$sender.'"
},
"message":{
"text":"'.$message_to_reply.'"
}
}';
//Encode the array into JSON.
$jsonDataEncoded = $jsonData;
//Tell cURL that we want to send a POST request.
curl_setopt($ch, CURLOPT_POST, 1);
//Attach our encoded JSON string to the POST fields.
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonDataEncoded);
//Set the content type to application/json
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
//curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded'));
//Execute the request
if(!empty($input['entry'][0]['messaging'][0]['message'])){
$result = curl_exec($ch);
}
?>
I can receive the message and see it, But the user don't receive back my message.
This is fixed by adding following lines of code before //Execute the request
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
Hope it helps.
$jsonData = '{
"messaging_type": "RESPONSE",
"recipient":{
"id": "' . $sender . '"
},
"message":{
"text": "teste web"
}
}';
$s="";
//Tell cURL that we want to send a POST request.
curl_setopt($ch, CURLOPT_POST, 1);
//Attach our encoded JSON string to the POST fields.
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonData);
//Set the content type to application/json
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$ret = curl_exec($ch);

Appkey is not entitled to use the Message Center in Urbanairship

I have created development app in urbanairship and trying to send push notification from server using PHP code.
My code:
define('APPKEY','xxx'); // Your App Key
define('PUSHSECRET', 'ytyy'); // Your Master Secret
define('PUSHURL', 'https://go.urbanairship.com/api/push/');
$notification = array();
$notification['alert'] = "alert";
$platform = array();
array_push($platform, "android"); //comment out if you don't want Android
$richpush = array();
$richpush['title'] = "title";
$richpush['body'] = $message;
if(strlen($deviceToken)>50){
$deviceToken=str_replace(" ","",$deviceToken);
$push = array("audience"=>array("device_token"=>$deviceToken), "notification"=>$notification, "device_types"=>$platform, "message"=>$richpush);
}else{
$deviceToken=str_replace(" ","-",$deviceToken);
$push = array("audience"=>array("apid"=>$deviceToken), "notification"=>$notification, "device_types"=>$platform, "message"=>$richpush);
}
$json = json_encode($push);
$session = curl_init(PUSHURL);
curl_setopt($session, CURLOPT_USERPWD, APPKEY . ':' . PUSHSECRET);
curl_setopt($session, CURLOPT_POST, True);
curl_setopt($session, CURLOPT_POSTFIELDS, $json);
curl_setopt($session, CURLOPT_HEADER, False);
curl_setopt($session, CURLOPT_RETURNTRANSFER, True);
curl_setopt($session, CURLOPT_HTTPHEADER, array('Content-Type:application/json', 'Accept: application/vnd.urbanairship+json; version=3;'));
$content = curl_exec($session);
echo "Response: " . $content . "\n";
But I'm getting :
Response: {"ok":false,"error":"The specified appkey [xxx] is not entitled to use the Message Center.","error_code":403}
Any help would be appreciated.
Message Center is not included in the Start or Basic accounts. Contact support and they will work with you to add Message Center entitlements.

Posting to posterous.com through posterous api

I am using curl and i want to post through posterous api. I am using this method posterous.com/api/newpost
My code is
$url="userid"
$posturl = "posterous.com/api/newpost";
$session = curl_init($posturl);
$postVars = array(
"site_id" => $url,
"body"=>$message
);
curl_setopt($session, CURLOPT_POST, true);
curl_setopt($session, CURLOPT_POSTFIELDS, $postVars);
curl_setopt($session, CURLOPT_HEADER, false);
curl_setopt($session, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($session);
$obj = json_decode($response,true);
curl_close($session);
But I am getting this error
<rsp stat="fail">
<err code="3001" msg="Invalid Posterous email or password" />
</rsp>
Please guide me on this
Thanks
Well you don't actually seem to be sending an email address or password, so I guess that's the problem.
All calls require user authentication
which is done by basic HTTP
authentication with a user's Posterous
email address and password.
You can do it like this:
$user = 'foo#bar.com';
$password = 'foobar';
curl_setopt($session, CURLOPT_USERPWD, $user . ":" . $password);

Categories