namelivia/push-notifications-laravel

此包已被弃用且不再维护。未建议替代包。

Laravel 的 Pusher Beams 桥接器

1.0.5 2020-03-04 08:50 UTC

This package is auto-updated.

Last update: 2024-03-29 03:44:58 UTC


README

Build Status StyleCI codecov License

这是一个用于 Laravel 的 Pusher Beams 桥接器。主要基于 pusher/pusher-http-php

向设备兴趣发布

您可以使用 设备兴趣 向已订阅的设备组广播通知

$publishResponse = PusherBeams::publishToInterests(
  "body" => "Hello, world!",
  ["donuts"],
  [
    "apns" => [
      "aps" => [
	    "alert" => "Hello!",
      ],
    ],
    "fcm" => [
      "notification" => [
        "title" => "Hello!",
        "body" => "Hello, world!",
      ],
    ],
  ]
);

echo("Published with Publish ID: " . $publishResponse->publishId . "\n");

向认证用户发布

使用 认证用户 安全地向您的应用程序的个别用户发送通知

$publishResponse = PuhserBeams::publishToUsers(
  ["user-0001"],
  [
    "apns" => [
      "aps" => [
        "alert" => "Hello!",
      ],
    ],
    "fcm" => [
      "notification" => [
        "title" => "Hello!",
        "body" => "Hello, world!",
      ],
    ],
  ]
);

echo("Published with Publish ID: " . $publishResponse->publishId . "\n");

安装

使用 Composer 在您项目的根目录中要求此包。

$ composer require namelivia/push-notifications-laravel

将服务提供者添加到 config/app.php 中的 providers 数组。如果您使用 Laravel 5.5 或更高版本,则无需这样做。

Pusher\Beams\Laravel\PusherBeamsServiceProvider::class

如果您愿意,可以使用 facade。在 config/app.php 中将引用添加到您的别名数组中。

'PusherBeams' => Pusher\Beams\Laravel\Facades\PusherBeams::class

配置

Laravel Pusher 需要 连接配置。要开始,您需要发布所有供应商资产

$ php artisan vendor:publish --provider="Pusher\Beams\Laravel\PusherBeamsServiceProvider"

这将创建一个 config/pusher-beams.php 文件在您的应用程序中,您可以根据需要修改它来设置配置。同时,请确保检查此包中原始配置文件之间的更改。

默认连接名称

此选项 default 是您可以指定以下哪个连接用作您所有工作的默认连接的地方。当然,您可以使用许多连接同时使用管理器类。此设置的默认值是 main

Pusher 连接

此选项 connections 是为您的应用程序设置每个连接的地方。已包括示例配置,但您可以添加尽可能多的连接。

用法

PusherBeamsManager

这是最感兴趣的类。它绑定到 ioc 容器为 pusher-beams,可以使用 Facades\PusherBeams facade 访问。此类通过扩展 AbstractManager 实现 ManagerInterface。该接口和抽象类都是 Graham Campbell 的 Graham CampbellLaravel Manager 包的一部分,因此您可能想查看该存储库中关于如何使用管理器类的文档。请注意,返回的连接类始终是 PushNotification 的实例。

Facades\PusherBeams

此 facade 将动态地将静态方法调用传递到 ioc 容器中的 pusher-beams 对象,默认情况下是 PusherBeamsManager 类。

PusherBeamsServiceProvider

此类不包含任何有趣的公共方法。应将此类添加到 config/app.php 中的提供者数组。此类将设置 ioc 绑定。

示例

在这里,您可以看到这个包是多么简单易用。默认情况下,默认适配器是 main。在您在配置文件中输入认证详细信息后,它就会正常工作

// You can alias this in config/app.php.
use Pusher\Beams\Laravel\Facades\PusherBeams;

PusherBeams::publish(
	["hello", "donuts"],
	[
		"fcm" => [
			"notification" => [
				"title" => "Hi!",
				"body" => "This is my first Push Notification!"
				]
		],
		"apns" => ["aps" => [
				"alert" => [
					"title" => "Hi!",
					"body" => "This is my first Push Notification!"
				]
		]]
	]
);
// We're done here - how easy was that, it just works!

Pusher Beams 管理器将表现得像是一个 PusherBeams。如果您想调用特定的连接,可以使用连接方法来实现。

use Pusher\Beams\Laravel\Facades\PusherBeams;

// Writing this…
PusherBeams::connection('main')->publish([...]);

// …is identical to writing this
PusherBeams::publish([...]);

// and is also identical to writing this.
PusherBeams::connection()->publish([...]);

// This is because the main connection is configured to be the default.
PusherBeams::getDefaultConnection(); // This will return main.

// We can change the default connection.
PusherBeams::setDefaultConnection('alternative'); // The default is now alternative.

如果您像我一样更喜欢使用依赖注入而不是外观(facade),则可以注入该管理器。

use Pusher\Beams\Laravel\PusherBeamsManager;

class Foo
{
    protected $pusherBeams;

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

    public function bar()
    {
        $this->pusherBeams->publish('my-channel', 'my-event', ['message' => $message]);
    }
}

App::make('Foo')->bar();

文档

此包是 官方 Pusher Beams 包 的 Laravel 包装器。

许可证

MIT