【问题标题】:Amazon ec2 can't send push notification on iosAmazon ec2 无法在 ios 上发送推送通知
【发布时间】:2017-03-26 18:41:36
【问题描述】:

我在 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);
    }

当我调用这个函数时,我得到了下面的成功消息。

Connected to APNS
Message successfully delivered

当我尝试使用相同的 pem 文件和设备令牌进行在线测试时,它可以正常工作,但 无法在 Amazon ec2 上工作

【问题讨论】:

  • 你是说当你在自己的电脑上运行这段代码时,它可以成功运行,但它在 Amazon EC2 上运行不成功?什么是错误信息。您的 EC2 实例是否具有通过 IAM 角色分配的权限?
  • @JohnRotenstein 是的,当我在 Godaddy 专用 linux 服务器中运行此代码时,成功传递通知消息。但是当我在 EC2 中运行此代码时,我收到类似“连接到 APNS 消息已成功传递”的响应消息,但通知消息未传递到 ios 设备。
  • @JohnRotenstein 抱歉,您的意思是“EC2 实例具有通过 IAM 角色分配的权限?”
  • 在启动时将角色分配给 Amazon EC2 时,将向在该实例上运行的应用程序提供凭证。见:Using an IAM Role to Grant Permissions to Applications Running on Amazon EC2 Instances

标签: php amazon-web-services amazon-ec2 push-notification


【解决方案1】:

您正在使用APN 服务发送推送通知。它将访问端口21952196 以在内部使用TCPSocket 发送推送通知。

您的本地系统将可以访问所有端口,那么就没有问题了。

EC2 实例默认无权访问这些端口。您可以在 ec2 实例 security groups 中允许/打开这些端口。

还允许端口 443

【讨论】:

  • 是否在入站规则中?
  • @Maulik:是的,完全正确
  • 好吧,那好吧。
【解决方案2】:

编写您自己的函数来引发 Apple 推送通知的另一种方法是使用 Amazon Simple Notification Service (SNS)。

Amazon SNS 可以将通知推送到 iOS、Android、百度、Windows mobile、Windows 桌面和 Mac 桌面。

见:Getting Started with Apple Push Notification Service

【讨论】:

  • 你是对的,Amazon SNS 是替代方式,但我需要知道 EC2 服务器有什么问题或为什么此代码无法传递消息?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-15
相关资源
最近更新 更多