novu / novu-laravel
Novu for Laravel
1.3.0
2024-04-09 04:20 UTC
Requires
- php: ^7.3|^8.0|^8.1
- ext-json: *
- illuminate/support: ~6|~7|~8|^9.0|^10.0|^11.0
- unicodeveloper/novu: ^1.2
Requires (Dev)
- mockery/mockery: ^1.1
- orchestra/testbench: ~4|~5|~6|^7.0
- pestphp/pest: ^1.20.0
- sempro/phpunit-pretty-print: ^1.0
This package is auto-updated.
Last update: 2024-09-09 05:19:59 UTC
README
Novu for Laravel
一个将Novu轻松集成到您的Laravel应用程序中的包。发送多渠道(短信、电子邮件、聊天、推送、应用内)通知,并无缝地将通知中心嵌入到您的应用中。
内容
安装
PHP 7.3+ 和 Composer 是必需的。支持 Laravel 7, 8, 9 和 10。
要获取Novu Laravel的最新版本,只需要求它
composer require novu/novu-laravel
配置
您可以使用此命令发布配置文件
php artisan vendor:publish --tag="novu-laravel-config"
将名为 novu.php
的配置文件(包含一些合理的默认值)放置在您的 config
目录中
<?php return [ /* |-------------------------------------------------------------------------- | Novu API Key |-------------------------------------------------------------------------- | | The Novu API key give you access to Novu's API. The "api_key" is | typically used to make a request to the API. | */ 'api_key' => env('NOVU_API_KEY'), /* |-------------------------------------------------------------------------- | The Novu API Base URL. |-------------------------------------------------------------------------- | | The Novu API Base URL can be a self-hosted novu api or Novu's web cloud API | typically used to make a request to Novu's service. | */ 'api_url' => env('NOVU_BASE_API_URL', 'https://api.novu.co/v1/'), ];
API密钥
打开您的 .env
文件并添加您的API密钥。
NOVU_API_KEY=xxxxxxxxxxxxx
注意:您需要从您的 Novu仪表板 获取这些凭据。
用法
应用内通知中心
Novu依赖于自己的JavaScript库来启动和显示应用内通知中心。您可以通过在应用程序布局的结束 </head>
标签之前放置 @novuJS 指令来加载JavaScript库
<head> ... @novuJS </head>
在主体中使用 x-notification-center
Blade通知中心组件,如下所示
<!DOCTYPE html> <html> <head> ... @novuJS </head> <body> <x-notification-center app-id="<YOUR_APP_IDENTIFIER>" subscriber-id="<SUBSCRIBER_ID>" style="display: inline-flex"> </x-notification-center> </body> </html>
注意:如果您使用此包进行Laravel API后端,则不需要使用此功能!使用针对 Vue、React、Angular 和 Vanilla JS 可用的应用内通知中心JavaScript库。
重要信息
您可以使用 Novu
Facade 或 novu()
辅助函数来调用此包中的方法。
事件
触发 事件 - 向订阅者发送通知
use Novu\Laravel\Facades\Novu; $response = Novu::triggerEvent([ 'name' => '<WORKFLOW_TRIGGER_ID_FROM_DASHBOARD>', 'payload' => ['customVariables' => 'Hello'], 'to' => [ 'subscriberId' => '<SUBSCRIBER_ID_FROM_DASHBOARD>', 'phone' => '07983887777' ] ])->toArray(); // or you can use the novu() helper function like so: novu()->triggerEvent([ 'name' => '<WORKFLOW_TRIGGER_ID_FROM_DASHBOARD>', 'payload' => ['customVariables' => 'Hello'], 'to' => [ 'subscriberId' => '<SUBSCRIBER_ID_FROM_DASHBOARD>', 'phone' => '07983887777' ] ])->toArray();
批量触发 事件
use Novu\Laravel\Facades\Novu; $response = Novu::bulkTriggerEvent([ [ 'name' => '<WORKFLOW_TRIGGER_ID_FROM_DASHBOARD>', 'to' => '<SUBSCRIBER_ID_FROM_DASHBOARD>', 'payload' => ['customVariables' => 'Hello'] ], [ 'name' => '<WORKFLOW_TRIGGER_ID_FROM_DASHBOARD>', 'to' => '<SUBSCRIBER_ID_FROM_DASHBOARD>', 'payload' => ['customVariables' => 'World'] ], [ 'name' => '<WORKFLOW_TRIGGER_ID_FROM_DASHBOARD>', 'to' => '<SUBSCRIBER_ID_FROM_DASHBOARD>', 'payload' => ['customVariables' => 'Again'] ] ])->toArray(); // or you can use the novu() helper function like so: novu()->bulkTriggerEvent([ [ 'name' => '<WORKFLOW_TRIGGER_ID_FROM_DASHBOARD>', 'to' => '<SUBSCRIBER_ID_FROM_DASHBOARD>', 'payload' => ['customVariables' => 'Hello'] ], [ 'name' => '<WORKFLOW_TRIGGER_ID_FROM_DASHBOARD>', 'to' => '<SUBSCRIBER_ID_FROM_DASHBOARD>', 'payload' => ['customVariables' => 'World'] ], [ 'name' => '<WORKFLOW_TRIGGER_ID_FROM_DASHBOARD>', 'to' => '<SUBSCRIBER_ID_FROM_DASHBOARD>', 'payload' => ['customVariables' => 'Again'] ] ])->toArray();
触发 事件 - 向主题发送通知
use Novu\Laravel\Facades\Novu; $response = Novu::triggerEvent([ 'name' => '<event_name>', 'payload' => ['customVariables' => 'Hello'], 'to' => [ [ 'type' => 'Topic', 'topicKey' => $topicKey ], [ 'type' => 'Topic', 'topicKey' => $topicSecondKey ] ] ])->toArray();
向所有现有订阅者 广播事件
use Novu\Laravel\Facades\Novu; $response = Novu::broadcastEvent([ 'name' => '<WORKFLOW_TRIGGER_ID_FROM_DASHBOARD>', 'payload' => ['customVariables' => 'Hello'], 'transactionId' => '<REPLACE_WITH_TRANSACTION_ID>' ])->toArray();
取消 触发的事件。在事件触发期间使用先前生成的transactionId,此操作将取消任何活动或挂起的流程
use Novu\Laravel\Facades\Novu; $response = Novu::cancelEvent($transactionId);
订阅者
use Novu\Laravel\Facades\Novu; // Get list of subscribers $subscribers = Novu::getSubscriberList(); // Create subscriber & get the details of the recently created subscriber returned. $subscriber = Novu::createSubscriber([ 'subscriberId' => '<YOUR_SYSTEM_USER_ID>', 'email' => '<insert-email>', // optional 'firstName' => '<insert-firstname>', // optional 'lastName' => '<insert-lastname>', // optional 'phone' => '<insert-phone>', //optional 'avatar' => '<insert-avatar>', // optional ])->toArray(); // Bulk create subscribers $response = Novu::bulkCreateSubscribers([ [ 'subscriberId' => '<SUBSCRIBER_IDENTIFIER>', 'email' => '<insert-email>', // optional 'firstName' => '<insert-firstname>', // optional 'lastName' => '<insert-lastname>', // optional 'avatar' => '<insert-avatar>', // optional ], [ 'subscriberId' => '<SUBSCRIBER_IDENTIFIER>', 'email' => '<insert-email>', // optional 'firstName' => '<insert-firstname>', // optional 'lastName' => '<insert-lastname>', // optional 'avatar' => '<insert-avatar>', // optional ], ]); // Get subscriber $subscriber = Novu::getSubscriber($subscriberId)->toArray(); // Update subscriber $subscriber = Novu::updateSubscriber($subscriberId, [ 'email' => '<insert-email>', // optional 'firstName' => '<insert-firstname>', // optional 'lastName' => '<insert-lastname>', // optional 'phone' => '<insert-phone>', //optional 'avatar' => '<insert-avatar>', // optional ])->toArray(); // Delete subscriber Novu::deleteSubscriber($subscriberId); // Update subscriber credentials $response = Novu::updateSubscriberCredentials($subscriberId, [ 'providerId' => '<insert-providerId>', 'credentials' => '<insert-credentials>' ])->toArray(); // Update subscriber online status $isOnlineStatus = true; // or false $response = Novu::updateSubscriberOnlineStatus($subscriberId, $isOnlineStatus)->toArray(); // Get subscriber preferences $preferences = Novu::getSubscriberPreferences($subscriberId)->toArray(); // Update subscriber preference Novu::updateSubscriberPreference($subscriberId, $templateId, [ 'channel' => 'insert-channel', 'enabled' => 'insert-boolean-value' // optional ]); // Get a notification feed for a particular subscriber $feed = Novu::getNotificationFeedForSubscriber($subscriberId); // Get the unseen notification count for subscribers feed $count = Novu::getUnseenNotificationCountForSubscriber($subscriberId); // Mark a subscriber feed message as seen Novu::markSubscriberFeedMessageAsSeen($subscriberId, $messageId, []); // Mark message action as seen Novu::markSubscriberMessageActionAsSeen($subscriberId, $messageId, $type, []);
主题
use Novu\Laravel\Facades\Novu; // Create a Topic Novu::createTopic([ 'key' => 'frontend-users', 'name' => 'All frontend users' ]); // Fetch all topics Novu::getTopics(); // Get a topic Novu::topic($topicKey); // Add subscribers to a topic $subscribers = [ '63e271488c028c44fd3a64e7', '3445' ]; Novu::topic($topicKey)->addSubscribers($subscribers); // Remove subscribers from a topic $subscribers = [ '63e271488c028c44fd3a64e7', '3445' ]; Novu::topic($topicKey)->removeSubscribers($subscribers); // Rename a topic Novu::topic($topicKey)->rename($topicName);
活动
use Novu\Laravel\Facades\Novu; // Get activity feed $feed = Novu::getActivityFeed(); // Get activity statistics $stats = Novu::getActivityStatistics()->toArray(); // Get activity graph statistics $graphStats = Novu::getActivityGraphStatistics()->toArray();
集成
use Novu\Laravel\Facades\Novu; // Get integrations Novu::getIntegrations()->toArray(); // Create integration Novu::createIntegration([ 'providerId' => '<insert->provider->id>', 'channel' => '<insert->channel>', 'credentials' => [ // insert all the fields ], 'active' => true, 'check' => true ])->toArray(); // Get active integrations Novu::getActiveIntegrations()->toArray(); // Get webhook support status for provider Novu::getWebhookSupportStatusForProvider($providerId)->toArray(); // Update integration Novu::updateIntegration($integrationId, [ 'active' => true, 'credentials' => [ // insert all the fields ], 'check' => true ])->toArray(); // Delete integration Novu::deleteIntegration($integrationId);
布局
use Novu\Laravel\Facades\Novu; // filter layouts Novu::filterLayouts(['pageSize' => 1])->toArray(); // Create layout Novu::createLayout([ 'name' => '<insert-name-of-layout>', 'identifier' => '<insert-identifier>', 'content' => '<insert-html-content>', ])->toArray(); // Get a layout Novu::getLayout('<insert-layout-id>')->toArray(); // Set Layout as default Novu::setLayoutAsDefault('<insert-layout-id>'); // Update layout Novu::updateLayout('<insert-layout-id>', [ 'name' => '<insert-name-of-layout>', 'identifier' => '<insert-identifier>', 'content' => '<insert-html-content>', ])->toArray(); // Delete layout Novu::deleteLayout('<insert-layout-id>');
通知
use Novu\Laravel\Facades\Novu; // Get all notifications Novu::getNotifications()->toArray(); // Get all notifications with query parameters $queryParams = [ 'page' => 3 ]; Novu::getNotifications($queryParams)->toArray(); // Get one notification Novu::getNotification($notificationId)->toArray(); // Get notification stats Novu::getNotificationStats()->toArray(); // Get Notification graph stats Novu::getNotificationGraphStats()->toArray(); // Get Notification graph stats with query parameters $queryParams = [ 'days' => 5 ]; Novu::getNotificationGraphStats($queryParams)->toArray();
通知模板
use Novu\Laravel\Facades\Novu; // Get notification templates Novu::getNotificationTemplates()->toArray(); // Create notification template Novu::createNotificationTemplate([ "name" => "name", "notificationGroupId" => "notificationGroupId", "tags" => ["tags"], "description" => "description", "steps" => ["steps"], "active" => true, "draft" => true, "critical" => true, "preferenceSettings" => preferenceSettings ])->toArray(); // Update notification template Novu::updateNotificationTemplate($templateId, [ "name" => "name", "tags" => ["tags"], "description" => "description", "identifier" => "identifier", "steps" => ["steps"], "notificationGroupId" => "notificationGroupId", "active" => true, "critical" => true, "preferenceSettings" => preferenceSettings ])->toArray(); // Delete notification template Novu::deleteNotificationTemplate($templateId); // Get notification template Novu::getANotificationTemplate($templateId); // Update notification template status Novu::updateNotificationTemplateStatus($templateId, [ 'active' => true ])
通知组
use Novu\Laravel\Facades\Novu; // Create Notification group Novu::createNotificationGroup([ 'name' => '<insert-name>' ]); // Get Notification groups Novu::getNotificationGroups()->toArray();
变更
use Novu\Laravel\Facades\Novu; // Get changes Novu::getChanges(); // Get changes count Novu::getChangesCount()->toArray(); // Apply changes Novu::applyBulkChanges([ 'changeIds' = [ '<insert-all-the-change-ids>' ] ])->toArray(); // Apply change Novu::applyChange($changeId, []);
环境
use Novu\Laravel\Facades\Novu; // Get current environment Novu::getCurrentEnvironment()->toArray(); // Create environment Novu::createEnvironment([ 'name' => '<insert-name>', 'parentId' => '<insert-parent-id>' // optional ])->toArray(); // Get environments Novu::getEnvironments()->toArray(); // Update environment by id Novu::updateEnvironment($envId, [ "name" => "name", "identifier" => "identifier", "parentId" => "parentId" ]); // Get API KEYS Novu::getEnvironmentsAPIKeys()->toArray(); // Regenerate API KEYS $key = Novu::regenerateEnvironmentsAPIKeys()->toArray(); // Update Widget Settings Novu::updateWidgetSettings([ 'notificationCenterEncryption' => true ]);
源
use Novu\Laravel\Facades\Novu; // Create feed Novu::createFeed([ 'name' => '<insert-name-for-feed>' ]); // Get feeds Novu::getFeeds()->toArray(); // Delete feed Novu::deleteFeed();
消息
use Novu\Laravel\Facades\Novu; // Get messages Novu::getMessages([ 'page' => 1, 'channel' => ['<insert-channel>'], ]); // Delete message Novu::deleteMessage();
执行详情
use Novu\Laravel\Facades\Novu; // Get execution details Novu::getExecutionDetails([ 'notificationId' => '<insert-notification-id>', 'subscriberId' => '<insert-subscriber-id>' ])->toArray();
租户
use Novu\Laravel\Facades\Novu; // Create tenant Novu::createTenant([ 'identifier' => '<identifier>', 'name' => '<name>', ]); // Get tenants Novu::getTenants()->toArray();
验证MX记录设置以启用入站解析功能
use Novu\Laravel\Facades\Novu; // Validate MX Record for Inbound Parse Novu::validateMXRecordForInboundParse()->toArray();
许可
许可协议为 MIT许可。