neo/pusher-beams

Pusher Beams 是 Pusher 提供的推送通知服务。

v1.0.5 2020-04-17 04:34 UTC

This package is auto-updated.

Last update: 2024-09-07 06:07:28 UTC


README

Latest Version on Packagist Software License Build Status StyleCI Quality Score

该软件包可以轻松地将Pusher 推送通知发送到 Laravel(应与其他非 Laravel PHP 项目兼容)。它基于 Mohamed Said 的此软件包

内容

安装

您可以通过 composer 安装此软件包

composer require neo/pusher-beams

...

设置您的 Pusher 账户

...

用法

更新您的 .env 文件,并添加以下密钥

PUSHER_BEAMS_SECRET_KEY="PUSHER_BEAMS_SECRET_KEY"
PUSHER_BEAMS_INSTANCE_ID="PUSHER_BEAMS_INSTANCE_ID"

💡 您需要将 PUSHER_BEAMS_SECRET_KEY 和 PUSHER_BEAMS_INSTANCE_ID 密钥替换为您从 Pusher 控制台获取的密钥。

打开配置目录中的 broadcasting.php 文件,并将以下密钥添加到 pusher 连接数组中

'connections' => [
    'pusher' => [
        // [...]

        'beams' => [
            'secret_key' => env('PUSHER_BEAMS_SECRET_KEY'),
            'instance_id' => env('PUSHER_BEAMS_INSTANCE_ID'),
        ],

        // [...]
    ],
],

接下来,创建一个新的通知类,我们将在这里添加我们的推送通知。在您的终端中运行以下命令以创建该类

$ php artisan make:notification UserCommented

这将在 app/Notifications 目录中创建一个新的 UserCommented 类。打开文件,您可以执行类似于以下示例类的操作

<?php

namespace App\Notifications;

use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Neo\PusherBeams\PusherBeams;
use Neo\PusherBeams\PusherMessage;
use App\User;
use App\PhotoComment;
use App\Photo;

class UserCommented extends Notification
{
    use Queueable;

    public $user;

    public $comment;

    public $photo;

    public function __construct(User $user, Photo $photo, PhotoComment $comment)
    {
        $this->user = $user;
        
        $this->comment = $comment;
        
        $this->photo = $photo;
    }

    public function via($notifiable)
    {
        return [PusherBeams::class];
    }

    public function toPushNotification($notifiable)
    {
        return PusherMessage::create()
            ->iOS()
            ->sound('success')
            ->title('New Comment')
            ->body("{$this->user->name} commented on your photo: {$this->comment->comment}")
            ->setOption('apns.aps.mutable-content', 1)
            ->setOption('apns.data.attachment-url', $this->photo->image);
    }

    public function pushNotificationInterest()
    {
        $id = $this->photo->id;

        $audience = strtolower($this->user->settings->notification_comments);

        return "photo_{$id}-comment_{$audience}";
    }
}

在上面的类中,我们扩展了 Notification 类,并实现了 toPushNotification 方法,该方法将在需要时用于发送推送通知。在 via 方法中,我们指定我们想要通过哪些通道发送通知,并在 pushNotificationInterest 中指定我们想要发布推送通知的兴趣。

发送到多个平台

...

路由消息

...

更新日志

有关最近更改的更多信息,请参阅更新日志

测试

$ composer test

安全

如果您发现任何安全相关的问题,请通过neo@creativitykills.co 发送电子邮件,而不是使用问题跟踪器。

贡献

有关详细信息,请参阅贡献指南

鸣谢

许可证

MIT 许可证 (MIT)。有关更多信息,请参阅许可证文件