tomatophp/pusher-notifications

Pusher 本地推送通知驱动程序。

v1.0.0 2024-03-12 18:59 UTC

This package is auto-updated.

Last update: 2024-09-12 20:11:44 UTC


README

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

此包可以轻松使用 Laravel 发送 Pusher Beams 推送通知

请注意,此通知通道不应与 Pusher Channels 混淆。

另外,请注意,在版本 2.0 之前,此包与 Pusher 的 beta 推送通知服务集成,该服务是 Pusher Channels 的一部分。有关更多信息,请参阅 Pusher 的 迁移指南

内容

安装

您可以通过 composer 安装此包

composer require laravel-notification-channels/pusher-push-notifications

设置您的 Pusher 账户

在开始使用此包之前,您应设置一个 Pusher Beams 账户。以下是所需的步骤。

  • 登录到 https://dash.pusher.com/
  • 选择“Beams”产品。
  • 从列表中选择您的实例或创建一个新实例。
  • 点击“设置”标签。
  • 上传您的 APNS 证书或添加您的 FCM 服务器密钥。
  • 现在选择“密钥”标签。
  • 复制您的 实例 IdSecret Key
  • 现在在您的 config/services.php 文件中添加一个新条目
    'pusher' => [
        'beams_instance_id' => 'Your Instance Id',
        'beams_secret_key' => 'Your Secret Key',
    ],
  • 现在您可以出发了。

使用方法

现在您可以在 Notification 类的 via() 方法中使用此通道。

use NotificationChannels\PusherPushNotifications\PusherChannel;
use NotificationChannels\PusherPushNotifications\PusherMessage;
use Illuminate\Notifications\Notification;

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

    public function toPushNotification($notifiable)
    {
        return PusherMessage::create()
            ->iOS()
            ->badge(1)
            ->sound('success')
            ->body("Your {$notifiable->service} account was approved!");
    }
}

可用的消息方法

  • platform(''): 接受 iOSAndroidweb 的字符串值。
  • iOS(): 将平台值设置为 iOS。
  • android(): 将平台值设置为 Android。
  • web(): 将平台值设置为 web。
  • link(): 接受一个字符串值,该值将引导到通知点击时指定的 URI。
  • title(''): 接受一个字符串值作为标题。
  • body(''): 接受一个字符串值作为正文。
  • sound(''): 接受一个字符串值作为通知声音文件。注意,如果您留空,则默认声音值为 default
  • icon(''): 接受一个字符串值作为图标文件。(仅限 Android)
  • badge(1): 接受一个整数值作为徽章。(仅限 iOS)
  • setOption($key, $value): 允许您在消息有效负载中设置任何值。有关更多信息,请参阅 Pusher Beam 文档中的请求体部分

发送到多个平台

您可以使用 withiOS()withAndroid() 方法同时向 iOS 设备和 Android 设备发送单个消息

public function toPushNotification($notifiable)
{
    $message = "Your {$notifiable->service} account was approved!";

    return PusherMessage::create()
        ->iOS()
        ->badge(1)
        ->body($message)
        ->withAndroid(
            PusherMessage::create()
                ->title($message)
                ->icon('icon')
        );
}
  • 请注意,iOS 是默认平台,这意味着您不需要调用 ->iOS()
  • 当使用 withAndroid()withiOS()withWeb() 时,您不需要定义平台,它会在幕后为您完成。

路由消息

默认情况下,推送的“兴趣”消息将使用 {notifiable}.{id} 约定定义,例如 App.User.1,但是您可以通过在 notifiable 类中包含一个 routeNotificationFor() 来更改此行为。

例如,如果您正在向User模型推送通知,您可以进入App\Models\User类并实现方法

public function routeNotificationForPusherPushNotifications($notification): string
{
    return 'your.custom.interest.string';
}

PusherPushNotifications()方法返回兴趣名称。

发布给用户

您可以像向兴趣发布一样向用户发布,但必须在可通知模型中添加以下变量

class Client extends Model
{
    use Notifiable;

    public $pushNotificationType = 'users';
}

变更日志

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

测试

$ composer test

安全性

如果您发现任何安全问题,请通过themsaid@gmail.com发送电子邮件,而不是使用问题跟踪器。

贡献

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

致谢

许可证

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