PHPackages                             medz/laravel-jpush-notification-channel - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Mail &amp; Notifications](/categories/mail)
4. /
5. medz/laravel-jpush-notification-channel

AbandonedArchivedLibrary[Mail &amp; Notifications](/categories/mail)

medz/laravel-jpush-notification-channel
=======================================

JPush for Laravel notification channel

1.0.4(6y ago)4444.2k↓38.9%22MITPHP

Since Feb 21Pushed 6y ago3 watchersCompare

[ Source](https://github.com/medz/laravel-jpush-notification-channel)[ Packagist](https://packagist.org/packages/medz/laravel-jpush-notification-channel)[ RSS](/packages/medz-laravel-jpush-notification-channel/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (5)Dependencies (3)Versions (6)Used By (2)

极光推送在 Laravel 通知的支持
===================

[](#极光推送在-laravel-通知的支持)

我们在开发针对国内运营的时候进行需要使用过程的几家推送，极光推送则是其中之一。这个包就可以让你方便的在你构件的 Laravel 应用中进行极光推送的使用。

前提
--

[](#前提)

安装驱动需要以下条件：

- PHP `>=` 7
- Laravel `>=` 5.5

安装
--

[](#安装)

在你的 Laravel 应用目录执行 Composer 进行安装：

```
composer require medz/laravel-jpush-notification-channel

```

> 包中依赖了匹配的 `jpush/jpush` 依赖版本为 `^3.6`，你已经依赖了更低版本的不兼容版本包，使用的时候要小心了！

配置
--

[](#配置)

在 `config/services.php` 中进行如下配置：

```
return [
    'jpush' => [
        'app_key' => env('JPUSH_APP_KEY', ''),
        'master_secret' => env('JPUSH_MASTER_SECRET', ''),
        'apns_production' => env('JPUSH_APNS_PRODUCTION', false),
    ],
]
```

然后在 `.env` 文件中进行配置：

```
JPUSH_APP_KEY=
JPUSH_MASTER_SECRET=
JPUSH_APNS_PRODUCTION=

```

使用
--

[](#使用)

我们已**用户为例**，这里使用 `laravel/laravel` 创建的默认应用模型位置。

### 数据模型

[](#数据模型)

在用户模型中进行配置，创建一个 `routeNotificationForJpush` 方法在模型上：

```
use Illuminate\Foundation\Auth\User as Authenticatable;
use Medz\Laravel\Notifications\JPush\Sender as JPushSender;

class User extends Authenticatable
{
    /**
     * Get Notification for JPush sender.
     * @return \Medz\Laravel\Notifications\JPush\Sender
     */
    protected function routeNotificationForJpush()
    {
        return new JPushSender([
            'platform' => 'all',
            'audience' => [
                'alias' => sprintf('user_%d', $this->id),
            ],
        ]);
    }
}
```

这里我们返回一个 `Medz\Laravel\Notifications\JPush\Sender` 实例，可以使用构造参数快速配置，如同上面一样，也可以使用链式调用进行配置。链式调用的 API 如下：

- `setPlatform` 设置平台，值有 `all`、`winphone`、`android` 和 `ios`
- `setAudience` 推送目标进行设置

> `setAudience` 方法或者构造参数中的 `audience` 设置参考：[推送目标](https://docs.jiguang.cn/jpush/server/push/rest_api_v3_push/#audience)文档。

### 通知类

[](#通知类)

一般我们写通知大概都是通过 `php artisan make:notification` 进行创建的，存放在 `app/Notifications/` 目录下，假设我们现在有一个评论通知类 `CommentNotification.php` 我们仅需在里面增加下面的代码：

```
