dptsi/laravel-push-notification

Laravel Firebase Cloud Messaging.

dev-main 2023-02-01 06:55 UTC

This package is auto-updated.

Last update: 2024-08-29 10:18:24 UTC


README

简介

一个通过Firebase在Laravel中发送推送通知或自定义消息的包。

Firebase Cloud Messaging (FCM) 是一个跨平台的消息解决方案,可以让您免费可靠地发送消息。

对于即时消息等用例,消息可以传输最多4KB的有效负载到客户端应用。

安装

按照以下步骤安装此包。

Composer

composer require dptsi/laravel-push-notification

复制配置

运行 php artisan vendor:publish --provider="Dptsi\PushNotification\Providers\PushNotificationServiceProvider" 以发布 pushnotification.php 配置文件。

获取认证密钥

https://console.firebase.google.com/ 获取认证密钥

根据需要配置 pushnotification.php

'authentication_key' => '{AUTHENTICATION_KEY}'

用法

按照以下步骤了解如何使用此包。

Controller/Service 或任何类中的示例用法

use Dptsi\PushNotification\Facades\PushNotification;

class MyController
{
    private $deviceTokens =['{TOKEN_1}', '{TOKEN_2}'];

    public function sendNotification()
    {
        return PushNotification::withTitle('Test Title')
            ->withBody('Test body')
            ->withImage('https://firebase.google.com/images/social.png')
            ->withIcon('https://seeklogo.com/images/F/firebase-logo-402F407EE0-seeklogo.com.png')
            ->withSound('default')
            ->withClickAction('https://www.google.com')
            ->withPriority('high')
            ->withAdditionalData([
                'color' => '#rrggbb',
                'badge' => 0,
            ])
            ->sendNotification($this->deviceTokens);

        // Or
        return PushNotification::fromArray(['title' => 'Test Title', 'body' => 'Test body'])->sendNotification($this->deviceTokens);
    }

    public function sendMessage()
    {
        return PushNotification::withTitle('Test Title')
            ->withBody('Test body')
            ->sendMessage($this->deviceTokens);

        // Or
        return PushNotification::fromArray(['title' => 'Test Title', 'body' => 'Test body'])->sendMessage($this->deviceTokens);
    }
}

Notification 类中的示例用法

use Illuminate\Notifications\Notification;
use Dptsi\PushNotification\Messages\FirebaseMessage;

class SendBirthdayReminder extends Notification
{
    /**
     * Get the notification's delivery channels.
     */
    public function via($notifiable)
    {
        return ['firebase'];
    }

    /**
     * Get the firebase representation of the notification.
     */
    public function toFirebase($notifiable)
    {
        $deviceTokens = [
            '{TOKEN_1}',
            '{TOKEN_2}'
        ];

        return (new FirebaseMessage)
            ->withTitle('Hey, ', $notifiable->first_name)
            ->withBody('Happy Birthday!')
            ->asNotification($deviceTokens); // OR ->asMessage($deviceTokens);
    }
}

提示

  • 检查如何在 JavaScript 客户端 中接收消息或推送通知的示例。
  • 您可以使用 pushnotification() 辅助函数而不是 Facade。

有效负载

检查如何构建发送到Firebase的有效负载

示例 1

PushNotification::withTitle('Test Title')->withBody('Test body')->sendNotification('token1');
{
  "registration_ids": [
    "token1"
  ],
  "notification": {
    "title": "Test Title",
    "body": "Test body"
  },
  "priority": "normal"
}

示例 2

PushNotification::withTitle('Test Title')->withBody('Test body')->sendMessage('token1');
{
  "registration_ids": [
    "token1"
  ],
  "data": {
    "title": "Test Title",
    "body": "Test body"
  }
}

如果您想从头开始创建有效负载,可以使用方法 fromRaw,例如

return PushNotification::fromRaw([
    'registration_ids' => ['token1', 'token2'],
    'data' => [
        'key_1' => 'Value 1',
        'key_2' => 'Value 2'
    ],
    'android' => [
        'ttl' => '1000s',
        'priority' => 'normal',
        'notification' => [
            'key_1' => 'Value 1',
            'key_2' => 'Value 2'
        ],
    ],
])->send();