dees040/pusher-push-notifications

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

2.0.1 2017-02-22 15:29 UTC

This package is auto-updated.

Last update: 2024-08-25 21:11:56 UTC


README

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

此包使您能够通过 Laravel 5.4 发送 Pusher 推送通知变得简单。

内容

安装

您可以通过 composer 安装此包。

composer require dees040/pusher-push-notifications:dev-master

您必须安装服务提供者。

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

设置您的 Pusher 账户

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

  • 登录到 https://dashboard.pusher.com/
  • 从侧边栏选择您的应用程序或创建一个新的应用程序。
  • 点击“推送通知”标签。
  • 上传您的 APNS 证书或添加您的 GCM API 密钥。
  • 现在选择“应用程序密钥”标签。
  • 复制您的 app_idkeysecret
  • 更新 config/broadcasting.php 文件中 pusher 连接下的值。
  • 现在您可以开始了。

用法

现在您可以在 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(''):接受 iOSAndroid 的字符串值。
  • iOS():将平台值设置为 iOS。
  • android():将平台值设置为 Android。
  • title(''):接受标题的字符串值。
  • body(''):接受正文的字符串值。
  • sound(''):接受通知声音文件的字符串值。注意,如果您留空,则默认声音值为 default
  • icon(''):接受图标文件的字符串值。(仅限 Android)
  • badge(1):接受徽章的整数值。(仅限 iOS)
  • setOption($key, $value):允许您在消息有效负载中设置任何值。有关更多信息,请参阅 此处(iOS),或 此处(Android)。

向多个平台发送

您可以使用 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() 时,您不需要定义平台,这将在幕后为您完成。

路由消息

默认情况下,Pusher 的“兴趣”消息将通过 {notifiable}.{id} 惯例定义,例如 App.User.1,但是您可以通过在 notifiable 类方法中包含 routeNotificationForPusherPushNotifications() 来更改此行为,该方法返回兴趣名称。

变更日志

有关最近更改的信息,请参阅 CHANGELOG

测试

$ composer test

安全

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

贡献

有关详细信息,请参阅 CONTRIBUTING

致谢

许可证

麻省理工学院许可证(MIT)。请参阅许可文件获取更多信息。