grohiro/laravel-firebase

为 Laravel 的 Firebase 云消息

1.0.1 2018-02-14 10:33 UTC

This package is auto-updated.

Last update: 2024-08-29 04:37:15 UTC


README

Firebase 通道插件用于 Laravel。

use Illuminate\Notifications\Notification;
use Grohiro\Laravel\FCM\FirebaseChannel;
use paragraph1\phpFCM\Message;
use paragraph1\phpFCM\Recipient\Device;
use paragraph1\phpFCM\Notification;

/**
 * @see https://laravel.net.cn/docs/5.5/notifications#custom-channels
 */
class PushMessage extends Notification
{
  public function via($notifiable)
  {
    return [FirebaseChannel::class];
  }
  
  public function toFcmMessage($user)
  {
    // @see https://github.com/Paragraph1/php-fcm
    $note = new Notification('test title', 'testing body');
    $note->setIcon('notification_icon_resource_name')
        ->setColor('#ffffff')
        ->setBadge(1);
    $message = new Message();
    $message->addRecipient(new Device($user->user_device_token));
    $message->setNotification($note)
            ->setData(array('someId' => 111));
    return $message;
  }
}

要求

用法

1. 安装 laravel-firebase

$ composer require grohiro/laravel-firebase dev-master

2. 设置 Guzzle HTTP 客户端

将 ServiceProvider 添加到 app.php

// config/app.php
'providers' => [
  \Grohiro\Laravel\FCM\ServiceProvider::class,
];

3. 创建 Laravel 通知类

php artisan make:notification PushNotification

4. 设置 Firebase API 密钥

// config/app.php
'firebase' => [
  'api_key' => 'your-api-key'
],