meddev / laravel-push-notification
Laravel 包,用于跨所有平台(apns, fcm)发送推送通知
This package is not auto-updated.
Last update: 2024-09-28 19:58:15 UTC
README
## 安装 将以下行添加到您的 composer.json 中的 "require" 部分
"meddev/laravel-push-notification": "1.0.0.x-dev"
使用以下命令更新 composer
"composer update"
### 提供器和外观 与所有提供器一样,将以下行添加到您的 config/app.php 中
'providers' => [ ... MedDev\PushNotification\PushNotificationProvider::class, ],
'aliases' => [ ... 'PushNotificationFacade' => MedDev\PushNotification\Facades\PushNotificationFacade::class, ],
### 配置 最后,您需要为此包生成一个配置文件。运行以下 composer 命令
php artisan vendor:publish --provider="MedDev\PushNotification\PushNotificationProvider"
此命令将在您的 config 目录中生成 pushnotification.php 文件。
return [ "aps" => [ /* * A valid PEM certificate generated from Apple Push Service certificate */ "certificate" => storage_path('app')."/aps.pem", /* * Password used to generate a certificate */ "passPhrase" => "", /* * Server used to send push notifications */ "server" => "ssl://gateway.push.apple.com:2195", //"server" => "https://api.development.push.apple.com", /* * Set to TRUE if HTTP/2 Is Enabled for Your SSL application */ "useApi" => false ], "fcm" => [ /* * Google FCM api server key * You can retrieve your key in Firebase Console */ "apiKey" => "", /* * Server used to send push notifications */ "server" => "https://fcm.googleapis.com/fcm/send" ] ];
请记住添加您的 FCM API 密钥和 PEM 证书路径。
## 令牌 您应该有一个模型来将设备信息存储到您的数据库中。为了使您的模型可以直接从 PushNotification 包中使用,您只需添加 use DeviceTrait
use MedDev\PushNotification\DeviceTrait; class YourDevicesTable extends Model { use DeviceTrait; }
为了与 PushNotification 包自动工作,您的表需要两个列,一个包含平台名称,另一个包含设备令牌。
默认情况下,这两个列的名称被认为是 "platform" 和 "device_token"
+--------------+
| Field |
+--------------+
| id |
| platform | enum (android, ios, web)
| device_token |
| created_at |
| updated_at |
+--------------+
您可以根据需要设计您的表,但只有这两个字段是必须的,以便与 PushNotification 包一起工作。
## 负载数据 您只需创建一个实现 MedDev\PushNotification\Contracts\Payload 的类,并用您的负载数据覆盖 apsPayload 和 fcmPayload 属性。
namespace App\Payload; use App\User; use MedDev\PushNotification\Contracts\Payload; class MessagePayload extends Payload { /** * Generate Notification Payload * * @param User $user * @return void */ public function __construct(User user) { //IOS payload format $this->apsPayload = [ "alert" => [ "title" => "Someone has sent you a message", "body" => "This is the notification body text", ], ]; //Android notification types can be 'notification' and 'data' $this->fcmPayloadType = "notification"; //Android payload format $this->fcmPayload = [ "title" => "Someone has sent you a message", "body" => "This is the notification body text", ]; } }
您可以为每个要发送给用户的事件或消息创建负载数据集合。
## 发送 现在您可以从数据库中获取设备令牌列表,并且您有一个特定事件的负载数据。要向设备列表发送负载数据,您可以使用从 Payload 类继承的 send 方法。
### 定期发送
//Create payload $payload = new MessagePayload(User::findOrFail(1)); //Retrieve devices list with your own criteria $tokens = YourDevicesTable::all(); //send directly $payload->send($tokens);
### 通过队列 包使用队列来原生发送通知,以在后台执行发送任务。您需要在 config/queue.php 中设置您的队列提供程序,并且您的负载数据将在队列中发送。
您可以使用 "send" 方法的第二个参数在特定队列中安排作业。
//push in queue $payload->send($payload, $tokens, "your-queue-name");