How to implement the push information function of iOS and other devices with PHP code?
It is possible that 6.1 million of terminal devices need to receive push messages, execute once, and enable multiple devices to receive information.
Pass by to give some useful advice, thank you!!!
Reply to discussion (solution)
This is not possible with PHP.
It is certainly not recommended that long connections remain attached until a message is returned to the user, so the server pressure must be great.
It is recommended to use the heartbeat to achieve this function, the client browser timed to the server to get the latest news.
In addition, if not PHP, such as Python, Nodejs, C + + and other implementation of the background, you can use long polling.
This is not possible with PHP.
It is certainly not recommended that long connections remain attached until a message is returned to the user, so the server pressure must be great.
It is recommended to use the heartbeat to achieve this function, the client browser timed to the server to get the latest news.
In addition, if not PHP, such as Python, Nodejs, C + + and other implementation of the background, you can use long polling.
6.1 million with a machine long connection is not likely, hehe
This is not what PHP can do!
PHP is a server-side script, not a server, not a network operating system
You just need to apply the mobile communications operating system to achieve your goals
You want the source code to go to the mobile version of->iphone, just a friend of the altar sent you need a post
I can also tell you how I do, in fact, is very simple, mainly with the Apple Development Account Generation certificate there to engage in.
Step 1
-------
First you have to use PHP on the server to open an interface, to provide to the iphone registration Device_token, which is loaded with your app's hand to make an HTTP request to this interface, the Device_token of each machine and some parameters submitted, and then you use PHP to receive, Save to Database
Step 2
-----------
Read the data in PHP, read the registered Device_token from the database, stitch it up into a string of prescribed formats, take the generated Apple certificate, and make a socket request to the Apple-provided push service API.
Key Code 1:
Stream_context_set_option ($ctx, ' SSL ', ' Local_cert ', $pemFile);//$pemFile for the certificate file, this is your own online search for build steps, you have to have an apple Development Account $ctx = Stream_context_create (); Stream_context_set_option ($ctx, ' SSL ', ' passphrase ', $passphrase); Open a connection to the APNS server, push service API, below is the sandbox environment $fp = stream_socket_client (' ssl://gateway.sandbox.push.apple.com : 2195 ', $err, $errstr, stream_client_connect| Stream_client_persistent, $ctx);
Key Code 2:
Create the payload body$body[' APS '] = Array (' alert ' = = Array ( ' body ' = $message,//' action-loc-key ' = ' Ba ') NGO App ', ' ), ' badge ' + $badge, ' sound ' = ' oven.caf ',); $deviceTokens = Array (), $payload = Fmfactory::getjson ()->encode ($body); $regs = Fmfactory::getquery ()->from ("Mobile_pn_register as M", "m.*")->where ("M.mobiletype= ' iOS ' and M.registered_app_id= ' {$app _record_id} ' ")->query (), if (!count ($regs)) {throw new Exception (mobile_not_register_ Push_notification_yet);}
Key Code 1 There is a copy error, should be as follows
$passphrase = "; $ctx = Stream_context_create (); Stream_context_set_option ($ctx, ' SSL ', ' Local_cert ', $pemFile); stream _context_set_option ($ctx, ' SSL ', ' passphrase ', $passphrase);//Open a connection to the APNS server, push service API, below is sandbox environment $fp = s Tream_socket_client (' ssl://gateway.sandbox.push.apple.com:2195 ', $err, $errstr, stream_client_connect| Stream_client_persistent, $ctx);
php Send mail? Push?
You want the source code to go to the mobile version of->iphone, just a friend of the altar sent you need a post
I can also tell you how I do, in fact, is very simple, mainly with the Apple Development Account Generation certificate there to engage in.
Step 1
-------
First you have to use PHP on the server to open an interface, to provide to the iphone registration Device_token, which is loaded with your app's hand to make an HTTP request to this interface, the Device_token of each machine and some parameters submitted, and then you use PHP to receive, Save to Database
Step 2
-----------
Read the data in PHP, read the registered Device_token from the database, stitch it up into a string of prescribed formats, take the generated Apple certificate, and make a socket request to the Apple-provided push service API.
Key Code 1:
Stream_context_set_option ($ctx, ' SSL ', ' Local_cert ', $pemFile);//$pemFile for the certificate file, this is your own online search for build steps, you have to have an apple Development Account $ctx = Stream_context_create (); Stream_context_set_option ($ctx, ' SSL ', ' passphrase ', $passphrase); Open a connection to the APNS server, push service API, below is the sandbox environment $fp = stream_socket_client (' ssl://gateway.sandbox.push.apple.com : 2195 ', $err, $errstr, stream_client_connect| Stream_client_persistent, $ctx);
Key Code 2:
Create the payload body$body[' APS '] = Array (' alert ' = = Array ( ' body ' = $message,//' action-loc-key ' = ' Ba ') NGO App ', ' ), ' badge ' + $badge, ' sound ' = ' oven.caf ',); $deviceTokens = Array (), $payload = Fmfactory::getjson ()->encode ($body); $regs = Fmfactory::getquery ()->from ("Mobile_pn_register as M", "m.*")->where ("M.mobiletype= ' iOS ' and M.registered_app_id= ' {$app _record_id} ' ")->query (), if (!count ($regs)) {throw new Exception (mobile_not_register_ Push_notification_yet);}
Take all the device numbers out into an array, and if it is 6.1 million, will the array be too large?
Is there a better plan to split this large array into several decimal groups? So the efficiency of the decimal group, and the efficiency of the whole large array, which is better?
php Send mail? Push?
is the iOS push
Then you can open a few PHP processes to send, a Send 1w bar.
Then you can open a few PHP processes to send, a Send 1w bar.
How to open the process? How to write?
Then you can open a few PHP processes to send, a Send 1w bar.
How to open the process? How to write?
Can take advantage of cronjob
Php-f send_apns.php 1 10000 #往数据库里1至10000的device_token推送消息
Php-f send_apns.php 10000 20000
Php-f send_apns.php 20000 30000
But think about it, I think you still do not slice the process, you still have to implement the function first, and then solve the problem
The efficiency of this situation is mainly due to the way you connect with the server, because it is the socket direct connection, non-HTTP (of course, HTTP also has keepalive), so you may be the segmentation process is slow, each process needs to reestablish the socket connection.
So, just do it, Sao year.
Then you can open a few PHP processes to send, a Send 1w bar.
How to open the process? How to write?
function is now testing 20,000, takes 45 minutes or so! It's too slow! And there will be more than 10 unsuccessful send! If this is a real hair 100,000, one day!!! So need to improve efficiency ah!
How exactly does this segmentation process work? How to add to the code in front of you?
If Linux is running under the PHP can run several sub-processes with pcntl_fork running under the look.
Cronjob also can, but need database to do some design, mean every time, check there is no message to send, and mass mail a reason
But I think 20,000 45 minutes is OK, 100,000 how to use 1 days. It depends on how fast your servers connect to the Apple API.
The problem of packet loss is a big problem, my side is not far to 20000 to send this scale, nor have I encountered
There's a problem here.
http://stackoverflow.com/questions/12708486/ Send-push-notification-to-multiple-devices-apns-response-is-negative-after-a-wh
is also the problem of packet loss, it seems that the Apple side think your socket connection is not active, will close the connection, so the code here is not a re-connected mechanism, this depends on your own to think about.
If Linux is running under the PHP can run several sub-processes with pcntl_fork running under the look.
Cronjob also can, but need database to do some design, mean every time, check there is no message to send, and mass mail a reason
But I think 20,000 45 minutes is OK, 100,000 how to use 1 days. It depends on how fast your servers connect to the Apple API.
The problem of packet loss is a big problem, my side is not far to 20000 to send this scale, nor have I encountered
There's a problem here.
http://stackoverflow.com/questions/12708486/ Send-push-notification-to-multiple-devices-apns-response-is-negative-after-a-wh
is also the problem of packet loss, it seems that the Apple side think your socket connection is not active, will close the connection, so the code here is not a re-connected mechanism, this depends on your own to think about.
foreach ((array) $regs as $reg) { $msg = chr (0). Pack (' n ', +). Pack (' h* ', $reg [' Devicetoken ']). Pack (' n ', strlen ($payload)) . $payload; Send it to the server $result = fwrite ($fp, $msg, strlen ($msg));
When I write this, the phenomenon of packet loss is very serious! Tens of thousands of devices can only send a successful hundreds of. Put $fp in the loop, drop packets or there, relatively few. 20,000 devices can have more than 10 devices unsuccessful!
May I ask how many devices you are sending?
You can try this.
while (true) { $j = count ($regs); for ($i =0; $i < $j;) { $msg = chr (0). Pack (' h* ', $reg [$i] [' Devicetoken ']). Pack (' n ', strlen ($pay Load)). $payload; Send it to the server $result = fwrite ($fp, $msg, strlen ($msg)); if (! $result) {//Send failed, socket re-connect $fp = stream_socket_client (' ssl://gateway.sandbox.push.apple.com:2195 ', $ Err, $ERRSTR, stream_client_connect| Stream_client_persistent, $ctx); } else { $i + +; } } break;}
Khan, can't edit the post is really troublesome.
while (true) { $j = count ($regs); for ($i =0; $i < $j;) { $msg = chr (0). Pack (' h* ', $reg [$i] [' Devicetoken ']). Pack (' n ', strlen ($pay Load)). $payload; Send it to the server $result = fwrite ($fp, $msg, strlen ($msg)); if (! $result) {//Send failed, socket re-connect $fp = stream_socket_client (' ssl://gateway.sandbox.push.apple.com:2195 ', $ Err, $ERRSTR, stream_client_connect| Stream_client_persistent, $ctx); } else { $i + +; } } Break }
Khan, can't edit the post is really troublesome.
while (true) { $j = count ($regs); for ($i =0; $i < $j;) { $msg = chr (0). Pack (' h* ', $reg [$i] [' Devicetoken ']). Pack (' n ', strlen ($pay Load)). $payload; Send it to the server $result = fwrite ($fp, $msg, strlen ($msg)); if (! $result) {//Send failed, socket re-connect $fp = stream_socket_client (' ssl://gateway.sandbox.push.apple.com:2195 ', $ Err, $ERRSTR, stream_client_connect| Stream_client_persistent, $ctx); } else { $i + +; } } Break }
if (! $result) {//Send failed, socket re-connect $fp = stream_socket_client (' ssl://gateway.sandbox.push.apple.com:2195 ', $ Err, $ERRSTR, stream_client_connect| Stream_client_persistent, $ctx); }
If so, is the message of success written?
Apnsphp-master
Search This bar, I wrote this, one push 10,000, crontab timed to send, as if not encountered any situation
I uploaded to the Linux server test, seemingly $ctx = Stream_context_create (); What's going on?
Khan, can't edit the post is really troublesome.
while (true) { $j = count ($regs); for ($i =0; $i < $j;) { $msg = chr (0). Pack (' h* ', $reg [$i] [' Devicetoken ']). Pack (' n ', strlen ($pay Load)). $payload; Send it to the server $result = fwrite ($fp, $msg, strlen ($msg)); if (! $result) {//Send failed, socket re-connect $fp = stream_socket_client (' ssl://gateway.sandbox.push.apple.com:2195 ', $ Err, $ERRSTR, stream_client_connect| Stream_client_persistent, $ctx); } else { $i + +; } } Break }
if (! $result) {//Send failed, socket re-connect $fp = stream_socket_client (' ssl://gateway.sandbox.push.apple.com:2195 ', $ Err, $ERRSTR, stream_client_connect| Stream_client_persistent, $ctx); }
If so, is the message of success written?
I uploaded to the Linux server test, seemingly $ctx = Stream_context_create (); What's going on?
$fp = stream_socket_client (' ssl://gateway.sandbox.push.apple.com:2195 ', $err, $errstr, Stream_client_connect | Stream_client_persistent, $ctx), if (! $fp) {fwrite ($fps, "Failed to connect: $err $errstr");} The operating result is: Failed to connect:0
Apnsphp-master
Search This bar, I wrote this, one push 10,000, crontab timed to send, as if not encountered any situation
How does a push 10,000 push? I push 100 is very slow, is there any good way?