为 iOS 实现推送通知(服务器端)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7301215/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Implementing Push notifications for iOS (Server Side)
提问by Alex1987
We want to be able to push simple text messages to ALL our iphone users. For that we obviously need to create a server side code that stores the device tokens and pushes the messages whenever necessary. Is there any good example on doing this? (Talking about the server code)
我们希望能够向所有 iphone 用户推送简单的短信。为此,我们显然需要创建一个服务器端代码来存储设备令牌并在必要时推送消息。有没有这样做的好例子?(说到服务器代码)
Thanks
谢谢
回答by Frank
Have a look at easyAPNSif you want to host it yourself, or visit Urban Airshipif you are ok with a hosting service (they have an extensive set of documentation)
如果您想自己托管,请查看easyAPNS,如果您对托管服务没问题,请访问Urban Airship(他们有大量文档)
Another good site for info is Ray Wenderlich's site which hosts a 2 part tutorial:
另一个很好的信息站点是 Ray Wenderlich 的站点,该站点包含两部分教程:
回答by Abhijit
// Push Notification code for IPHONE in PHP
$deviceToken = $users_rows['gcm_regid'];
$passphrase = 'pass1234';
$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'DrinksterDevelopment.pem');
stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);
$fp = stream_socket_client(
'ssl://gateway.sandbox.push.apple.com:2195', $err,
$errstr, 120, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);
if (!$fp)
exit("Failed to connect: $err $errstr" . PHP_EOL);
echo 'Connected to APNS' . PHP_EOL;
$body['aps'] = array(
// 'alert' => $_GET["message"].'#'.$_GET["type"].'#'.$_GET["deal_id"],
'alert' => $_GET["message"],
'sound' => 'default'
);
$body['other'] = $_GET["type"].'#'.$_GET["deal_id"];
$payload = json_encode($body);
$msg = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . $payload;
$result_iphone = fwrite($fp, $msg, strlen($msg));
if (!$result_iphone)
$msg_iphone = 'Message not delivered' . PHP_EOL;
else
$msg_iphone = 'Message successfully delivered' . PHP_EOL;
mail('[email protected]', 'IOSPushMsgStatus', $msg_iphone);
fclose($fp);
} //if($users_rows['Platform'] == 'Web' OR $users_rows['Platform'] == 'Android')
回答by Asaf
I also recommend using an external service for that such as Urban Airship or PushApps. I more familiar with the last, and I can tell you that besides of "regular" push notifications messages, you also get messaging by segmentation, location or even scheduling notification. It may not seem like a critical features for you at first, but as your user base grow, you'll see how important those features are.
我还建议为此使用外部服务,例如 Urban Airship 或PushApps。我对最后一个比较熟悉,我可以告诉你,除了“常规”推送通知消息之外,你还可以通过分段、位置甚至调度通知来获取消息。乍一看,这对您来说似乎不是一个关键功能,但是随着您的用户群的增长,您会发现这些功能有多么重要。