简体   繁体   中英

Amazon ec2 can't send push notification on ios

I am using below php code in Amazon ec2, php 7

<?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 .

You are using APN service to send push notifications . It will access ports 2195 & 2196 to send push notifications using TCPSocket internally.

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. You can allow/open those ports in your ec2 instance security groups .

Also allow port 443 .

An alternative to writing your own function to cause an Apple Push Notification would be to use Amazon Simple Notification Service (SNS).

Amazon SNS can push notifications to iOS, Android, Baidu, Windows mobile, Windows desktop and Mac desktop.

See: Getting Started with Apple Push Notification Service

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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