MrDanshur / awspushbundle
一套简化使用Aws发送推送通知的服务
Requires
- php: >=7.4
- ext-json: *
- aws/aws-sdk-php: ^3.0
- symfony/framework-bundle: ^4.4|^5.0|^6.0
- symfony/polyfill-mbstring: ^1.1
Requires (Dev)
- fakerphp/faker: ^1.10
- friendsofphp/php-cs-fixer: ^3.3
- phpunit/phpunit: ^9.5
- sensio/framework-extra-bundle: ^4.4|^5.0|^6.0
- symfony/browser-kit: ^4.4|^5.0|^6.0
- symfony/console: ^4.4|^5.0|^6.0
- symfony/expression-language: ^4.4|^5.0|^6.0
- symfony/monolog-bundle: ^3.0
- symfony/phpunit-bridge: ^4.4|^5.0|^6.0
- symfony/property-info: ^4.4|^5.0|^6.0
- symfony/security-bundle: ^4.4|^5.0|^6.0
- symfony/serializer: ^4.4|^5.0|^6.0
- symfony/validator: ^4.4|^5.0|^6.0
- symfony/yaml: ^4.4|^5.0|^6.0
Suggests
- ext-mbstring: Its polyfilled, but better to use the extension
- sensio/framework-extra-bundle: Required if you want to use the included ApiController
- symfony/expression-language: Required if you want to use the included ApiController
- symfony/property-info: Required if you want to use the included ApiController
- symfony/security-bundle: Required if you want to use the included ApiController
- symfony/serializer: Required if you want to use the included ApiController
- symfony/validator: Required if you want to use the included ApiController
This package is auto-updated.
Last update: 2024-09-30 01:41:18 UTC
README
一个方便的包,用于注册设备并使用亚马逊SNS服务向它们推送消息。
安装
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是如何执行的
-
注册设备令牌
$arn = $this->get('mcfedr_aws_push.devices')->registerDevice($token, $platform)
-
向单个设备发送消息
$this->get('mcfedr_aws_push.messages')->send($message, $arn)
-
向所有设备发送消息
$this->get('mcfedr_aws_push.messages')->broadcast($message)
替代用法,使用主题向大量设备发送消息
-
注册设备令牌
$arn = $this->get('mcfedr_aws_push.devices')->registerDevice($token, $platform)
-
在主题上注册设备
$this->get('mcfedr_aws_push.topics')->registerDeviceOnTopic($arn, $topicArn)
-
发送消息
$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发送
命令
有一些命令可以帮助管理设备
-
mcfedr:aws:enable
这将重新启用所有设备
-
mcfedr:aws:remove
这将删除任何已禁用的设备。定期这样做以删除旧设备是一个好主意
-
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正文
-
第一个是注册设备的方式
POST /devices { "device": { "deviceId": "a push token", "platform": "the platform name in your config file" } }
-
第二种方式是发送广播消息。如果您为所有设备使用主题,则不要发送平台参数。
POST /broadcast { "broadcast": { "platform": "ios" "message": { "text": "The plain text message to send", "badge": 1 } } }