katsana/fcm-notification

Laravel 的 Firebase Cloud Messaging (FCM) 通知通道

v1.0.1 2020-04-03 06:00 UTC

This package is auto-updated.

Last update: 2024-08-29 05:08:47 UTC


README

此包使您能够通过使用 Firebase Cloud Messagingkreait/laravel-firebase,在 Laravel 6.0+ 中轻松发送通知。

Build Status Latest Stable Version Total Downloads Latest Unstable Version License Coverage Status

安装

FCM 通知可以通过 composer 安装

composer require "katsana/fcm-notification"

配置

这部分基于 Firebase for Laravel 配置

为了通过服务器 SDK 访问 Firebase 项目及其相关服务,必须对请求进行身份验证。对于服务器之间的通信,这通过服务帐户来完成。

该包使用自动发现来查找通过检查某些环境变量并查看谷歌的已知路径来验证对 Firebase API 请求所需的凭据。

如果您尚未生成服务帐户,可以按照官方文档页面上的说明进行操作,请参阅 https://firebase.google.com/docs/admin/setup#initialize_the_sdk

一旦您下载了服务帐户 JSON 文件,就可以使用它通过在 .env 文件中指定环境变量 FIREBASE_CREDENTIALS 来配置包。

FIREBASE_CREDENTIALS=/full/path/to/firebase_credentials.json
# or
FIREBASE_CREDENTIALS=relative/path/to/firebase_credentials.json

有关进一步配置,请参阅 config/firebase.php。您可以通过发布命令将其复制到本地配置目录以修改配置。

php artisan vendor:publish --provider="Kreait\Laravel\Firebase\ServiceProvider" --tag=config

用法

如果通知支持作为 FCM 发送,应在通知类中定义一个 toFcm 方法。此方法将接收一个 $notifiable 实体并应返回一个 NotificationChannels\Fcm\Message 实例

use NotificationChannels\Fcm\Message;

// ...

/**
 * Get the FCM representation of the notification.
 *
 * @param  mixed  $notifiable
 * @return \NotificationChannels\Fcm\Message
 */
public function toFcm($notifiable)
{
    return (new Message)
        ->notification('Your title', 'Your body');
}

路由 FCM 通知

在通过 fcm 通道发送通知时,通知系统将自动在实体上查找 routeNotificationForFcm 方法

<?php

namespace App;

use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    use Notifiable;

    /**
     * Route notifications for the FCM channel.
     *
     * @param  \Illuminate\Notifications\Notification  $notification
     * @return string
     */
    public function routeNotificationForFcm($notification)
    {
        return $this->deviceToken;
    }
}