定制 paxha/laravel-fcm 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

paxha/laravel-fcm

Composer 安装命令:

composer require paxha/laravel-fcm

包简介

This package provides (Laravel Notification) channels for sending notifications via FCM (Firebase Cloud Messaging) using HTTP v1 API.

README 文档

README

Latest Stable Version Total Downloads Latest Unstable Version License PHP Version Require

Introduction

This package provides (Laravel Notification) channels for sending notifications via FCM (Firebase Cloud Messaging) using HTTP v1 API. For more information about the Firebase Cloud Messaging HTTP v1 API, please refer to the official documentation: https://firebase.google.com/docs/reference/fcm/rest/v1/projects.messages

Installation

To get started, you need to install the package via Composer:

composer require paxha/laravel-fcm

Optional: Once the package is installed, you can publish the configuration file using the following command:

php artisan vendor:publish --tag=fcm-config

This will create a new fcm.php file in your config directory, where you can configure your FCM settings. If you don't publish it, it will create a default channel named fcm with the default configuration.

Configuration

You can configure your FCM settings in the fcm.php file. Here's an example of the default configuration:

'fcm' => [ // this fcm key is the channel name you can create multiple channels over here...
    'project' => env('GOOGLE_PROJECT'),
    'service_account' => env('GOOGLE_SERVICE_ACCOUNT'),
],

You can also set your FCM settings in your .env file:

Please put your service account file at config folder and set the filename with path in the .env file.

You can create a service account file from the Google Cloud Console. For more information, please refer to the official: https://cloud.google.com/iam/docs/service-accounts-create

GOOGLE_PROJECT=your-project-id
GOOGLE_SERVICE_ACCOUNT=firebase-certificates/service-account.json

Usage

To send a notification, you can use the channel provided by this package. Here's an example of how to send a notification using the fcm channel:

Before sending the notification, you need to use our HasPushToken trait in your notifiable model.

class YourModel extends Model
{
    use \LaravelFCM\Traits\HasPushToken;
    
    // if your model has a push token column name other than `push_token` then you can define it like this
    public function routeNotificationForFCM()
    {
        return $this->your_custom_column; // column name where you stored the push token
    }
}

Create a new notification using the following command:

php artisan make:notification NewMessage
<?php

namespace App\Notifications;

use Illuminate\Bus\Queueable;use Illuminate\Contracts\Queue\ShouldQueue;use Illuminate\Notifications\Notification;

class NewMessage extends Notification implements ShouldQueue
{
    use Queueable;

    // take params according to your requiremnt.
    public function __construct(
        public ?string $title = null,
        public ?string $body = null,
        public $data = null,
    ) {
    }

    public function via()
    {
        return ['fcm'];
    }

    // optional: you can use according to your requirement.
    public function toFCM()
    {
        return [
            'title' => $this->title,
            'body' => $this->body,
        ];
    }

    // optional: you can use according to your requirement.
    public function toAPS()
    {
        return [
            //
        ];
    }
    
    // optional: you can use according to your requirement.
    public function toAndroid()
    {
        return [
            //
        ];
    }

    // optional: you can use according to your requirement.
    public function toWeb()
    {
        return [
            //
        ];
    }

    // optional: you can use according to your requirement.
    public function toData() {
        return $this->data;
    }
}

Post Sending Notification

After sending the notification, it will call an event, and you can listen to the event to get the response of the notification. And you can also do some other stuff after sending the notification.

php artisan make:listener PostNotificationListener

In your EventServiceProvider:

protected $listen = [
    \LaravelFCM\Events\FCMNotificationSent::class => [
        PostNotificationListener::class,
    ],
];

In your PostNotificationListener:

public function handle(object $event): void
{
    // you can the notifiable instance
    $notifiable = $event->notifiable;
    // you can get the notification instance
    $notification = $event->notification;
    // you can get the message instance. This is the message that was sent to FCM
    $message = $event->message;
     // you can get the response of the notification
    $response = $event->response;

    // you can also do some other stuff after sending the notification
}

Cleaning up unused services OPTIONAL

There are 200+ Google API services. The chances are good that you will not want them all. In order to avoid shipping these dependencies with your code, you can run the Google\Task\Composer::cleanup task and specify the services you want to keep in composer.json:

{
    "scripts": {
        "post-autoload-dump": [
            .
            .
            .
            "Google\\Task\\Composer::cleanup"
        ]
    },
    "extra": {
        .
        .
        "google/apiclient-services": [
            "FirebaseCloudMessaging"
        ]
    }
}

This example will remove all services other than "FirebaseCloudMessaging" when composer update or a fresh composer install is run.

IMPORTANT: If you add any services back in composer.json, you will need to remove the vendor/google/apiclient-services directory explicitly for the change you made to have effect:

rm -r vendor/google/apiclient-services
composer update

paxha/laravel-fcm 适用场景与选型建议

paxha/laravel-fcm 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2.15k 次下载、GitHub Stars 达 10, 最近一次更新时间为 2024 年 02 月 27 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 paxha/laravel-fcm 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 paxha/laravel-fcm 我们能提供哪些服务?
定制开发 / 二次开发

基于 paxha/laravel-fcm 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 2.15k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 10
  • 点击次数: 2
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 10
  • Watchers: 1
  • Forks: 4
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-02-27