简体   繁体   English

Amazon EC2无法在iOS上发送推送通知

[英]Amazon ec2 can't send push notification on ios

I am using below php code in Amazon ec2, php 7 我在Amazon EC2,PHP 7中使用以下php代码

<?php
    function sendApplePushNotificationMessage( $data, $message )
    {
        global $notification;
        $apns_settings = $notification['apns_user'];
        $ctx = stream_context_create();
        stream_context_set_option($ctx, 'ssl', 'local_cert', $apns_settings['pemFile']);
        $fp = stream_socket_client('ssl://gateway.'.(($apns_settings['environment'] == $err, $errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);

        if (!$fp)
        exit("Failed to connect amarnew: $err $errstr" . PHP_EOL);
        echo 'Connected to APNS' . PHP_EOL;
        $body['aps'] = array(
            'alert' => $message,
            'sound' => 'default'
        );
        $payload = json_encode($body+$extra_values);
        $msg = chr(0) . pack('n', 32) . pack('H*', $data['token']) . pack('n', strlen($payload)) . $payload;
        $result = fwrite($fp, $msg, strlen($msg));
        if (!$result)
            echo 'Message not delivered' . PHP_EOL;
        else
            echo 'Message successfully delivered '.$message. PHP_EOL;   
        // Close the connection to the server
        fclose($fp);
    }

when i call this function i got below success message. 当我调用此函数时,出现以下成功消息。

Connected to APNS
Message successfully delivered

When i try same pem file and device token to test online then it works properly but not work on Amazon ec2 . 当我尝试相同的pem文件和设备令牌进行在线测试时,它可以正常运行,但不能在Amazon ec2上运行

You are using APN service to send push notifications . 您正在使用APN服务发送推送通知。 It will access ports 2195 & 2196 to send push notifications using TCPSocket internally. 它将访问端口21952196以在内部使用TCPSocket发送推送通知。

Your local system will have access to all ports, then there will be no problem. 您的本地系统将可以访问所有端口,因此不会有问题。

EC2 instance in default doesn't have access to those ports. 默认情况下, EC2实例无权访问这些端口。 You can allow/open those ports in your ec2 instance security groups . 您可以在ec2实例security groups允许/打开这些端口。

Also allow port 443 . 还允许端口443

An alternative to writing your own function to cause an Apple Push Notification would be to use Amazon Simple Notification Service (SNS). 编写自己的导致Apple Push Notification的功能的替代方法是使用Amazon Simple Notification Service(SNS)。

Amazon SNS can push notifications to iOS, Android, Baidu, Windows mobile, Windows desktop and Mac desktop. Amazon SNS可以将通知推送到iOS,Android,百度,Windows Mobile,Windows桌面和Mac桌面。

See: Getting Started with Apple Push Notification Service 请参阅: Apple Push Notification Service入门

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM