rendyfutsuy/my-fcm

一个简单的包,可以帮助您在Laravel应用程序中发送Firebase通知。

0.3 2020-09-18 08:06 UTC

This package is auto-updated.

Last update: 2024-09-18 17:53:05 UTC


README

一个简单的包,可以帮助您在Laravel应用程序中发送Firebase通知

安装

您可以通过Composer拉取此包

$ composer require rendyfutsuy/my-fcm

Laravel

您必须注册服务提供者

// config/app.php

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

如果您想使用外观,也必须安装它

// config/app.php

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

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

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

这是发布文件的包含内容

return [

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

    'server_key' => env('FCM_SERVER_KEY', ''),

];

Lumen

将以下服务提供者添加到bootstrap/app.php文件中

$app->register(Angga\Fcm\FcmServiceProvider::class);

还将my-fcm.php配置文件复制到config/my-fcm.php

将配置添加到bootstrap/app.php文件中 重要: 这需要在注册服务提供者之前完成

$app->configure('my-fcm');
...
$app->register(Angga\Fcm\FcmServiceProvider::class);

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

APP_NAME="Laravel"
# ...
FCM_SERVER_KEY=putYourKeyHere

用法

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

$recipients = [
    'clKMv.......',
    'GxQQW.......',
];

fcm()
    ->to($recipients)
    ->priority('high')
    ->timeToLive(0)
    ->data([
        'title' => 'Test FCM',
        'body' => 'This is a test of FCM',
    ])
    ->send();

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

fcm()
    ->toTopic($topic) // $topic must an string (topic name)
    ->priority('normal')
    ->timeToLive(0)
    ->notification([
        'title' => 'Test FCM',
        'body' => 'This is a test of FCM',
    ])
    ->send();

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

fcm()
    ->to($recipients) // $recipients must an array
    ->priority('high')
    ->timeToLive(0)
    ->notification([
        'title' => 'Test FCM',
        'body' => 'This is a test of FCM',
    ])
    ->send();

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

fcm()
    ->to($recipients) // $recipients must an array
    ->priority('normal')
    ->timeToLive(0)
    ->data([
        'title' => 'Test FCM',
        'body' => 'This is a test of FCM',
    ])
    ->notification([
        'title' => 'Test FCM',
        'body' => 'This is a test of FCM',
    ])
    ->send();