onsetsoftware/sns-push

适用于亚马逊SNS的跨平台API包装器

v1.2.1 2023-04-20 14:55 UTC

This package is auto-updated.

Last update: 2024-09-20 18:09:06 UTC


README

此包提供了使用亚马逊(AWS)SNS API发送移动推送通知的辅助方法。

Packagist Build Status Packagist

SNS Push是一个简单的SNS SDK包装器,包含一系列方法,帮助与AWS SNS API交互。它可以与Laravel直接工作,也可以作为独立的PHP包使用。

先决条件

安装

您需要使用Composer将SNS Push安装到您的项目中

composer require onsetsoftware/sns-push

其他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]
<?php

use SNSPush\SNSPush;

$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' => [ // application endpoints - Required
        'ios' => '<application-endpoint-arn>',
        'android' => '<application-endpoint-arn>'
    ]
]);

Laravel服务提供者

如果您是Laravel用户,可以利用包含的服务提供者。只需在您的config/app.php中添加SNSPushServiceProvider

<?php
[
    //...
    'providers' => [
        /*
         * Package Service Providers...
         */
        SNSPush\SNSPushServiceProvider::class,
    ]
];

config/services.php添加'sns'配置密钥

<?php
[
    //...
    '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>'
        ]
    ]
];

将设备添加到应用程序

通过将设备令牌和应用密钥传递给addDevice(),将设备添加到平台应用程序(ios/android)。

<?php
/**
 * @param string $token the raw device token
 * @param string $platform ( ios | android )  
 *                         
 * @return ARN the ARN endpoint for the device
 */
$sns->addDevice('<device-token>', '<platform-id>');

从应用程序中移除设备

通过传递Endpoint ARN给removeDevice(),从AWS SNS中移除设备。

<?php

$sns->removeDevice('<endpoint-arn>');

订阅设备到主题

通过将Endpoint Arn和Topic Arn传递给subscribeDeviceToTopic(),订阅设备到主题。

<?php
/**
 * @return SubscriptionARN
 */
$sns->subscribeDeviceToTopic('<device-endpoint-arn>', '<topic-arn>');

从主题中移除设备

通过传递Subscription Arn给removeDeviceFromTopic(),从主题中移除设备。

<?php

$sns->removeDeviceFromTopic('<subscription-arn>');

发送推送通知

SNS Push支持向主题端点或直接向端点 ARN(设备)发送通知。

消息

消息必须实现SNSPush\Messages\MessageInterface。有一些实用类可以正确格式化推送通知,以适应各种端点类型。

<?php

use SNSPush\Messages\IOsMessage;

$message = new IOsMessage();

$message->setTitle('Message Title')
        ->setBody('Message body')
        ->setBadge(5)
        ->setSound('sound.caf')
        ->setPayload(
          [
              'custom-key' => 'value',
          ]
      );

Phonegap插件推送

此包包含两个类,帮助格式化用于Phonegap插件推送Cordova包的消息。

PhoneGapPluginPushIOSMessage::class;
PhoneGapPluginPushAndroidMessage::class;

有关完整API,请查阅每种消息类型的源代码

发送到设备

只需传递一个实现SNSPush\Messages\MessageInterface的对象,以及端点 ARN。端点平台必须与消息类型匹配。

<?php

$sns->sendPushNotificationToDevice(
    '<endpoint-arn>',
    $message
);

发送到主题

首先,通过传递需要解决的端点消息对象的数组来形成您的SNSPush\Messages\TopicMessage。然后将TopicMessage传递给sendPushNotificationToTopic方法。

<?php

$iosMessage = new IOsMessage();
$androidMessage = new AndroidMessage();

$message = new TopicMessage([$iosMessage, $androidMessage]);

/**
 * @param TopicARN $arm
 * @param TopicMessage $message 
 */
$sns->send->sendPushNotificationToTopic(
    '<topic-arn>',
    $message
);

感谢

此包基于ReduGroup的工作。

许可

MIT许可 © On Set Software Ltd