mcfedr/awspushbundle

一套简化使用Aws发送推送通知的服务

安装次数: 339,054

依赖关系: 1

建议者: 0

安全性: 0

星标: 39

关注者: 6

分支: 24

公开问题: 3

类型:symfony-bundle

6.19.0 2023-05-11 17:52 UTC

README

这是一个方便的bundle,用于注册设备并使用亚马逊SNS服务向它们推送消息。

Latest Stable Version License Build Status

安装

Composer

php composer.phar require mcfedr/awspushbundle

AppKernel

在您的AppKernel中包含此bundle

public function registerBundles()
{
    $bundles = array(
        ...
        new Mcfedr\AwsPushBundle\McfedrAwsPushBundle()

配置

在您的配置中放入类似以下内容。平台部分中的arn应该是SNS中预先配置的应用arn。

mcfedr_aws_push:
    platforms:
        ios: 'arn:aws:sns:....'
        android: 'arn:aws:sns:....'
    topic_arn: 'arn:aws:sns:...'
    pushPlatforms: [apns, fcm]
    aws:
        credentials: 
            key: 'my key'
            secret: 'my secret'
        region: 'my region'

如果您希望Aws SDK间接获取凭据,则可以跳过credentials,无论是从环境还是ec2角色

如果您未设置pushPlatforms设置,则Android消息将以与GCM兼容的格式发送,即不会设置notification字段。这是为了与该bundle的向后兼容性。

使用方法

基本上看看ApiController是如何工作的

  1. 注册设备令牌

     $arn = $this->get('mcfedr_aws_push.devices')->registerDevice($token, $platform)
    
  2. 向单个设备发送消息

     $this->get('mcfedr_aws_push.messages')->send($message, $arn)
    
  3. 向所有设备发送消息

     $this->get('mcfedr_aws_push.messages')->broadcast($message)
    

另一种使用方法,使用主题向大量设备发送消息

  1. 注册设备令牌

     $arn = $this->get('mcfedr_aws_push.devices')->registerDevice($token, $platform)
    
  2. 在主题上注册设备

     $this->get('mcfedr_aws_push.topics')->registerDeviceOnTopic($arn, $topicArn)
    
  3. 发送消息

     $this->get('mcfedr_aws_push.topics')->broadcast($message, $topicArn)
    

如果您稍后向配置中添加了topic_name,则可以运行mcfedr:aws:subscribe命令以将现有设备添加到主题。

通知中的文本

对于GCM和ADM,与苹果推送相比,没有用于文本数据的'标准'键,所以这个bundle在名为message的键中发送文本。

如果发送本地化文本,则键是

  • message-loc-key
  • message-loc-args

ADM上的“复杂”数据

ADM只允许推送数据中的字符串作为值。此bundle允许您发送“复杂”的值,并将自动将这些值json编码为ADM。当它这样做时,键会添加_json,以便在应用端易于处理。

示例

发送

$message = new Message();
$message->setCustom(['simple' => 'Hello', 'complicated' => ['inner' => 'value']]);

ADM接收到的数据

{"data": {"simple": "Hello", "complicated_json": "{\"inner\":\"value\"}"}}

要处理这些数据,您应该检测以_json结尾的键并解码值

这也适用于message-loc-args,它们将以message-loc-args_json的形式通过ADM始终到来

命令

有一些命令可以帮助管理设备

  1. mcfedr:aws:enable

    这将重新启用所有设备

  2. mcfedr:aws:remove

    这将删除任何已禁用的设备。定期执行此操作以删除旧设备是个好主意

  3. mcfedr:aws:subscribe

    这将使所有设备订阅主题,这在引入主题时非常有用

Api控制器

可选。

包含了一个控制器,它使得基本使用该bundle变得非常简单。您可能需要或不需要使用它,您可能发现它作为示例最有用。

使用控制器时,您必须添加一些额外的依赖项

  • sensio/framework-extra-bundle
  • symfony/validator
  • symfony/serializer
  • symfony/property-info
  • symfony/security-bundle
  • symfony/expression-language

它们还需要在框架配置中启用

framework:
    validation: { enable_annotations: true }
    serializer:
        enabled: true
    property_info:
        enabled: true

在您的routing.yaml中添加路由

mcfedr_aws_push:
    resource: "@McfedrAwsPushBundle/Controller/"
    type:     annotation
    prefix:   /

使用方法

控制器提供了两个URL,两者都期望一个JSON POST正文

  1. 第一个是用来注册设备的

     POST /devices
     {
         "device": {
             "deviceId": "a push token",
             "platform": "the platform name in your config file"
         }
     }
    
  2. 第二个是用来发送广播消息的。如果您使用一个主题对所有设备,则不要发送平台参数。

     POST /broadcast
     {
         "broadcast": {
             "platform": "ios"
             "message": {
                 "text": "The plain text message to send",
                 "badge": 1
             }
         }
     }
    

更多信息