I have a problem with push notification in my app. I just started to work with push notifications so I dont know what's the reason of my problem, and i cant find the solution in google.
So, I used this tutorial http://www.raywenderlich.com/3443/apple-push-notification-services-tutorial-part-12 as the example of programming of push notifications. When I run "php simplepush.php" on my terminal everything is fine and I get the notification on my device. But when I load that script on the server and trying to run it from there there is no actions performed. only 30 sec of waiting and message "Failed to connect: 110 Connection timed out"
Here os the code of my script
<?php
Put your device token here (without spaces):
$deviceToken = '***************';
// Put your private key's passphrase here:
$passphrase = '**************';
// Put your alert message here:
$message = 'My first push notification!';
////////////////////////////////////////////////////////////////////////////////
$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'ck.pem');
stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);
// Open a connection 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 connect: $err $errstr" . PHP_EOL);
echo 'Connected to APNS' . PHP_EOL;
// Create the payload body
$body['aps'] = array(
'alert' => $message,
'sound' => 'default'
);
// 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 connection to the server
fclose($fp);
Yes, as NewObjective clarified, 2195 & 2196 ports have to be open. To open these ports you might ask Bluehost technical support team (ticket/chat), but you have to have dedicated IP address before that.
But there is another issue. If you try to send push notification message right after dedicated IP address is set and both ports are open you might receive the same "Connection timed out" error. You can test that problem by logging in to your server via SSH and running "telnet gateway.sandbox.push.apple.com 2195" command. If you receive "Connection timed out" error - you can try to contact Bluehost support team, but they can't solve this problem.
If you try to telnet any other server with 2195 opened port you will also receive that error. I had about 10 chats with Bluehost support team, opened 10 or so tickets, but they really couldn't help me. But two weeks later everything began to work. I think it was because turning dedicated IP on takes some time, but I'm not sure. Anyway, it very interesting that Bluehost support team always answers that the problem is on the Apple side, even if you tell them that any other server with 2195 opened port isn't available too)
Related
I've try to send mail using perl with attachment I've tried with Email::Stuff and MIME::Lite,during run time i got some error as Authentication failed or server not connected can anybody help me?
corresponding code is:
use MIME::Lite;
use Net::SMTP;
### Adjust sender, recipient and your SMTP mailhost
my $from_address = 'atme04#gmail.com';
my $to_address = 'thiyagu040#gmail.com';
my $mail_host = 'smtp.gmail.com';
### Adjust subject and body message
my $subject = 'A message with 2 parts ...';
my $message_body = "Here's the attachment file(s) you wanted";
my $your_file_zip = 'my.zip';
$msg = MIME::Lite->new (
From => $from_address,
To => $to_address,
Subject => $subject,
Type =>'multipart/mixed'
) or die "Error creating multipart container: $!\n";
MIME::Lite->send('smtp', 'smtp.gmail.com' ,
Port =>465 ,
Timeout=>320 ,
Debug => 1 ,
Hello => $mail_host,
User => $from_address,
Password => 'Thiyagu.04' );
#$mime_msg->send() or die "Error sending message: $!\n";
#MIME::Lite->send('smtp',$mail_host,AuthUser=> $from_address, AuthPass=>"apssword");
$msg->send();
error message is;
SMTP Failed to connect to mail server: A connection attempt failed because the connected party did not properly respond aft
er a period of time, or established connection failed because connected host has failed to respond.
at mail.pl line 54.
thanks in advance
The error message seems pretty clear to me. The problem is with your connection to the mail server - so changing the module that you're using is unlikely to achieve anything useful. I don't know enough about Gmail's server settings to comment on what the problem is, but this page has some suggestions that you might follow. Specifically, you could check that the Gmail account has 'allow authentication' turned on and that your mail client (the Perl program) is using SSL for the connection.
Also, this might be easier if you used an email that was specifically designed for use with Gmail. Email::Send::SMTP::Gmail looks tailor-made for your requirements.
i'm using Zend_Mail to send out an email from Aruba PEC.
$config = array('auth'=>'login',
'ssl' => 'ssl',
'port' => 465,
'username'=>'user#pec.it',
'password'=>'pass');
$tr = new Zend_Mail_Transport_Smtp('62.149.152.91',$config);
Zend_Mail::setDefaultTransport($tr);
$mail = new Zend_Mail();
$mail->setBodyHtml('Mail test');
$mail->setFrom('user#pec.it');
$mail->addTo('marco#email.it');
$mail->setSubject('Test');
$mail->send();
but i receive this error: " Could not open socket ".
Now if i try to exceute this script on my local pc it works correctly, but wen i try to exceute it on server in my local area network i receive this error.
than i i try with another ip address (no certifaction account) it works correctly.
(so i think that is a problem of configuration server??)
thanks
You need to have OpenSSL installed on the PC. Is your server Windows? It is a bit of a pain if you are.
I have created some forms using zendframework on my local machine that send the form content via email.
I would like to test the functionality locally and have read some posts regarding configuring the php.ini file to do this but not sure which is the correct method ?
can anyone help me with this, many thanks
On Windows you will have to use SMTP to send the message. There is a drop in fake sendmail for Windows but it still requires an SMTP server.
You could use your ISP's sendmail server if they offer one, or you can set one up on the local machine. 1, 2, 3, 4
Since you are using Zend Framework, you can alternatively use Zend_Mail to send through an SMTP server (Zend_Mail can also use sendmail, but since it isn't configured, you can't use that transport). In that case see Sending via SMTP, SMTP Authentication, and Securing SMTP Transport.
Here is some sample code for sending an SMTP message with AUTH and TLS security.
<?php
require_once 'Zend/Mail.php';
require_once 'Zend/Mail/Transport/Smtp.php';
$config = array('ssl' => 'tls',
'port' => '465', // 25 if no ssl
'auth' => 'login',
'username' => 'user',
'password' => 'password');
$transport = new Zend_Mail_Transport_Smtp('smtp.example.com', $config);
$mail = new Zend_Mail();
$mail->addTo('user#domain')
->setSubject('Mail Test')
->setBodyText("Hello,\nThis is a Zend Mail message...\n")
->setFrom('sender#domain');
try {
$mail->send($transport);
echo "Message sent!<br />\n";
} catch (Exception $ex) {
echo "Failed to send mail! " . $ex->getMessage() . "<br />\n";
}
Also note, your ISP may not require you to auth at all if you are sending from one of their IP addresses, but you probably do have to authenticate with your username and password, in which case you will want to use TLS.
I have meet a big problem when I was doing developing about Apple Push Notification Service.
I wrote a PHP script(apns.php) which will use a PEM(ck.pem) file as security certificate and build a SSL connection to APNS when I post a HTTP request to it, but it always failed when it tried to connect the service port ( ssl://gateway.sandbox.push.apple.com:2195). The PHP script is deployed on a Linux Hosting of GoDaddy (it should support SSL).
I have already registered to be a member of iOS developer program,and I have registered the apple push notification service for my application. And I have generated a certificate file and a key file (cert.p12 and key.p12) from Keychain Access and translate them into PEM files(cert.pem and key.pem) and join them into one(ck.pem) and put it in the same directory of PHP script .
I want to know if there is something wrong I did ! Would you refer to the attachment of the files that you may need?
Thanks very much!
The HTTP request is something like below.
http://www.insidertracker.net/apns/apns.php?message=&badge=2&sound=received5.caf
The response message of my request is below:
Warning: stream_socket_client() [function.stream-socket-client]:
unable to connect to ssl://gateway.sandbox.push.apple.com:2195
(Connection refused) in /home/content/40/6967940/html/apns/apns.php on
line 25 Failed to connect 111 Connection refused
The PHP script:
<?php
$deviceToken = '0535dda1 6fd04e87 ed0a8194 d418a6c1 99eec462 8a871891 d062018d c6af4f99';
$pass = 'Php1234'; // Passphrase for the private key (ck.pem file)
// Get the parameters from http get or from command line
$message = $_GET['message'] or $message = $argv[1] or $message = 'You have an important message from InsiderTracker';
$badge = (int)$_GET['badge'] or $badge = (int)$argv[2];
$sound = $_GET['sound'] or $sound = $argv[3];
// Construct the notification payload
$body = array();
$body['aps'] = array('alert' => $message);
if ($badge)
$body['aps']['badge'] = $badge;
if ($sound)
$body['aps']['sound'] = $sound;
/* End of Configurable Items */
$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'ck.pem');
// assume the private key passphase was removed.
stream_context_set_option($ctx, 'ssl', 'passphrase', $pass);
// connect to apns
$fp = stream_socket_client('ssl://gateway.sandbox.push.apple.com:2195', $err, $errstr, 60, STREAM_CLIENT_CONNECT, $ctx);
// $fp = fsockopen('ssl://gateway.sandbox.push.apple.com:2195', 2195, $err, $errstr, 30);
if (!$fp) {
print "Failed to connect $err $errstr\n";
return;
}
else {
print "Connection OK\n<br/>";
}
// send message
$payload = json_encode($body);
$msg = chr(0) . pack("n",32) . pack('H*', str_replace(' ', '', $deviceToken)) . pack ("n",strlen($payload)) . $payload;
print "Sending message :" . $payload . "\n";
fwrite($fp, $msg);
fclose($fp);
?>
It's my understanding that GoDaddy prohibits outgoing connections on exotic ports (anything other than 80 a perhaps a few others). You're probably out of luck trying to roll your own push service hosted on GoDaddy. I have had luck doing what you're attempting with bluehost.
For general advice on developing a push service in PHP, these two articles were invaluable: One Two
I also got this error. Following things you have to remember:
Path of ios push cert file should be correct. Check on online portals using device token and push cert.
Your server is enabled for 2195 port. Check on terminal by using telnet command.
Check ssl supported by your php version if using ssl://gateway.sandbox.push.apple.com and tls if using tls://gateway.sandbox.push.apple.com
The data you are sending in the push or token etc should be equal or less than the specified bytes mentioned in the apple doc. link is mentioned below.
You can also use api get reference for sending push Development server: api.development.push.apple.com:443 Production server: api.push.apple.com:443
https://developer.apple.com/library/archive/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/CommunicatingwithAPNs.html#//apple_ref/doc/uid/TP40008194-CH11-SW1
To connect my server with the APN server I use the following code.
// coonecting the apn server
$apnsHost = 'gateway.sandbox.push.apple.com';
$apnsPort = 2195;
$apnsCert = 'apns-dev.pem';
$streamContext = stream_context_create();
stream_context_set_option($streamContext, 'ssl', 'local_cert', $apnsCert);
$apns = stream_socket_client('ssl://' . $apnsHost . ':' . $apnsPort, $errorNo, $errorString, 2, STREAM_CLIENT_CONNECT, $streamContext);
But I am failed to connect, I print the $errorNo and $errorString
the output was:
error: Connection timed out
errorNo: 110
But I am also getting the following warnings in errorLog:
Unable to connect to ssl://gateway.sandbox.push.apple.com:2195 (Connection timed out) in /home/riseupla/public_html/applications/apn/apn.php on line 35
What shoud I do?
NOTE:
I can send push notifications by my mac project (using push me baby project).
But my PHP project failed to connect.
You do not say if you run the php from a different machine than your Mac, but if you are using a hosted service for your php site, then it might be that the ISP has blocked port 2195 (and 2196).
Try to run your php on the same machine as you use when running your Mac project and see if this helps. If that's the case, then you ISP has most likely blocked the ports, and you will have to either find a different ISP or have them open the ports.
Claus
You probably solved it.
But for those interested to find out if the ports are blocked: IF you have SSH access to the server a simple way is to check if you can telnet to the server. This way i found out my provider blocked the ports...
:~ telnet gateway.sandbox.push.apple.com 2195
:~ telnet gateway.sandbox.push.apple.com 2196
You should see something like:
Trying 17.172.232.236...
Connected to gateway.sandbox.push-apple.com.akadns.net.
Escape character is '^]'.
may be that your server's IP variety, it must be fixed so work!!