origami/push

Push 通知 Laravel 扩展包

5.1.0 2024-08-30 13:32 UTC

README

此包为 Laravel 6 或更高版本的项目添加推送通知通道。

有关更多通知通道信息,请访问 https://laravel.net.cn/docs/8.x/notifications

安装

通过 Composer 安装此包。

composer require origami/push

要求

此包旨在与 Laravel >= 6 一起使用。

配置

有一些 API 配置选项您可能希望覆盖。首先,发布默认配置。

php artisan vendor:publish --provider="Origami\Api\ApiServiceProvider"

这将在以下位置添加一个新的配置文件: config/push.php

APNs

此包的 4+ 版本使用 edamov/pushok 进行 APNs 传输和逻辑。您应将以下内容添加到 .env 文件中,以设置所需的配置,或参阅 edamov/pushok 的说明以获取证书 (.pem) 选项。

默认私有密钥位置为 storage/certificates/apns.p8

PUSH_APNS_ENV=production
PUSH_APNS_KEY_ID=
PUSH_APNS_TEAM_ID=
PUSH_APNS_APP_BUNDLE=
PUSH_APNS_PRIVATE_KEY=
PUSH_APNS_PRIVATE_KEY_SECRET=

FCM

此包的 5 版本使用 FCM HTTP v1 APIorigami/google-auth 包从服务账户凭据获取 OAuth 令牌。

首先,从 Firebase 控制台中设置项目 ID

PUSH_FCM_PROJECT="your-app-123abc"

然后生成并下载您的服务账户 JSON 文件以生成 OAuth 访问令牌。

  1. 在 Firebase 控制台中,转到设置 > 服务账户。
  2. 单击“生成新私钥”,然后通过单击“生成密钥”进行确认。
  3. 请将包含密钥的 JSON 文件安全地存储在:storage/app/google-auth/service-account-credentials.json

如果您想更改加载的路径,可以设置 GOOGLE_AUTH_SERVICE_CREDENTIALS 环境变量,或要更改其他设置,可以发布 google-auth 配置:php artisan vendor:publish --provider="Origami\GoogleAuth\GoogleAuthServiceProvider"

用法

设备 Eloquent 模型

您可能会使用 Eloquent 模型在数据库中存储设备,例如 App\Device

为了让它与此包一起工作,您只需确保它实现了 Origami\Push\Contracts\Device 接口。

namespace App;

use Origami\Push\Contracts\Device as PushDevice;

class Device extends Model implements PushDevice {

}

接下来,您需要添加两个方法来获取服务 - 要使用 iOS 的 apns 或要使用 Firebase 云消息的 fcm - 以及设备标识符。

public function getPushService()
{
    switch ( $this->make ) {
        case 'apple':
        case 'ios':
        case 'iphone':
            return 'apns';
            break;
        case 'android':
            return 'fcm';
            break;
        default:
            throw new \Exception('Unable to determine push service for ' . $this->make);
    }
}

public function getPushToken()
{
    return $this->device_token;
}

用户可通知设备

在 Laravel 项目中,您最有可能向您的用户发送推送通知。有关更多信息,请参阅 Laravel 文档

要获取用户的设备,假设您正在使用上面的 Eloquent 模型,只需向 Eloquent 模型添加一个 routeNotificationForPush 方法即可。

public function routeNotificationForPush()
{
    $devices = $this->devices()->get();
    return $devices ? $devices->all() : [];
}

示例

通知类

<?php

namespace App\Notifications;

use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Origami\Push\PushNotification;

class UserJoined extends Notification implements ShouldQueue
{
    use Queueable;

    public function __construct($user)
    {
        $this->user = $user;
    }

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

    public function toPush($notifiable)
    {
        return (new PushNotification)
                    ->setTitle('New User')
                    ->setBody($this->user->name . ' just joined')
                    ->setMeta([
                        'event' => 'NewUser',
                        'user' => $this->user->id
                    ]);
    }

}

独立

<?php

$device = new Origami\Push\Device('apns', '12346...');

$push = (new Origami\Push\PushNotification)
        ->setBody('Testing, testing, 1, 2, 3.');

app('Origami\Push\PushManager')
		->driver($device->getPushService())
        ->send($device, $push);

版本

  • v5.* - 版本 5 是一个破坏性变更,它更新了配置和 fcm 的驱动程序以使用新的 HTTP v1 API 和服务账户凭据 / OAuth。
  • v4.* - 版本 4 是一个破坏性变更,它更新了 apns 和 fcm 的配置和驱动程序。
  • v3.* - 版本 3 将 Laravel 支持升级到包括 6、7 和 8 项目。Laravel 5.x 已弃用。
  • v2.* - 版本 2 是包的重写,以与 Laravel 5.3 通知或独立版本一起使用
  • v1.-* - 第1版没有与Laravel的通知服务集成

作者

Papertank Limited

许可证

查看许可证