notific / notific-php-sdk
一个用于轻松使用 Notific API 的 SDK
Requires
- php: ^7.0
- guzzlehttp/guzzle: ^6.3
Requires (Dev)
- phpunit/phpunit: ^6.0
This package is auto-updated.
Last update: 2024-09-08 07:46:40 UTC
README
一个用于轻松使用 Notific API 的 SDK
此 SDK 允许您对 Notific 进行 API 调用。API 文档 https://notific.io/api
用法
要在项目中安装 SDK,您需要通过 composer 需求包
$ composer require notific/notific-php-sdk
创建 SDK 实例
$notific = new Notific\PhpSdk\Notific(APP_ID, API_TOKEN);
如果您还没有 App ID,请在 https://app.notific.io/signup 注册
公开通知
您可以使用 publicNotifications() 方法获取公开通知实例的数组。
$notific->publicNotifications();
您可以选择向方法提供参数,这些参数将被转换为查询参数。有关过滤和排序选项的更多信息,请参阅 API 文档
$notific->publicNotifications([ 'page' => 2, 'limit' => 25, 'sort' => '-created_at' ]);
创建新的公开通知。
$notific->createPublicNotification([ 'title' => 'Welcome!' 'body' => 'This is the body of the notification...' ]);
检索公开通知实例。
$notification = $notific->publicNotification($id);
更新通知。
$notification->update([ 'title' => 'Boom!' 'body' => 'This is the updated body of the notification.' ]);
删除通知。
$notification->delete();
收件人
您可以使用 recipients() 方法获取收件人实例的数组。
$notific->recipients();
您可以选择向方法提供参数,这些参数将被转换为查询参数。有关过滤和排序选项的更多信息,请参阅 API 文档
创建收件人。
$notific->createRecipient([ 'id' => '123', // Unique identifier for the user. You can use integers, hashes or what ever suites you best. 'name' => 'Kung Fury', 'email' => 'kung.fury@email.com' 'phone' => '+358 40 123 456' // You can add meta information to recipient as key => value pairs. ]);
检索收件人实例。
$recipient = $notific->recipient($id);
更新收件人。
$recipient = $recipient->update([ 'name' => 'Fury' ]);
向收件人添加标签。
$recipient = $recipient->tag([ 'Foo', 'Bar', 'Baz' ]);
从收件人删除标签。
$recipient = $recipient->removeTags([ 'Bar' ]);
从收件人删除所有标签。
$recipient = $recipient->removeAllTags();
模板
由于私有通知是不可变和无名的,因此模板可编辑且易于按名称检索。
您可以使用 templates() 方法获取模板实例的数组。
$notific->templates();
创建模板。
$notific->createTemplate([ 'title' => 'Lorem ipsum dolor sit amet.', 'body' => 'Lorem ipsum dolor sit amet, consectetur adipiscing...', 'name' => 'lorem-ipsum' ]);
使用名称参数检索模板。
$template = $notific->template($name);
私有通知
您可以使用 privateNotifications() 方法获取私有通知实例的数组。
$notific->privateNotifications();
您可以选择向方法提供参数,这些参数将被转换为查询参数。有关过滤和排序选项的更多信息,请参阅 API 文档
$notific->privateNotifications([ 'filter[type]' => '1,2', 'sort' => '-created_at' ]);
创建新的私有通知。
$notific->createPrivateNotification([ 'title' => 'Welcome!' 'body' => 'This is the body of the private notification...' ]);
检索私有通知实例。
$notification = $notific->privateNotification($id);
更新私有通知。
$notification->update([ 'title' => 'Boom!' 'body' => 'This is the updated body of the private notification.' ]);
发送私有通知
您可以使用私有通知或模板向收件人发送私有通知。我们建议您使用模板,因为它们易于引用和管理,并且是可编辑的。
使用通知 ID 和收件人 ID:s 发送私有通知。收件人可以是字符串、列表或 ID:s 的数组。
$data = $notific->template($name)->recipients($recipients)->send()
$data = $notific->privateNotification($notificationId)->recipients($recipients)->send()
您可以使用两个交付渠道发送私有通知:广播(默认)和电子邮件。电子邮件渠道需要有效的电子邮件设置。
$data = $notific->template($name)->recipients($recipients)->channels('broadcast', 'email')->send()
$data = $notific->privateNotification($notificationId)->recipients($recipients)->channels('broadcast')->send()
如果您正在标记收件人,则可以使用标签发送通知。标签可以是字符串、列表或 ID:s 的数组。收件人必须拥有所有给定的标签。
$data = $notific->template($name)->tags($tags)->send()
$data = $notific->privateNotification($notificationId)->tags($tags)->send()
要向所有收件人发送通知,请使用预定义的标签 all。
$data = $notific->template($name)->tags('all')->send()
$data = $notific->privateNotification($notificationId)->tags('all')->send()
使用私有通知实例发送私有通知的另一种方法。
$data = $notific->template($id)->sendTo($recipients);
$data = $notific->privateNotification($notificationId)->sendTo($recipients);
使用收件人实例发送私有通知。
$data = $notific->recipient($id)->sendNotification($notificationId);
提示:您可以测试私有通知并将其发送给自己。如果您已登录到 notific.io 控制台,您将立即收到通知。
$data = $notific->template($name)->test();
$data = $notific->privateNotification($id)->test();
分页与限制
默认情况下,响应数据将分页显示为15项。可以通过查询参数进行页面浏览和限制每页的项目数量。例如 ?page=2&limit=25。有关分页和限制的更多信息,请参阅 API 文档。
安全
如果您发现任何与安全相关的问题,请发送电子邮件至 kalle@klopal.com,而不是使用问题跟踪器。
致谢
此包使用了来自 Oh Dear SDK 包 的代码,并深受其启发,该包又受到了 Forge SDK 包 的启发,后者由 Mohammed Said 开发。
许可证
MIT 许可证(MIT)。有关更多信息,请参阅 许可证文件。
