mcfedr / awspushbundle
一套简化使用Aws发送推送通知的服务
Requires
- php: >=7.4
- ext-json: *
- aws/aws-sdk-php: ^3.0
- symfony/framework-bundle: ^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: ^5.0|^6.0
- symfony/browser-kit: ^5.0|^6.0
- symfony/console: ^5.0|^6.0
- symfony/expression-language: ^5.0|^6.0
- symfony/monolog-bundle: ^3.0
- symfony/phpunit-bridge: ^5.0|^6.0
- symfony/property-info: ^5.0|^6.0
- symfony/security-bundle: ^5.0|^6.0
- symfony/serializer: ^5.0|^6.0
- symfony/validator: ^5.0|^6.0
- symfony/yaml: ^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
- dev-master
- 6.19.0
- 6.18.0
- 6.17.2
- 6.17.1
- 6.17.0
- 6.16.0
- 6.15.1
- 6.15.0
- 6.14.0
- 6.13.0
- 6.12.0
- 6.11.0
- 6.10.0
- 6.9.1
- 6.9.0
- 6.8.3
- 6.8.2
- 6.8.1
- 6.8.0
- 6.7.0
- 6.6.1
- 6.6.0
- 6.5.2
- 6.5.1
- 6.5.0
- 6.4.0
- 6.3.0
- 6.2.1
- 6.2.0
- 6.1.4
- 6.1.3
- 6.1.2
- 6.1.1
- 6.1.0
- 6.0.2
- 6.0.1
- 6.0.0
- v5.x-dev
- 5.1.3
- 5.1.2
- 5.1.1
- 5.1.0
- 5.0.1
- 5.0.0
- 4.1.1
- 4.1.0
- 4.0.7
- 4.0.6
- 4.0.5
- 4.0.4
- 4.0.3
- 4.0.2
- 4.0.1
- 4.0.0
- 3.0.6
- 3.0.5
- 3.0.4
- 3.0.3
- 3.0.2
- 3.0.1
- 3.0.0
- 2.4.8
- 2.4.7
- 2.4.6
- 2.4.5
- 2.4.4
- 2.4.3
- 2.4.2
- 2.4.1
- 2.4.0
- 2.3.1
- 2.3.0
- 2.2.1
- 2.2.0
- 2.1.8
- 2.1.7
- 2.1.6
- 2.1.5
- 2.1.4
- 2.1.3
- 2.1.2
- 2.1.1
- 2.1.0
- 2.0.0
- 1.0.0
- 0.0.2
- 0.0.1
This package is auto-updated.
Last update: 2024-08-30 01:09:04 UTC
README
这是一个方便的bundle,用于注册设备并使用亚马逊SNS服务向它们推送消息。
安装
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是如何工作的
-
注册设备令牌
$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,与苹果推送相比,没有用于文本数据的'标准'键,所以这个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始终到来
命令
有一些命令可以帮助管理设备
-
mcfedr:aws:enable
这将重新启用所有设备
-
mcfedr:aws:remove
这将删除任何已禁用的设备。定期执行此操作以删除旧设备是个好主意
-
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正文
-
第一个是用来注册设备的
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 } } }