nexmo/laravel-notification
Composer 安装命令:
composer require nexmo/laravel-notification
包简介
Send SMS, WhatsApp, Viber and Facebook Messenger messages via Nexmo
README 文档
README
Although Nexmo is available in Laravel as the default SMS provider, the next generation of Nexmo APIs will offer more communication channels. This package adds the ability to send notifications to WhatsApp, Facebook Messenger and Viber via Nexmo.
Note that the Messages API that this package uses has not yet had its stable release so both the API and this library are subject to change
Usage
To use this package, run composer require nexmo/laravel-notification. Once it completes, you can implement the following methods on your notification:
toNexmoWhatsApptoNexmoFacebooktoNexmoViberServiceMessagetoNexmoSms
See examples/Notification/MerryChristmas.php for a complete example.
To send a notification, specify the channel you'd like to use:
// To a user $user->notify(new \App\Notifications\MerryChristmas()); // To any person Notification::route( 'nexmo-whatsapp', 'YOUR_NUMBER' )->notify(new \App\Notifications\MerryChristmas());
The available channels are:
nexmo-smsnexmo-whatsappnexmo-facebooknexmo-viber_service_msg
As each notification receives a $notifiable (usually a user) it can decide how best to route the information. In this case, it checks the via_whatsapp property on the user and sends via WhatsApp if it's true. Otherwise it falls back to email
public function via($notifiable)
{
return $notifiable->via_whatsapp ? ['nexmo-whatsapp'] : ['mail'];
}
Message Types
Nexmo supports multiple message types, depending on the channel that you're sending to. The Text type is the safest if you want to deliver to all channels:
public function toNexmoWhatsApp($notifiable)
{
return (new \Nexmo\Notifications\Message\Text)
->content('This is a message being sent to WhatsApp');
}
Caveats
For some channels you need to send a templated message before you can send a free text message due to spam control rules. Here's an example of how to use a preapproved template intended for two-factor authentication purposes:
public function toNexmoWhatsApp($notifiable)
{
return (new \Nexmo\Notifications\Message\Template)
->name("whatsapp:hsm:technology:nexmo:verify")
->parameters([
["default" => "Your Brand"],
["default" => "64873"],
["default" => "10"],
]);
}
If the recipient replies to your message, you can send them Text type messages without any issues
Configuration
Authentication
This notifications package is built on top of nexmo/laravel and uses the Nexmo client from there.
For this to work, you need to set your application ID and path to your private key in the .env file:
NEXMO_APPLICATION_ID=my_application_id
NEXMO_PRIVATE_KEY=./private.key
Setting the from address
You can set a from address via the .env file. This package will look for provider specific entries before falling back to NEXMO_FROM.
NEXMO_FROM_SMS=""
NEXMO_FROM_WHATSAPP=""
NEXMO_FROM_MESSENGER=""
NEXMO_FROM_VIBER_SERVICE_MSG=""
NEXMO_FROM="" # This is the default if any of the above aren't set
Alternatively, you can set a from address for a single notification by calling the ->from() method on a message:
public function toNexmoViberServiceMessage($notifiable) { return (new Text)->content('Merry Christmas Viber!')->from("YOUR_ID"); }
nexmo/laravel-notification 适用场景与选型建议
nexmo/laravel-notification 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2.75k 次下载、GitHub Stars 达 51, 最近一次更新时间为 2018 年 11 月 26 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 nexmo/laravel-notification 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 nexmo/laravel-notification 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 2.75k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 51
- 点击次数: 22
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2018-11-26