davidvrsantos/laravel-fcm-notification

Laravel FCM (Firebase Cloud Messaging) 通知通道

dev-master 2023-11-30 12:05 UTC

This package is auto-updated.

Last update: 2024-09-30 01:41:25 UTC


README

Laravel FCM (Firebase Cloud Messaging) 通知通道

GitHub tag Packagist Downloads Build Status License

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

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

安装

此包可以通过Composer安装。

composer require benwilkins/laravel-fcm-notification

如果在Laravel 5.5以下版本安装,请添加服务提供者

// config/app.php
'providers' => [
    ...
    Benwilkins\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 Benwilkins\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 Benwilkins\FCM\FcmMessage;

...

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

或当有条件发送时

use Benwilkins\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 Benwilkins\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)。请参阅许可证文件以获取更多信息。