I have been trying for the past how long to figure out how to go from the sandbox APNS to production APNS. Below is the PHP code used to send notifications to my app.
$passphrase = 'SomethingStrong';
$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', $_SERVER['DOCUMENT_ROOT'] . '/ck.pem');
stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);
$fp = stream_socket_client(
'ssl://gateway.sandbox.push.apple.com:2195', $err,
$errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);
if (!$fp)
{
//return json_encode(array('response' => 'connection_fail'));
}
$body['aps'] = array(
'alert' => $message,
'sound' => 'default'
);
$payload = json_encode($body);
$msg = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . $payload;
$result = fwrite($fp, $msg, strlen($msg));
if (!$result)
return json_encode(array('response' => 'unsuccessful'));
else
return json_encode(array('response' => 'successful'));
fclose($fp);
This whole thing works when I keep the URL ssl://gateway.sandbox.push.apple.com:2195, but when I when I change it to ssl://gateway.push.apple.com:2195 no notifications come through my app, yet PHP outputs that it was sent successfully.
I'm code-signing with the development certificates.
I'm new to notifications and have never used them before, so sorry if I'm doing something really obvious. Thanks.
There are two things, you need to confirm:
You generate your build with distribution provisioning profile
You use production pem file for sending PUSH from your PHP server
You are setting the correct ssl socket ssl://gateway.push.apple.com:2195 for production.
Check the certificates or the ck.pem file you have upload to server should be production.
Also check for the provisioning profile which you select for build should be Distribution profile.
Everything was setup correctly, but I had an issue with my Ad Hoc profile and the way it was code-signing. All is good now! Thanks!
Related
I'm following Ray Wenderlich's IOS Notification tutorial:
http://www.raywenderlich.com/32960/apple-push-notification-services-in-ios-6-tutorial-part-1
Its all gone well and the original tests which establish that I can connect to APNS and that my certificates are recognized all went well. I have an error though when trying to run the php script he supplies, though I have added the details requested and the ck.pem to the folder.
The full error is:
Parse error: parse error in /Users/carsoncarbery/Desktop/SimplePush/simplepush.php on line 10
And here is the code in my PHP script:
<?php
// Put your device token here (without spaces):
$deviceToken = '521fbe4fbee30cb68ec7303a12a9d1ea56d89e6c18557479311f9417a2208415';
// Put your private key's passphrase here:
$passphrase = 'pushchat!’;
// Put your alert message here:
$message = 'My first push woo hoo :-)’;
////////////////////////////////////////////////////////////////////////////////
$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'ck.pem');
stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);
stream_context_set_option($ctx, 'ssl', 'cafile', 'entrust_2048_ca.cer');
// 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);
I should add that I'm new to this (hence trying to learn from the tutorials) and dont understand PHP yet, I've checked for solutions for this online but haven't found any references that can help.
Many thanks for any assistance
I got the same error as you mentioned above. I fixed it by doing the following steps,
download and unzip SimplePush.zip
Delete ck.pem from SimplePush folder
Copy and paste your ck.pem in SimplePush folder
Edit device token and passphrase in simplepush.php
Follow further steps from tutorial
After looking at a little more I saw that 'pushchat!’, didn't have the correct closing comment '. As this was copied from a lesson, I'm not sure how it got in there. Any way thanks for the comments and help.
I am integrating push notification into my iOS app. I am following this tutorial http://www.raywenderlich.com/32960/apple-push-notification-services-in-ios-6-tutorial-part-1 to send push.
I can successfully sent push on development mode. When I tried to send push from server on Distribution mode, push not received on device.
I have
a) created aps_develoment.cer for development purpose and aps_production.cerfor distribution purpose
b) export push.p12 from keychain and converted to pushChatKey.pem
c) created pushChatCertDev.pem from aps_development.cer and pushChatCertDis.pem from aps_production.cer
d) created combineDev.pem from pushChatCertDev.pem and pushChatKey.pem. Similarly created combineDis.pem from pushChatCertDis.pem and pushChatKey.pem.
I followed the above tutorial and successfully sent push in Development mode from terminal. PHP script is
<?php
$deviceToken ='eff527a7f5f3127d55a63269538498bff2134c6d7b72e4920809d0a102aa04d6';
// Put your private key's passphrase here:
$passphrase = 'Nopass#1234';
// Put your alert message here:
$message = 'TJS push notification test';
$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'combineDev.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);
?>
Push sent into iPhone 4S on development mode. Then I download same app from app store into my iPhone 4S. I uploaded php script into my server and replace combineDev.pem with combineDis.pem. When I run php script from server it shows
Connected to APNS Message successfully delivered
but no push received at iPhone 4S. I also tried with replacing ssl://gateway.sandbox.push.apple.com:2195 by ssl://gateway.push.apple.com:2195 but no luck.
Please help. I am stuck here for 8-10 hours.
Thanks
In your above code change the line
stream_context_set_option($ctx, 'ssl', 'local_cert', 'combineDev.pem');
to
stream_context_set_option($ctx, 'ssl', 'local_cert', 'combineDis.pem');
How to implpement on below code for send 1000 push notification at same time server to ios deivce using php.
function push_iphone($deviceTokenid,$message){
$passphrase = '12345';
$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'ck.pem');
stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);
$fp = stream_socket_client(
'ssl://gateway.sandbox.push.apple.com:2195', $err,
$errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);
if (!$fp)
return false;
echo 'Connected to APNS' . PHP_EOL;
// Create the payload body
$body['aps'] = array(
'alert' => $message,
'message' => $message,
'sound' => 'default',
'flag' => '0'
);
// Encode the payload as JSON
$payload = json_encode($body);
//echo $device_token_id;
// Build the binary notification
$msg = chr(0) . pack('n', 32) . pack('H*', $deviceTokenid) . pack('n', strlen($payload)) . $payload;
// Send it to the server
$result = fwrite($fp, $msg, strlen($msg));
if(!$result){
echo "tst";
return true;
}else{ echo "test ";
return false;
}
fclose($fp);
}
Can any body help me about it.
Apple has an answer to this in their documentation:
You may establish multiple connections to the same gateway or to
multiple gateway instances. If you need to send a large number of
remote notifications, spread them out over connections to several
different gateways. This improves performance compared to using a
single connection: it lets you send the remote notifications faster,
and it lets APNs deliver them faster.
Keep your connections with APNs open across multiple notifications;
don’t repeatedly open and close connections. APNs treats rapid
connection and disconnection as a denial-of-service attack. You should
leave a connection open unless you know it will be idle for an
extended period of time—for example, if you only send notifications to
your users once a day it is ok to use a new connection each day.
By threading your php application you can achieve this at (almost) the same time. Take a look at Patterns for PHP multi processes? for a direction which to take to setup forks.
I have read many answers on this topic but issue not resolved by suggested solutions. My issue is that I am not getting Push Notifications for Production Certificate. Same Push Notifications are successfully getting in Development certificate. I specially want to say that my device tokens for production and development environment is entirely different. So there is no issue for same device token. Also, i am using different profiles for both. Means to say, there is not configuration issue on app level.
We are using PHP as a server that is sending push notifications.
Here are my two questions:
Is there any thing missing at server side? For which PHP server is sending Push Notifications for development environment successfully and for Production environment, its generating problem?
Am i missing any thing in the app?
I will be very thankful to all of you. I am stuck on this issue. Many Thanks in advance
check php function for iphone push notification...
function iphone_notification($deviceToken,$message,$not_type,$sound,$vibration_type){
$passphrase = '******';
$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'ck.pem');
stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);
$fp = stream_socket_client('ssl://gateway.push.apple.com:2195', $err, $errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);
$body['aps'] = array('alert' => $message, 'sound' => $sound);
$body['notification_type'] = $not_type;
$body['vibration_type'] = $vibration_type;
//1 = news;
//99 = other;
//echo $err."<br>".$errstr;
$payload = json_encode($body);
$msg = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . $payload;
$result = fwrite($fp, $msg, strlen($msg));
fclose($fp);
}
I faced this same problem. If your .pem file is correct then using following setting you will get push notification. (check terminal commands to make .pem)
//for development profile
$apns_url = 'gateway.sandbox.push.apple.com';
//for production you should use this
$apns_url = 'gateway.push.apple.com';s.
for more detail check this link1 >> link2 >>
My PNS is working good in development mode. I have done this using raywendelich blog. same way i have created certificates in production mode and run same script from server but not receiving any notification.
Which additional step needed when we are testing in production mode from our server.Its very urgent need. plase help what to do for production mode.
our PHP code
<?php
// Put your device token here (without spaces):
$deviceToken = 'd5d89cab86e6f1a3cfa66dd853f3f4d7dd932c4a6da793cb9c86d31e9cfcb31f';
// Put your private key's passphrase here:
$passphrase = '*******';
// Put your alert message here:
$message = '****';
$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'ckm.pem');
stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);
// Open a connection to the APNS server
$fp = stream_socket_client(
'ssl://gateway.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);
You need to create the production APNS certificate (very similar to the way you created the developer certificate) and use that to connect to Apple instead. The development and production pushtokens are different so in order to get your production push token, you'll have to build an AdHoc version of your app. When you install the ad-hoc version on your device, it should ask you if you want to recieve push tokens. Accept and your code should send your production push token to your server (I'm assuming you save all pushtokens on a server somewhere). This is the token you need to use to test your production push code.