syntech/syntechfcm

Laravel 的 FCM 包

1.2.0 2024-08-12 20:34 UTC

This package is auto-updated.

Last update: 2024-09-12 20:49:42 UTC


README

本指南提供了逐步说明,用于在 Laravel 应用程序中集成 Syntech\SyntechFcm 以发送 Firebase Cloud Messaging (FCM) 通知。

要求

  • PHP >= 7.4
  • Laravel >= 8.0
  • Composer

安装

  1. 通过 Composer 安装包:

    composer require syntech/syntechfcm
  2. 发布配置文件:

    php artisan vendor:publish --provider="Syntech\Syntechfcm\SyntechFcmServiceProvider"
  3. 配置您的 FCM 凭证:

    使用您的 FCM 项目凭证更新 config/syntechfcm.php 文件。

    return [
    'project_id'   => env('FCM_PROJECT_ID'),
    'client_email' => env('FCM_CLIENT_EMAIL'),
    'private_key'  => env('FCM_PRIVATE_KEY'),
    ];
  4. 将 FCM 凭证添加到您的 .env 文件:

    FCM_PROJECT_ID=your-project-id
    FCM_CLIENT_EMAIL=your-client-email
    FCM_PRIVATE_KEY=your-private-key

创建和发送通知

  1. 创建一个通知:

    php artisan make:notification YourNotification
  2. 实现通知类:

    <?php
    
    namespace App\Notifications;
    
    use Illuminate\Bus\Queueable;
    use Illuminate\Contracts\Queue\ShouldQueue;
    use Illuminate\Notifications\Notification;
    
    class YourNotification extends Notification
    {
        use Queueable;
    
        /**
         * Create a new notification instance.
         *
         * @return void
         */
        public function __construct()
        {
            //
        }
    
        /**
         * Get the notification's delivery channels.
         *
         * @param  mixed  $notifiable
         * @return array
         */
        public function via($notifiable)
        {
            return ['fcm'];
        }
    
        /**
         * Get the FCM representation of the notification.
         *
         * @param  mixed  $notifiable
         * @return array
         */
        public function toFcm($notifiable)
        {
            return [
                'to' => $notifiable->device_token,
                'notification' => [
                    'title'    => 'Notification Title',
                    'body'     => 'Notification Body',
                    'image'    => '', // Optional image URL
                ],
            ];
        }
    }
    <?php
    
    namespace App\Models;
    
    use Illuminate\Database\Eloquent\Model;
    use Illuminate\Notifications\Notifiable;
    
    class User extends Model
    {
        use Notifiable;
    
        /**
         * Route notifications for the FCM channel.
         *
         * @return string
         */
        public function routeNotificationForFcm()
        {
            return $this->device_token;
        }
    }
  3. 发送通知:

    在控制器或适当的位置使用通知

    use App\Notifications\YourNotification;
    use App\Models\User;
    
    // Assuming $user is an instance of the User model
    $user = User::find(1); // Find the user you want to notify
    $user->notify(new YourNotification($title, $body));

示例用法

  1. 存储 FCM 令牌:

    确保您有方法存储每个用户的 FCM 令牌,通常在您的 users 表中。

    Schema::table('users', function (Blueprint $table) {
        $table->string('device_token')->nullable();
    });
  2. 更新 FCM 令牌:

    当用户登录或注册时更新用户的 FCM 令牌。

    $user->update(['device_token' => $request->input('device_token')]);
  3. 发送测试通知:

    在控制器方法中

    public function sendTestNotification(User $user)
    {
        $user->notify(new YourNotification());
    }

许可

本项目为开源项目,可在MIT 许可证下使用。

贡献

欢迎贡献!请先阅读贡献指南

支持

如需支持,请提交问题或联系维护者。

示例代码图像

以下是您通知类中 toFcm 方法实现的示例

toFcm Method Example