I have a php script that is coecting to Apple APNS services to send push notifications to specified users. The script works correctly on localhost when I post to it from swift, but when I move the script over to the free web host service (Byet), I am not receiving the notification on my phone. I am also not receiving any errors which is the weird part, also I will mention that when I pass the data directly into the url, the script works correctly, it only does not work when I pass it in from swift. I will post my swift file and php file below. Any help would be greatly appreciated.
SWIFT FILE
let url = NSURL(string: "http://localhost/push/test.php")!
let request = NSMutableURLRequest(URL: url)
request.HTTPMethod = "POST"
let body = "devicetoken=(token)&message= (PFUser.currentUser()!.useame!) is coming to your event!"
request.HTTPBody = body.dataUsingEncoding(NSUTF8StringEncoding)
NSURLSession.sharedSession().dataTaskWithRequest(request) { (data, response, error) in
if error == nil{
//success
//communicate back
dispatch_async(dispatch_get_main_queue(), {
do{
let json = try NSJSONSerialization.JSONObjectWithData(data!, options: .MutableContainers) as? NSDictionary
guard let parseJSON = json else{
print("error while parsing")
retu
}
let id = parseJSON["message"]
if id != nil{
print(parseJSON)
}
}catch{
print("caugh an error")
}
})
}
else{
print(error?.localizedDescription)
}
}.resume()
PHP File
<?php
$deviceToken = htmlentities($_REQUEST["devicetoken"]);
$message = htmlentities($_REQUEST["message"]);
$passphrase = 'mypassword';
$url = 'myurl';
if (empty($deviceToken) || empty($message)){
$retuArray["status"] = "400";
$retuArray["message"] = "Missing Information";
echo json_encode($retuArray);
retu;
}
$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'ck.pem');
stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);
// Open a coection to the APNS server
$fp = stream_socket_client(
'ssl://gateway.sandbox.push.apple.com:2195', $err,
$errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);
if (!$fp)
exit("Failed to coect: $err $errstr" . PHP_EOL);
echo 'Coected to APNS' . PHP_EOL;
// Create the payload body
$body['aps'] = array(
'alert' => $message,
'sound' => 'default',
'link_url' => $url,
);
// Encode the payload as JSON
$payload = json_encode($body);
// Build the binary notification
$msg = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . $payload;
// Send it to the server
$result = fwrite($fp, $msg, strlen($msg));
if (!$result)
echo 'Message not delivered' . PHP_EOL;
else
echo 'Message successfully delivered' . PHP_EOL;
// Close the coection to the server
fclose($fp);
