Skip to content

Commit

Permalink
push notification
Browse files Browse the repository at this point in the history
  • Loading branch information
liyu001989 committed Aug 7, 2020
1 parent 3078af3 commit 74330ce
Show file tree
Hide file tree
Showing 10 changed files with 138 additions and 5 deletions.
2 changes: 1 addition & 1 deletion app/Http/Controllers/Api/UsersController.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public function update(UserRequest $request)
{
$user = $request->user();

$attributes = $request->only(['name', 'email', 'introduction']);
$attributes = $request->only(['name', 'email', 'introduction', 'registration_id']);

if ($request->avatar_image_id) {
$image = Image::find($request->avatar_image_id);
Expand Down
1 change: 1 addition & 0 deletions app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class User extends Authenticatable implements MustVerifyEmailContract, JWTSubjec

protected $fillable = [
'name', 'phone', 'email', 'password', 'introduction', 'avatar',
'weixin_openid', 'weixin_unionid', 'registration_id'
];

protected $hidden = [
Expand Down
22 changes: 22 additions & 0 deletions app/Notifications/Channels/JPushChannel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace App\Notifications\Channels;

use JPush\Client;
use Illuminate\Notifications\Notification;

class JPushChannel
{
protected $client;

public function __construct(Client $client)
{
$this->client = $client;
}

public function send($notifiable, Notification $notification)
{
$notification->toJPush($notifiable, $this->client->push())->send();
}
}

12 changes: 11 additions & 1 deletion app/Notifications/TopicReplied.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace App\Notifications;

use JPush\PushPayload;
use App\Notifications\Channels\JPushChannel;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Illuminate\Contracts\Queue\ShouldQueue;
Expand All @@ -23,7 +25,15 @@ public function __construct(Reply $reply)
public function via($notifiable)
{
// 开启通知的频道
return ['database', 'mail'];
return ['database', 'mail', JPushChannel::class];
}

public function toJPush($notifiable, PushPayload $payload): PushPayload
{
return $payload
->setPlatform('all')
->addRegistrationId($notifiable->registration_id)
->setNotificationAlert(strip_tags($this->reply->content));
}

public function toDatabase($notifiable)
Expand Down
23 changes: 23 additions & 0 deletions app/Providers/JpushServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace App\Providers;

use JPush\Client;
use Illuminate\Support\ServiceProvider;

class JpushServiceProvider extends ServiceProvider
{
public function boot()
{
//
}

public function register()
{
$this->app->singleton(Client::class, function ($app) {
return new Client(config('jpush.key'), config('jpush.secret'));
});

$this->app->alias(Client::class, 'jpush');
}
}
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"gregwar/captcha": "^1.1",
"guzzlehttp/guzzle": "~6.3",
"intervention/image": "^2.5",
"jpush/jpush": "^3.6",
"laravel/framework": "^7.0",
"laravel/horizon": "~4.3",
"laravel/tinker": "^2.0",
Expand Down
51 changes: 50 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions config/app.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<?php

return [

/*
|--------------------------------------------------------------------------
| Application Name
Expand Down Expand Up @@ -176,7 +175,7 @@
App\Providers\RouteServiceProvider::class,

App\Providers\EasySmsServiceProvider::class,

App\Providers\JpushServiceProvider::class,
],

/*
Expand Down
6 changes: 6 additions & 0 deletions config/jpush.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php

return [
'key' => env('JPUSH_KEY'),
'secret' => env('JPUSH_SECRET'),
];
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class AddRegistrationIdToUsersTable extends Migration
{
public function up()
{
Schema::table('users', function (Blueprint $table) {
$table->string('registration_id')->nullable();
});
}

public function down()
{
Schema::table('users', function (Blueprint $table) {
$table->dropColumn('registration_id');
});
}
}

0 comments on commit 74330ce

Please sign in to comment.