vikashdudhatra/laravel-fcm-notification

Laravel FCM (Firebase Cloud Messaging) 通知通道

dev-main 2022-07-01 07:25 UTC

This package is auto-updated.

Last update: 2024-09-29 06:36:08 UTC


README

Laravel FCM (Firebase Cloud Messaging) 通知通道

使用此包通过 Laravel 向 Firebase Cloud Messaging 发送推送通知。需要 Laravel 5.5+。

此包仅与 旧版 HTTP 服务器协议 兼容

安装

可以通过 Composer 安装此包。

composer require vishaldudhatra/laravel-fcm-notification

如果安装的 Laravel 版本小于 5.5,则添加服务提供者

// config/app.php
'providers' => [
    ...
    vishaldudhatra\FCM\FcmNotificationServiceProvider::class,
    ...
];

config/services.php 中添加您的 Firebase API 密钥。

return [
   
    ...
    ...
    /*
     * Add the Firebase API key
     */
    'fcm' => [
        'key' => env('FCM_SECRET_KEY')
     ]
];

示例用法

使用 Artisan 创建通知

php artisan make:notification SomeNotification

在您的通知的 public function via($notifiable) 方法中返回 [fcm]

public function via($notifiable)
{
    return ['fcm'];
}

在您的通知中添加方法 public function toFcm($notifiable),并返回 FcmMessage 实例

use vishaldudhatra\FCM\FcmMessage;

...

public function toFcm($notifiable) 
{
    $message = new FcmMessage();
    $message->content([
        'title'        => 'Foo', 
        'body'         => 'Bar', 
        'sound'        => '', // Optional 
        'icon'         => '', // Optional
        'click_action' => '' // Optional
    ])->data([
        'param1' => 'baz' // Optional
    ])->priority(FcmMessage::PRIORITY_HIGH); // Optional - Default is 'normal'.
    
    return $message;
}

当向特定设备发送时,确保您的通知实体已定义 routeNotificationForFcm 方法

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

当向一个主题发送时,您可以在通知的 toFcm 方法中定义

use vishaldudhatra\FCM\FcmMessage;

...

public function toFcm($notifiable) 
{
    $message = new FcmMessage();
    $message->to('the-topic', $recipientIsTopic = true)
    ->content([...])
    ->data([...]);
    
    return $message;
}

或者当有条件发送时

use vishaldudhatra\FCM\FcmMessage;

...

public function toFcm($notifiable) 
{
    $message = new FcmMessage();
    $message->contentAvailable(true)
        ->priority('high')
        ->condition("'user_".$notifiable->id."' in topics")
        ->data([...]);
    
    return $message;
}

您可以通过 setHeaders() 提供可选的头部信息或覆盖请求头部

use vishaldudhatra\FCM\FcmMessage;

...

public function toFcm($notifiable) 
{
    $message = new FcmMessage();
    $message->setHeaders([
        'project_id'    =>  "48542497347"   // FCM sender_id
    ])->content([
        'title'        => 'Foo', 
        'body'         => 'Bar', 
        'sound'        => '', // Optional 
        'icon'         => '', // Optional
        'click_action' => '' // Optional
    ])->data([
        'param1' => 'baz' // Optional
    ])->priority(FcmMessage::PRIORITY_HIGH); // Optional - Default is 'normal'.
    
    return $message;
}

解释响应

要处理任何 Laravel 通知通道的响应,请检查 Laravel 通知事件

此通道返回一个 JSON 数组响应

 {
    "multicast_id": "number",
    "success": "number",
    "failure": "number",
    "canonical_ids": "number",
    "results": "array"
 }

有关响应解释的文档,请参阅 FCM 旧版 HTTP 服务器协议

许可证

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