viipers/fcm-notif-laravel

Laravel FCM (Firebase Cloud Messaging) 通知通道

v1.0 2024-01-17 11:07 UTC

This package is auto-updated.

Last update: 2024-09-26 21:44:54 UTC


README

Laravel FCM (Firebase Cloud Messaging) 通知通道

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

此包仅与Legacy HTTP Server Protocol配合使用

安装

此包可以通过 Composer 安装。

composer require viipers/fcm-notif-laravel

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 Viipers\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 Viipers\FCM\FcmMessage;

...

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

或者当在条件发送时

use Viipers\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 Viipers\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 Legacy HTTP Server Protocol

许可

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