digimantra/digi-notification

一个用于发送 FCM 推送通知的 Laravel 扩展包。

v1.0.2 2024-09-25 11:51 UTC

This package is not auto-updated.

Last update: 2024-09-25 12:01:59 UTC


README

Packagist Version Packagist Downloads GitHub License

此包为 Laravel 应用程序提供了一种简单的方式来使用 Google 的 Firebase API 发送 Firebase 云消息 (FCM) 推送通知。

目录

需求

  • Laravel 8、9 或 10
  • PHP 8.0 或更高版本
  • Google API 客户端(通过 composer 自动安装)
  • 启用了云消息 API 的 Firebase 账户

安装

  1. 通过 Composer 安装

    composer require digimantra/digi-notification
  2. 发布配置

    php artisan vendor:publish --tag=config --provider="DigiNotification\FcmHelper\FcmServiceProvider"
  3. 设置环境变量

    将以下内容添加到您的 .env 文件中

    FIREBASE_PROJECT_ID=your-firebase-project-id
    FIREBASE_CREDENTIALS=path/to/your/credentials.json

    您可以从 Firebase 控制台 生成凭证文件。

使用

发送 FCM 通知

### Use Namespaces
use DigiNotification\FcmHelper\FcmHelper; 

### Assigning variables 
$tokens = ['device_token_1', 'device_token_2']; // Array of device tokens to which the notification will be sent.
$title = 'New Notification'; // The title of the notification.
$body = 'This is the body of the notification'; // The body content of the notification.
$data = ['key' => 'value']; // (Optional) Additional custom data.

### Example Send FCM notificaiton Dispatch
FcmHelper::sendFcmNotification($tokens, $title, $body, $data);  

配置

队列配置

Ensure your queue system is set up properly by adding the following to your `.env` file:

env

QUEUE_CONNECTION=database

然后,创建 jobs 表并运行迁移

php artisan queue:table
php artisan migrate

日志记录

要启用日志记录,将以下内容添加到您的 .env 文件中

FCM_LOGGING=true
Set to false to disable logging.

已知问题

Ensure FCM tokens are valid, as invalid tokens might cause delivery failures.
Invalid service account credentials will cause the package to fail to obtain an access token from Firebase.

许可

This package is open-sourced under the MIT license.

支持

For support or more details you can reach out at it@digimantra.com.