algorithm-sam/laravel-fcm-notification

基于AlgorithmSam/laravel-fcm-notification的Laravel FCM (Firebase Cloud Messaging) 通知通道,支持laravel 10

v1.0.1 2024-05-06 11:38 UTC

This package is auto-updated.

Last update: 2024-09-06 12:24:48 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)。有关更多信息,请参阅许可文件