diff --git a/.env.example b/.env.example index d166dcc..57e999d 100644 --- a/.env.example +++ b/.env.example @@ -53,4 +53,8 @@ WEIXIN_SECRET= JWT_SECRET= # 单位分钟 JWT_TTL=60 -JWT_REFRESH_TTL=20160 \ No newline at end of file +JWT_REFRESH_TTL=20160 + +#极光推送 +JPUSH_KEY= +JPUSH_SECRET= \ No newline at end of file diff --git a/app/Http/Controllers/Api/UsersController.php b/app/Http/Controllers/Api/UsersController.php index ff1388c..29d70b6 100644 --- a/app/Http/Controllers/Api/UsersController.php +++ b/app/Http/Controllers/Api/UsersController.php @@ -35,7 +35,7 @@ class UsersController extends Controller public function update(UserRequest $request) { $user = $this->user(); - $attributes = $request->only(['name', 'email', 'introduction']); + $attributes = $request->only(['name', 'email', 'introduction', 'registration_id']); if ($request->get('avatar_image_id')) { $image = Image::find($request['avatar_image_id']); $attributes['avatar'] = $image->path; diff --git a/app/Http/Requests/UserRequest.php b/app/Http/Requests/UserRequest.php index 60b28bc..406971d 100644 --- a/app/Http/Requests/UserRequest.php +++ b/app/Http/Requests/UserRequest.php @@ -54,6 +54,7 @@ class UserRequest extends FormRequest 'verification_key' => '短信验证码 key', 'verification_code' => '短信验证码', 'introduction' => '个人简介', + 'registration_id' => '设备id' ]; } diff --git a/app/Listeners/PushNotification.php b/app/Listeners/PushNotification.php new file mode 100644 index 0000000..f958084 --- /dev/null +++ b/app/Listeners/PushNotification.php @@ -0,0 +1,48 @@ +client = $client; + } + + /** + * Handle the event. + * + * @param DatabaseNotification $notification + * @return void + */ + public function handle(DatabaseNotification $notification) + { + if (app()->environment('local')) { + return; + } + + $user = $notification->notifiable; + + if (!$user->registration_id) { + return; + } + + // 推送消息 + $this->client->push() + ->setPlatform('all') + ->addRegistrationId($user->registration_id) + ->setNotificationAlert(strip_tags($notification->data['reply_content'])) + ->send(); + } +} diff --git a/app/Models/User.php b/app/Models/User.php index 7c9448a..8852b68 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -39,7 +39,7 @@ class User extends Authenticatable implements JWTSubject */ protected $fillable = [ 'name', 'phone', 'email', 'password', 'introduction', 'avatar ', - 'weixin_openid', 'weixin_unionid', + 'weixin_openid', 'weixin_unionid', 'registration_id' ]; /** diff --git a/app/Providers/EventServiceProvider.php b/app/Providers/EventServiceProvider.php index 77f92e5..3594d1a 100644 --- a/app/Providers/EventServiceProvider.php +++ b/app/Providers/EventServiceProvider.php @@ -20,6 +20,9 @@ class EventServiceProvider extends ServiceProvider // add your listeners (aka providers) here 'SocialiteProviders\Weixin\WeixinExtendSocialite@handle' ], + 'eloquent.created: Illuminate\Notifications\DatabaseNotification' => [ + 'App\Listeners\PushNotification', + ], ]; /** diff --git a/app/Providers/JpushServiceProvider.php b/app/Providers/JpushServiceProvider.php new file mode 100644 index 0000000..5b11f59 --- /dev/null +++ b/app/Providers/JpushServiceProvider.php @@ -0,0 +1,33 @@ +app->singleton(Client::class, function ($app) { + return new Client(config('jpush.key'), config('jpush.secret')); + }); + + $this->app->alias(Client::class, 'jpush'); + } +} diff --git a/composer.json b/composer.json index f61afcb..e28747a 100644 --- a/composer.json +++ b/composer.json @@ -16,6 +16,7 @@ "guzzlehttp/guzzle": "~6.3", "hieu-le/active": "~3.5", "intervention/image": "^2.4", + "jpush/jpush": "^3.6", "laravel/framework": "5.5.*", "laravel/horizon": "~1.0", "laravel/tinker": "~1.0", diff --git a/composer.lock b/composer.lock index 0b6301a..e4cfb61 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "32f207e4a0d2ec61b4710f12c6b34d6d", + "content-hash": "e838c10d6726e4e4ac83fcfec83bc9d7", "packages": [ { "name": "cakephp/chronos", @@ -1457,8 +1457,51 @@ "time": "2015-04-20T18:58:01+00:00" }, { + "name": "jpush/jpush", + "version": "v3.6.1", + "source": { + "type": "git", + "url": "https://github.com/jpush/jpush-api-php-client.git", + "reference": "a2250612a33ced6c3e565eb9807089b693111154" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/jpush/jpush-api-php-client/zipball/a2250612a33ced6c3e565eb9807089b693111154", + "reference": "a2250612a33ced6c3e565eb9807089b693111154", + "shasum": "" + }, + "require": { + "ext-curl": "*", + "php": ">=5.3.3" + }, + "require-dev": { + "phpunit/phpunit": "*" + }, + "type": "library", + "autoload": { + "psr-4": { + "JPush\\": "src/JPush/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "JPush", + "email": "support@jpush.cn", + "homepage": "https://www.jpush.cn/", + "role": "Developer" + } + ], + "description": "JPush API PHP Client", + "homepage": "https://github.com/jpush/jpush-api-php-client", + "time": "2018-05-30T02:28:58+00:00" + }, + { "name": "laravel/framework", - "version": "v5.5.40", + "version": "v5.5.40", "source": { "type": "git", "url": "https://github.com/laravel/framework.git", @@ -1588,7 +1631,7 @@ "framework", "laravel" ], - "time": "2018-03-30T13:29:30+00:00" + "time": "2018-03-30T13:29:30+00:00" }, { "name": "laravel/horizon", diff --git a/config/app.php b/config/app.php index 0b33c3e..037a9ac 100644 --- a/config/app.php +++ b/config/app.php @@ -178,7 +178,7 @@ return [ App\Providers\RouteServiceProvider::class, App\Providers\EasySmsServiceProvider::class, - + App\Providers\JpushServiceProvider::class ], /* diff --git a/config/jpush.php b/config/jpush.php new file mode 100644 index 0000000..dd11027 --- /dev/null +++ b/config/jpush.php @@ -0,0 +1,12 @@ + env('JPUSH_KEY'), + 'secret' => env('JPUSH_SECRET'), +]; \ No newline at end of file diff --git a/database/migrations/2018_06_16_093907_add_registration_id_to_users_table.php b/database/migrations/2018_06_16_093907_add_registration_id_to_users_table.php new file mode 100644 index 0000000..6373377 --- /dev/null +++ b/database/migrations/2018_06_16_093907_add_registration_id_to_users_table.php @@ -0,0 +1,32 @@ +string('registration_id')->nullable(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table('users', function (Blueprint $table) { + $table->dropColumn('registration_id'); + }); + } +}