besanek / laravel-firebase-notifications

用于集成Firebase Messaging作为Laravel通知的轻量层

v4.1.0 2024-04-24 10:17 UTC

This package is auto-updated.

Last update: 2024-09-24 11:08:06 UTC


README

用于集成Firebase Messaging作为Laravel通知的轻量层

需求

  • PHP >= 8.1
  • Laravel = 10.x

安装

$ composer require "besanek/laravel-firebase-notifications"

这就完成了,感谢包自动发现。

设置

请遵循kreait/laravel-firebase的配置指南。

基本用法

将新方法routeNotificationForFirebase添加到你的可通知实体中,该方法返回设备ID。

    /**
     * It could be one device
     */
    public function routeNotificationForFirebase()
    {
        return $this->device_id;
    }
    
    /**
     * Or you can return array for multicast
     */
    public function routeNotificationForFirebase()
    {
        return $this->devices()->get()->pluck('id');
    }

在通知实体中,您应在via()方法中添加firebase

    public function via(): array
    {
        return ['firebase', /* 'email', 'database', 'etc...'*/];
    }

然后您可以在toFirebase()方法中构造CloudMessage

    public function toFirebase(): Messaging\CloudMessage
        $notification = Messaging\Notification::create('I <3 laravel', 'It is true');
        return Messaging\CloudMessage::new()->withNotification($notificatin);
    }

请查看官方PHP SDK文档以了解所有功能的完整使用方法。