gg-innovative/larafirebase

Laravel Firebase云消息传递。

1.0.3 2024-04-25 14:24 UTC

This package is auto-updated.

Last update: 2024-09-25 21:07:04 UTC


README

Total Downloads Latest Stable Version License

简介

Larafirebase 是一个通过Firebase在Laravel中发送推送通知的包。

Firebase云消息传递(FCM)是一个跨平台的消息解决方案,可以让您免费可靠地传递消息。

对于即时消息等用例,消息可以向客户端应用程序传输高达4KB的有效负载。

安装

按照以下步骤安装此包。

通过Composer安装

composer require gg-innovative/larafirebase

复制配置

运行以下命令以发布 larafirebase.php 配置文件

php artisan vendor:publish --provider="GGInnovative\Larafirebase\Providers\LarafirebaseServiceProvider"

根据需要配置larafirebase.php

打开您刚刚发布的 larafirebase.php 配置文件,并按需设置以下值

  • project_id:替换为您的实际Firebase项目ID。
  • firebase_credentials:这指的是您的Firebase项目的JSON凭证文件。请确保它指向项目中正确的位置。此JSON文件包含Firebase项目的认证信息,允许Laravel应用程序与Firebase服务交互。您可以在Firebase控制台中生成此JSON文件。一旦您有了它,请在此配置中指定其路径。

用法

按照以下步骤了解如何使用此包。

在您想要使用Larafirebase的任何类中的示例用法

use GGInnovative\Larafirebase\Facades\Larafirebase;

class MyController
{
    public function sendNotification()
    {
        return Larafirebase::withTitle('Hello World')
            ->withBody('I have something new to share with you!')
            ->withImage('https://firebase.google.com/images/social.png')
            ->withAdditionalData([
                'name' => 'wrench',
                'mass' => '1.3kg',
                'count' => '3'
            ])
            ->withToken('TOKEN_HERE') // You can use also withTopic
            ->sendNotification();
        
        // Or
        return Larafirebase::fromRaw([
            // https://firebase.google.com/docs/reference/fcm/rest/v1/projects.messages
            "name" => "string",
            "data" => [
                "string" => "string",
            ],
            "notification" => [
                "object" => "(Notification)"
            ],
            "android" => [
                "object" => "(AndroidConfig)"
            ],
            "webpush" => [
                "object" => "(WebpushConfig)",
            ],
            "apns" => [
                "object" => "(ApnsConfig)"
            ],
            "fcm_options" => [
                "object" => "(FcmOptions)"
            ],
            "token" => "string",
            "topic" => "string",
            "condition" => "string"
        ])->sendNotification();
    }
}

Notification 类中的示例用法

use Illuminate\Notifications\Notification;
use GGInnovative\Larafirebase\Messages\FirebaseMessage;

class SendBirthdayReminder extends Notification
{
    /**
     * Get the notification's delivery channels.
     */
    public function via($notifiable)
    {
        return ['firebase'];
    }

    /**
     * Get the firebase representation of the notification.
     */
    public function toFirebase($notifiable)
    {
        return (new FirebaseMessage)
            ->withTitle('Hey, ', $notifiable->first_name)
            ->withBody('Happy Birthday!')
            ->withToken('TOKEN_HERE')
            ->asNotification();
    }
}

提示

  • 您可以使用 larafirebase() 辅助函数代替Facade。