Laravel FCM:仅发送数据有效载荷

时间:2019-08-19 09:44:45

标签: php laravel firebase-cloud-messaging

我遇到了一些问题,我想使用 brozot / Laravel-FCM 发送推送通知。我只想在后台运行应用程序时仅发送数据有效负载而无需通知有效负载。但是当我尝试时,推送通知不会给我“ 标题”和“ 正文

我一直在尝试使用此代码,但是它不起作用:

system('stty cbreak');
while(true){
    if($char = fread(STDIN, 1)) {
        echo chr(8) . mb_strtoupper($char);
    }
}
  

我找到了答案

我只是这样更改我的代码:

<?php

namespace App\Jobs;

use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use FCM as FCM;
use GuzzleHttp\Client;
use LaravelFCM\Message\OptionsBuilder;
use LaravelFCM\Message\PayloadDataBuilder;
use LaravelFCM\Message\PayloadNotificationBuilder;

class FCMNotification implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    protected $data;
    protected $notification;
    protected $token;

    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct($data, $notification, $token)
    {
        $this->data = $data;
        $this->notification = $notification;
        $this->token = $token;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        $this->data["notification_id"] = $this->notification->id;
        //$notificationBuilder = new PayloadNotificationBuilder($this->data['title']);
        //$notificationBuilder->setBody($this->data["body"])->setSound('default');

        $optionBuilder = new OptionsBuilder();
        $optionBuilder->setTimeToLive(60 * 20);

        $dataBuilder = new PayloadDataBuilder();
        $dataBuilder->addData($this->data);

        $option = $optionBuilder->build();
        //$notification = $notificationBuilder->build();
        $data = $dataBuilder->build();

        $downstreamResponse = FCM::sendTo($this->token, $option, null, $data);
        return response()->default(200, 'Sent', $downstreamResponse);
    }

    public function tags()
    {
        return [ 'listener: ' . static::class, ' FCM Notification'];
    }
}

0 个答案:

没有答案