Laravel 的 PubNub 通知通道

1.1.0 2019-10-05 05:44 UTC

This package is auto-updated.

Last update: 2024-08-29 05:07:10 UTC


README

Latest Version on Packagist Software License Build Status StyleCI Quality Score Code Coverage Total Downloads

适用于 Laravel 5.5+ & 6.0 的 PubNub 通知通道。此通道允许您使用 PubNub 发送消息有效负载以及推送通知到 iOS、Android 和 Windows。

内容

安装

composer require laravel-notification-channels/pubnub

将服务提供者添加到您的 config/app.php

// config/app.php
'providers' => [
    ...
    NotificationChannels\Pubnub\PubnubServiceProvider::class,
],

设置 PubNub 服务

将您的 PubNub 发布密钥、订阅密钥和密钥添加到您的 config/services.php

// config/services.php
...

'pubnub' => [
    'publish_key'   => env('PUBNUB_PUBLISH_KEY'),
    'subscribe_key' => env('PUBNUB_SUBSCRIBE_KEY'),
    'secret_key'    => env('PUBNUB_SECRET_KEY'),
],

... 

使用方法

use NotificationChannels\Pubnub\PubnubChannel;
use NotificationChannels\Pubnub\PubnubMessage;
use Illuminate\Notifications\Notification;

class InvoicePaid extends Notification
{
    public function via($notifiable)
    {
        return [PubnubChannel::class];
    }

    public function toPubnub($notifiable)
    {
        return (new PubnubMessage())
            ->channel('my_channel')
            ->title('My message title')
            ->body('My message body');
    }
}

或者,您可以通过实现 routeNotificationForPubnub() 方法来提供一个与您的可通知实体特别相关的频道。

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

class User extends Model
{
    use Notifiable;
    
    public function routeNotificationForPubnub()
    {
        return $this->pubnub_channel;
    }
}

发送推送通知。您可以将 withiOS()withAndroid()withWindows() 中的任何一种方法链式调用,以将推送通知添加到消息中,适用于每个平台。

use NotificationChannels\Pubnub\PubnubChannel;
use NotificationChannels\Pubnub\PubnubMessage;
use Illuminate\Notifications\Notification;

class InvoicePaid extends Notification
{
    public function via($notifiable)
    {
        return [PubnubChannel::class];
    }

    public function toPubnub($notifiable)
    {
        return (new PubnubMessage())
            ->channel('my_channel')
            ->title('Alert: Jon Doe Sent You A Message')
            ->body('Hi')
            ->withiOS(
                (new PubnubMessage())
                    ->sound('default')
                    ->badge(1)
            )
            ->withAndroid(
                (new PubnubMessage())
                    ->sound('notification')
                    ->icon('myicon')
            )
            ->withWindows(
                (new PubnubMessage())
                    ->type('toast')
                    ->delay(450);
            );
    }
}

可用方法

  • channel(''):指定消息应发送到的频道
  • title(''):设置消息标题
  • body(''):设置消息正文
  • storeInHistory(true):如果消息应存储在 PubNub 历史记录中
  • badge(1):设置推送通知徽章上显示的数字(iOS)
  • sound(''):设置推送通知的声音(iOS、Android)
  • icon(''):设置推送通知图标(Android)
  • type(''):设置推送通知类型(Windows)
  • delay(450):设置推送通知的延迟时间(以秒为单位)(Windows)
  • setData($key, $value):向有效负载添加任何额外的数据
  • setOption($key, $value):设置推送通知的任何选项(iOSAndroid、Windows)
  • withiOS(PubnubMessage $message):设置 iOS 的推送通知
  • withAndroid(PubnubMessage $message):设置 Android 的推送通知
  • withWindows(PubnubMessage $message):设置 Windows 的推送通知

变更日志

请参阅 CHANGELOG 了解最近更改的信息。

测试

$ composer test

安全

如果您发现任何安全问题,请通过电子邮件 wade@iwader.co.uk 而不是使用问题跟踪器。

贡献

有关详细信息,请参阅 CONTRIBUTING

致谢

许可

MIT 许可证 (MIT)。请参阅 许可文件 了解更多信息。