freelyformed/laravel-firebase

此包已被放弃,不再维护。未建议替代包。

用于 paragraph1/php-fcm 的 Laravel 封装

0.0.4 2016-06-24 11:30 UTC

This package is not auto-updated.

Last update: 2016-12-30 12:19:59 UTC


README

此包是 Paragraph1/php-fcm 的 Laravel 服务提供者。

安装

composer require freelyformed/laravel-firebase @dev

使用方法

将服务提供者添加到您的 config/app.php 文件中的 providers 列表

<?php return [

    // ...

    'providers' => [
        // ...
        Freelyformed\LaravelFirebase\FirebaseServiceProvider::class,
    ]

];

发布并编辑包配置

php artisan vendor:publish --provider="Freelyformed\LaravelFirebase\FirebaseServiceProvider" --tag=config

在您的作业或事件监听器中为 php-fcm Client 类型提示,以下是一个示例作业,用于在发生某些情况时向 FCM 发送通知

<?php namespace App\Jobs;

use App\Something;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use paragraph1\phpFCM\Client;
use paragraph1\phpFCM\Message;
use paragraph1\phpFCM\Notification;
use paragraph1\phpFCM\Recipient\Device;

class SomethingHappened extends Job implements ShouldQueue {
    use InteractsWithQueue, SerializesModels;

    protected $thing;

    /**
     * Create a new job instance.
     *
     * @param Something $thing A thing instance
     */
    public function __construct(Something $thing) {
        $this->thing = $thing;
    }

    /**
     * Execute the job.
     * @param Client $client
     *
     * @return void
     */
    public function handle(Client $client) {
        $message = new Message();


        $message->addRecipient(new Device('device-token'));

        $notification = new Notification('Something happened', 'Something really did just happen');
        $notification
            ->setIcon('notification_icon_resource_name')
            ->setColor('#ffffff')
            ->setBadge(1);

        $message->setNotification($notification);
        $message->setData(['id' => $this->something->id]);

        $response = $client->send($message);
        // TODO: Handle response
    }
}