laradevs/

notifications-fcm

一个用于在 Laravel 应用中发送 Firebase 通知的包

v1.0.1 2020-03-21 20:14 UTC

This package is auto-updated.

Last update: 2024-09-22 06:58:25 UTC


README

FCM 通知

此包是从 kawnkoding/laravel-fcm 分支出来的,并针对您的工作进行了调整和单元测试,以支持队列和额外的参数

安装

您可以通过 composer 拉取此包

$ composer require laradevs/notifications-fcm

接下来,您必须注册服务提供者

// config/app.php

'Providers' => [
   // ...
   LaraDevs\Fcm\FcmServiceProvider::class,
]

如果您想使用门面(facade),也必须安装它

// config/app.php

'aliases' => [
    // ...
    'Fcm' => LaraDevs\Fcm\FcmFacade::class,
];

接下来,您必须发布配置文件以定义您的 FCM 服务器密钥

php artisan vendor:publish --provider="LaraDevs\Fcm\FcmServiceProvider"

这是发布文件的內容

return [

    /**
     * Set your FCM Server Key
     * Change to yours
     */

    'server_key' => env('FCM_SERVER_KEY', ''),
    'server_endpoint' => env('FCM_SERVER_ENDPOINT', 'https://fcm.googleapis.com/fcm/send'),
    'server_icon_app'=> env('FCM_ICON_APP')

];
importScripts('https://www.gstatic.com/firebasejs/3.9.0/firebase-app.js');
importScripts('https://www.gstatic.com/firebasejs/3.9.0/firebase-messaging.js');


// Initialize Firebase
var config = {
    apiKey: "YOUR-API-KEY",
    authDomain: "YOUR-DOMAIN",
    databaseURL: "YOUR-DATABASE-URL",
    projectId: "YOUR-PROJECT-ID",
    storageBucket: "YOUR-STORAGE-BUCKET",
    messagingSenderId: "YOUR-MESSAGING-SENDER",
    appId: "YOUR-APP-ID",
    measurementId: "YOUR-MEASURE-ID"
};

firebase.initializeApp(config);
const messaging = firebase.messaging();

.env 文件中设置您的 FCM 服务器密钥

APP_NAME="Laravel"
# ...
FCM_SERVER_KEY=putYourKeyHere
FCM_SERVER_ENDPOINT=putServerEndpointHere
FCM_ICON_APP=putIconNotification

与作业使用

如果您想简单地使用 FCM,可以通过以下作业完成

FcmSendJob::dispatch('Hello World',['RECIPIENTS_IDs']);

如果您想使用队列来使用 FCM,请按以下步骤操作

FcmSendJob::dispatch('Hello World',['RECIPIENTS_IDs'])->onqueue('NAME_QUEUE');

无作业使用

如果您只想使用通知参数发送 FCM,以下是一个仅使用数据参数发送 FCM 的示例

fcm()
    ->to($recipients) // $recipients must an array
    ->priority('high')
    ->timeToLive(0)
    ->data([
        'title' => 'LaradevTest',
        'body' => 'This tests laraDevs',
    ])
    ->send();

如果您想向主题发送 FCM,请使用方法 toTopic($topic) 代替 to()

fcm()
    ->toTopic($topic) // $topic must an string (topic name)
    ->priority('normal')
    ->timeToLive(0)
    ->notification([
        'title' => 'LaradevTest',
        'body' => 'This tests laraDevs',
    ])
    ->send();

如果您只想使用通知参数发送 FCM,以下是一个仅使用通知参数发送 FCM 的示例

fcm()
    ->to($recipients) // $recipients must an array
    ->priority('high')
    ->timeToLive(0)
    ->notification([
        'title' => 'LaradevTest',
        'body' => 'This tests laraDevs',
    ])
    ->send();

如果您想同时使用数据参数和通知参数发送 FCM,以下是一个同时使用数据参数和通知参数发送 FCM 的示例

fcm()
    ->to($recipients) // $recipients must an array
    ->priority('normal')
    ->timeToLive(0)
    ->data([
        'title' => 'LaradevTest',
        'body' => 'This tests laraDevs',
    ])
    ->notification([
        'title' => 'LaradevTest',
        'body' => 'This tests laraDevs',
    ])
    ->send();

自定义图标和操作,请按照以下步骤操作

fcm()
    ->to($recipients) // $recipients must an array
    ->priority('normal')
    ->timeToLive(0)
    ->data([
        'title' => 'LaradevTest',
        'body' => 'This tests laraDevs',
        'icon' => 'Your URL public to icon',
        'click_action'=>'action click'
    ])
    ->send();