douglasresendemaciel/fcm-laravel-notification

用于连接laravel通知系统到Google Firebase Cloud Messaging的驱动程序

dev-master 2017-11-14 12:57 UTC

This package is auto-updated.

Last update: 2024-09-09 14:03:50 UTC


README

此库允许通过laravel的通知发送Firebase Cloud Messaging的推送通知。

可以发送通知到网站、Android和IOS,无需任何其他集成。

安装

在您的终端中运行以下命令

composer require "douglasresendemaciel/fcm-laravel-notification:@dev"

或者将此添加到您的composer.json文件中的require部分

"douglasresendemaciel/fcm-laravel-notification"

然后运行composer update

安装完成后,您需要注册服务提供者。打开config/app.php文件,并将以下内容添加到providers键中。

'providers' => [
...
DouglasResende\FCM\NotificationServiceProvider::class
...

使用方法

首先,使用artisan命令php artisan make:notification MyNotification创建您的通知类

实现您的通知

<?php

namespace App\Notifications;

use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use DouglasResende\FCM\Messages\FirebaseMessage;

class MyNotification extends Notification
{
    use Queueable;

    public function via($notifiable)
    {
        return ['fcm'];
    }

    public function toFcm($notifiable) 
    {
       
        return (new FirebaseMessage())->setContent('Test Notification', 'This is a Test');
        
    }
}

在您的可通知对象上实现

use Illuminate\Database\Eloquent\Model;
use Illuminate\Notifications\Notifiable;

class User extends Model {
    use Notifiable;

   public function routeNotificationForFcm() {
        //return a device token, either from the model or from some other place. 
        return $this->device_token;
    }

}

打开config/broadcasting.php文件,并将Firebase API密钥添加到connections部分

...
'fcm' => [
    'key' => env('FCM_API_KEY','YOUR_API_KEY')
]
...

触发通知

$User = User::find(1);

//Send Notification
$User->notify(new MyNotification());

参考资料

有关更多信息,请阅读官方文档:https://laravel.net.cn/docs/5.3/notifications