MrDanshur/awspushbundle

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

安装次数: 3,103

依赖项: 0

建议者: 0

安全: 0

星标: 0

关注者: 0

分支: 24

类型:symfony-bundle

1.0.1 2023-03-30 20:12 UTC

This package is auto-updated.

Last update: 2024-09-30 01:41:18 UTC


README

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

Latest Stable Version License Build Status

安装

Composer

php composer.phar require mcfedr/awspushbundle

AppKernel

在您的AppKernel中包含此包

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字段。这是为了保持此包的向后兼容性。

使用方法

基本上查看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,由于苹果推送中存在“标准”键,因此没有用于文本数据的“标准”键,因此此包在名为message的键中发送文本。

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

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

ADM上的“复杂”数据

ADM仅允许推送数据中的字符串作为值。此包允许您发送“复杂”值,并将自动将这些值进行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 Controller

可选。

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

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

  • 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
             }
         }
     }
    

更多信息