redu / sns-push
Amazon SNS PHP SDK的API包装器
Requires
- php: >=7.0.0
- aws/aws-php-sns-message-validator: ^1.4
- aws/aws-sdk-php: ^3.36
This package is not auto-updated.
Last update: 2024-09-29 02:29:04 UTC
README
本软件包提供了一组辅助方法,用于与Amazon (AWS) SNS API交互。
SNS Push是一个简单的SNS SDK包装器,包含一组用于与AWS SNS API交互的方法。它可以与Laravel直接使用,也可以作为一个独立的PHP软件包使用。
先决条件
安装
您需要使用Composer将SNS Push安装到项目中
composer require redu/sns-push
配置(Laravel)
现在您需要在config/app.php
中包含SNSPushServiceProvider
'providers' => [ /* * Package Service Providers... */ SNSPush\SNSPushServiceProvider::class, ]
向config/services.php
添加'sns'配置键
'sns' => [ 'account_id' => env('SNS_ACCOUNT_ID', ''), 'access_key' => env('SNS_ACCESS_KEY', ''), 'secret_key' => env('SNS_SECRET_KEY', ''), 'scheme' => env('SNS_SCHEME', 'https'), 'region' => env('SNS_REGION', 'eu-west-1'), 'platform_applications' => [ 'ios' => '<application-endpoint-arn>', 'android' => '<application-endpoint-arn>' ] ],
其他PHP框架(非Laravel)设置
如果尚未加载,您应该包含Composer的autoload.php
文件
require __DIR__ . '/vendor/autoload.php';
使用以下必需配置值实例化SNSPush类
- account_id
- access_key
- secret_key
- platform_applications
也可配置
- region [默认: eu-west-1]
- api_version [默认: 2010-03-31]
- scheme [默认: https]
$sns = new SNSPush([ 'account_id' => '<aws-account-id>', // Required 'access_key' => '<aws-iam-user-access-key>', // Required 'secret_key' => '<aws-iam-user-secret-key>', // Required 'scheme' => 'http', // Defaults to https 'platform_applications' => [ // Required 'ios' => '...', 'android' => '...' ] ]);
将设备添加到应用程序
通过将设备令牌和应用程序密钥传递给addDevice()
,将设备添加到平台应用程序(iOS/Android)。
$sns->addDevice('<device-token>, 'ios');
从应用程序中删除设备
通过传递端点ARN给removeDevice()
,从AWS SNS中删除设备。
$sns->removeDevice('<endpoint-arn>');
将设备订阅到主题
通过传递端点ARN和主题ARN给subscribeDeviceToTopic()
,将设备订阅到主题。
$sns->subscribeDeviceToTopic('<endpoint-arn>', '<topic-arn>');
从主题中删除设备
通过传递订阅ARN给removeDeviceFromTopic()
,从主题中删除设备。
$sns->removeDeviceFromTopic('<subscription-arn>');
发送推送通知
SNS Push支持向主题端点或直接向端点ARN(设备)发送通知。
发送到设备
$message = 'Push notification message.'; $sns->sendPushNotificationToDevice( '<endpoint-arn>', $message );
您还可以可选地与消息一起发送自定义负载。
$sns->sendPushNotificationToDevice('<endpoint-arn>', $message, [ 'payload' => [ 'id' => 9 ] ]);
消息结构以JSON格式发送,并将根据设备正确格式化。如果您正在向多个平台发送并/或发送自定义负载,则这是必需的。
如果您只向单个平台发送简单的消息并希望节省字节,可以将消息结构设置为字符串。
$sns->sendPushNotificationToDevice('<endpoint-arn>', $message, [ 'message_structure' => 'string' ]);
发送到主题
$message = 'Push notification message.'; $sns->send->sendPushNotificationToTopic( '<topic-arn>', $message );
同样,您可以设置消息结构和负载。
待办事项
- 支持更多端点
- 测试,测试,测试...(仍处于早期开发阶段,请谨慎使用)
许可证
MIT许可证 © Redu Group Ltd