veskodigital/laravel-fcm-channel

使用Laravel从Firebase云消息传递发送通知。

dev-master 2024-04-20 02:49 UTC

This package is auto-updated.

Last update: 2024-09-20 03:41:54 UTC


README

Laravel FCM Channel

使用Laravel FCM Channel轻松管理FCM通知。

内容

此包旨在使发送FCM消息更容易。

还有一个Flutter ,您可以使用它来节省移动开发的时间。

概述

  • 将多个(FCM)设备添加到Laravel中的模型
  • 添加新设备到模型的API路由
  • 使用Laravel Notification中的新"fcm_channel"发送FCM通知
  • Flutter移动包,帮助您加快通知开发

安装

首先,通过composer安装包

composer require veskodigital/laravel-fcm-channel

该包将自动注册自身。

配置

运行install命令。

php artisan laravelfcm:install

这将添加一个(laravelfcm.php)配置文件

服务提供者到您的app.php: App\Providers\FcmAppServiceProvider::class

然后,询问您是否要运行迁移。

以下是它将迁移的表

  • fcm_user_devices
  • fcm_user_devices_api_requests

将您的FCM服务器令牌添加到您的.env文件。

LARAVEL_FCM_SERVER_KEY="MyFCMServerKey"

您可以在Firebase项目设置>云消息传递中找到您的Fcm服务器密钥。

您可以在config/laravelfcm.php文件中完全配置此包(此文件应在您运行php artisan laravelfcm:install之后添加)。

配置您的模型

HasFCMDevices特性添加到您的User模型。

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use VeskoDigital\LaravelFCM\Models\Traits\HasFCMDevices; // Use HasFCMDevices trait
use Laravel\Sanctum\HasApiTokens;

class User extends Authenticatable
{
    use HasApiTokens, HasFactory, HasFCMDevices; // Add it to your model
    
    ...
}

此包使用laravel/sanctum作为您模型的默认中间件。

但是,如果您想使用不同的中间件进行身份验证,您可以更新config/laravelfcm.php文件中的"middleware"键。

该中间件用于通过此包创建的API端点通过API端点对用户进行身份验证。

API端点

此包向您的路由器添加API端点,以允许您的应用程序存储设备。

Postman集合示例

存储设备

默认端点: /api/fcm/device

方法: PUT

授权: "Bearer {{Sanctum Token}}"

添加此头部键: X-DMeta

{
    "uuid": "12992", // required, a uuid which should be from the device. The value must be persistented on the device.
    "model": "iphone", // optional
    "version":" 12", // optional
    "display_name": "Maes iPhone", // optional
    "platform": "IOS" // optional
}

有效负载正文

{
    "is_active": 1, // optional, use this key to define if a device active or not
    "push_token": "kjnsdmnsdc0sdco23" // optional, when you have an FCM token for the device, use this key in the payload
}

这将为用户添加一个新的FCM设备。如果您在有效负载中提供push_token,则用户将能够接收推送通知。

发送通知

要使用FCMChannel发送通知,首先在您的Laravel项目中创建一个通知。

php artisan make:notification ParcelDispatchedNotification

创建通知后,将fcm_channel添加到下面的数组中。

    /**
     * Get the notification's delivery channels.
     *
     * @param  mixed  $notifiable
     * @return array
     */
    public function via($notifiable)
    {
        return [
            'mail',
            'fcm_channel', // add this
        ];
    }

然后,将以下代码片段添加到您的通知类中。

    /**
     * Get the array representation of the notification.
     *
     * @param  mixed  $notifiable
     * @return array
     */
    public function toFcm($notifiable)
    {
        return [
            'title' => config('app.name'), // Laravel App Name
            'body' => $title, // Body of the notification
            'priority' => 'high', // Priority
        ];
    }

尝试发送通知

$user->notify(new ParcelDispatchedNotification($order));

这将向用户的设备发送通知。

控制是否发送FCM通知

在某些情况下,您可能只想根据条件通知用户。

在您的User模型类中添加以下代码片段。

<?php
...
class User {

    use HasFCMDevices;

    /**
     * Determines if the devices can be notified.
     *
     * @return bool
     */
    public function canSendNotification($notification) : bool
    {
        // $notification - Will return the type of Notification you are trying to send.
        // E.g. first send a notification which is using the `fcm_channel`
        // $user->notify(new ParcelDispatchedNotification($order));
        //
        // The canSendNotification method will be called before dispatching the fcm notifications and
        // perform a check in this method. If you return True, it will send. If you return False, it will not send.
        //
        // You can check the type of notification that is trying to send from the $notification variable.
        // Using the above example. $notification = 'App\Notifications\ParcelDispatchedNotification'.

        if ($notification  == 'App\Notifications\ParcelDispatchedNotification') {
            return ($this->receives_notificiations == true); // example condition
        }
    	return true;
    }
}
...

class User extends Authenticatable
{   
    ...

    /**
     * Determines if the devices can be notified.
     *
     * @return bool
     */
    public function canSendNotification($notification) : bool
    {
    	return true;
    }
}

通知对象

以下是您可以分配给FCMNotification的属性。

$notification = new FCMNotification();

$notification->setTitle('My Application');
$notification->setBody('Hello World');
$notification->setAndroidChannelId('1');
$notification->setBadge(1);
$notification->setClickAction('NOTIFICATION_CLICK');
$notification->setSound('default');
$notification->setIcon(''); // android only, set the name of your drawable resource as string
$notification->setTag(''); // Identifier used to replace existing notifications in the notification drawer.
$notification->setContentAvailable(''); // On Apple platforms, use this field to represent content-available in the APNs payload.

关系

当您的模型使用HasFCMDevices特性时,您可以调用以下方法。

<?php

$user = User::first();

$user->fcmDevices // Returns all the FCM Devices that the user owns

// send notification
$fcmDevice = $user->fcmDevices->first(); 
$notification = new FCMNotification(...);
$fcmDevice->send($notification);

Flutter 插件

需要向Flutter应用发送通知?

请查看该项目的官方仓库这里

变更日志

有关最近更改的详细信息,请参阅变更日志

测试

$ composer test

安全

如果您发现任何与安全相关的问题,请发送电子邮件至hello@veskodigital.com,而不是使用问题跟踪器。

贡献

有关详细信息,请参阅贡献指南

致谢

许可

MIT许可证(MIT)。有关更多信息,请参阅许可证文件