snappmarket/smnotif-php-bridge

用于使用 snappmarket 通知服务的客户端包。

3.1.2 2023-04-17 06:52 UTC

README

Total Downloads Latest Stable Version License License

SnappMarket 通知服务 PHP 桥接器

本包旨在使用 SnappMarket 通知服务。

要查看完整文档,请查看

  • 测试: 测试文档
  • 预发布: 预发布文档
  • 生产: 生产文档
    (注意:别忘了将 Swagger-Token 设置为请求头。Swagger-Token 的值是从 Snappmarket 通知服务获得的 API 密钥)

要求

  • PHP >= 7.2.0
  • JSON PHP 扩展

安装

在您的 composer.json 文件内要求该包。

$ composer require snappmarket/smnotif-php-bridge

发送 SMS 通知的基本用法

您可以在原始 PHP 文件、项目或 PHP 框架(如 Laravel 或 Symfony)中使用它。NotifierApi 类接受四个参数。

  • $api_key:从通知服务获取的 API 密钥。
  • $api_version:您正在使用的 API 版本,例如:1
  • $is_secure:如果为 true,则使用 'https' 调用服务,否则使用 'http'。
  • $app_env:您正在使用的应用程序环境(包括 NotifierApi::PRODUCTIONNotifierApi::STAGENotifierApi::TEST

1- 原始 PHP 文件

<?php
require __DIR__ . '/vendor/autoload.php';

try {
    $api_token = "Your API Token";
    $api_version = 1;
    $is_secure = true;
    $app_env = \Notifier\NotifierApi::PRODUCTION;
    $notifier = new \Notifier\NotifierApi($api_token,$api_version,$is_secure,$app_env);
    $sms_notifier = $notifier->setType(\Notifier\NotifierApi::SMS);
    $response = $sms_notifier->setBypassLimitControl(1) // to bypass time limit control (like activation codes)
        ->setExpireTime('1h 15m') // expires in 1 hour and 15 minutes
        ->setMode(\Notifier\NotifierApi::ASYNC_MODE) // send notification async or sync
        ->setPriority(\Notifier\NotifierApi::BLOCKER_PRIORITY) // priority : blocker, high, medium, low
        ->setProviderCode('10002') // get sms provider codes from notification service
        ->setSmsBodyStructure(\Notifier\NotifierApi::DYNAMIC_STRUCTURE) // static or dynamic?
        ->setSmsBody('Hello {{name}}. Your discount is {{discount}}!!') // sms body (you can also use sms templates)
        ->setReceivers([
            [
                'number' => "0939*******",
                'sms_template_data' => [
                    'name' => 'Alireza Jangi',
                    'discount' => 45
                ]
            ],
            [
                'number' => "0937*******",
                'sms_template_data' => [
                    'name' => 'Another Name',
                    'discount' => 77
                ]
            ]
        ])
        ->send();
} catch (Exception $e) {
    throw $e;
}
?>

2- 在 PHP 类内部

use Notifier\NotifierApi;

try {
    $api_token = "Your API Token";
    $api_version = 1;
    $is_secure = true;
    $app_env = NotifierApi::PRODUCTION;
    $notifier = new NotifierApi($api_token,$api_version,$is_secure,$app_env);
    $sms_notifier = $notifier->setType(\Notifier\NotifierApi::SMS);
    $response = $sms_notifier->setBypassLimitControl(1) // to bypass time limit control (like activation codes)
        ->setExpireTime('1h 15m') // expires in 1 hour and 15 minutes
        ->setMode(NotifierApi::ASYNC_MODE) // send notification async or sync
        ->setPriority(NotifierApi::BLOCKER_PRIORITY) // priority : blocker, high, medium, low
        ->setProviderCode('10002') // get sms provider codes from notification service
        ->setSmsBodyStructure(NotifierApi::DYNAMIC_STRUCTURE) // static or dynamic?
        ->setSmsBody('Hello {{name}}. Your discount is {{discount}}!!') // sms body (you can also use sms templates)
        ->setReceivers([
            [
                'number' => "0939*******",
                'sms_template_data' => [
                    'name' => 'Alireza Jangi',
                    'discount' => 45
                ]
            ],
            [
                'number' => "0937*******",
                'sms_template_data' => [
                    'name' => 'Another Name',
                    'discount' => 77
                ]
            ]
        ])
        ->send();
} catch (Exception $e) {
    throw $e;
}

发送 PUSH 通知的基本用法

您可以在原始 PHP 文件、项目或 PHP 框架(如 Laravel 或 Symfony)中使用它。NotifierApi 类接受四个参数。

  • $api_key:从通知服务获取的 API 密钥。
  • $api_version:您正在使用的 API 版本,例如:1
  • $is_secure:如果为 true,则使用 'https' 调用服务,否则使用 'http'。
  • $app_env:您正在使用的应用程序环境(包括 NotifierApi::PRODUCTIONNotifierApi::STAGENotifierApi::TEST
// Static Push Notification
try {
    $api_token = "5df0d20cd6b9c5df0d20cd6ba3";
    $api_version = 1;
    $is_secure = true;
    $app_env = NotifierApi::TEST;
    $notifier = new NotifierApi($api_token,$api_version,$is_secure,$app_env);
    $push_notifier = $notifier->setType(\Notifier\NotifierApi::PUSH);
    $response = $push_notifier->setBypassLimitControl(1) // to bypass time limit control (like activation codes)
        ->setExpireTime('10m') // expires in 10 minutes
        ->setMode(NotifierApi::ASYNC_MODE) // send notification async or sync
        ->setReceivers(["3502960","4727193"]) // Receivers for static push notification are user IDs
        ->setPriority(NotifierApi::BLOCKER_PRIORITY) // priority : blocker, high, medium, low
        ->setProviderCode('20001') // get push provider codes from notification service
        ->setBodyStructure(NotifierApi::STATIC_STRUCTURE) // you set it to static in this type
        ->setTitle('This is a test ') // push title
        ->setBody('This is a test message without any variables!!') // push body
        ->setMessagePageTitle("") // This is the message title you want to save in user phone
        ->setMessagePageBody("") // This is the message body you want to save in user phone
        ->setImage("https://m.snapp.market/logo.png") // set push notification image
        ->setBanner("https://m.snapp.market/logo.png") // set push notification banner
        ->setSound("default") // set push notification sound
        ->setModalText("Some text for modal") // if you set it a modal will be open in application
        ->setDeepLink("") // set Deep Link
        ->setWebView("") // set Web View
        ->setWebLink("") // set Web Link
        ->send();
    die($response);
} catch (Exception $e) {
    throw $e;
}
// Dynamic Push Notification
try {
    $api_token = "5df0d20cd6b9c5df0d20cd6ba3";
    $api_version = 1;
    $is_secure = true;
    $app_env = NotifierApi::TEST;
    $notifier = new NotifierApi($api_token,$api_version,$is_secure,$app_env);
    $push_notifier = $notifier->setType(\Notifier\NotifierApi::PUSH);
    $response = $push_notifier->setBypassLimitControl(1) // to bypass time limit control (like activation codes)
        ->setExpireTime('10m') // expires in 10 minutes
        ->setMode(NotifierApi::ASYNC_MODE) // send notification async or sync
        ->setReceivers([
                [
                    'user_id' => "********",
                    'push_template_data' => [
                        'first_name' => 'Alireza',
                        'last_name' => "jangi",
                        'code' => "3434"
                    ]
                ],
                [
                    'number' => "*******",
                    'push_template_data' => [
                        'first_name' => 'Another Name',
                        'last_name' =>  'Another Name',
                        'code' => '9833'
                    ]
                ]
        ]) // Receivers for static push notification are user IDs
        ->setPriority(NotifierApi::BLOCKER_PRIORITY) // priority : blocker, high, medium, low
        ->setProviderCode('20001') // get push provider codes from notification service
        ->setBodyStructure(NotifierApi::DYNAMIC_STRUCTURE) // you set it to static in this type
        ->setTitle('Hello {{first_name}} {{last_name}}') // push title
        ->setBody('Welcome! This is your code : {{code}} ') // push body
        ->setMessagePageTitle("") // This is the message title you want to save in user phone
        ->setMessagePageBody("") // This is the message body you want to save in user phone
        ->setImage("https://m.snapp.market/logo.png") // set push notification image
        ->setBanner("https://m.snapp.market/logo.png") // set push notification banner
        ->setSound("default") // set push notification sound
        ->setModalText("Some text for modal") // if you set it a modal will be open in application
        ->setDeepLink("") // set Deep Link
        ->setWebView("") // set Web View
        ->setWebLink("") // set Web Link
        ->send();
    die($response);
} catch (Exception $e) {
    throw $e;
}

示例