diff --git a/.travis.yml b/.travis.yml index ff42c2c..1aed6ed 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,7 +1,6 @@ language: php php: - - 5.6 - 7.0 - 7.1 diff --git a/README.md b/README.md index 5a22763..2cb1599 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# Pusher push notifications channel for Laravel 5.3 +# Pusher push notifications channel for Laravel 5.6 [![Latest Version on Packagist](https://img.shields.io/packagist/v/laravel-notification-channels/pusher-push-notifications.svg?style=flat-square)](https://packagist.org/packages/laravel-notification-channels/pusher-push-notifications) [![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE.md) @@ -9,7 +9,7 @@ [![Code Coverage](https://img.shields.io/scrutinizer/coverage/g/laravel-notification-channels/pusher-push-notifications/master.svg?style=flat-square)](https://scrutinizer-ci.com/g/laravel-notification-channels/pusher-push-notifications/?branch=master) [![Total Downloads](https://img.shields.io/packagist/dt/laravel-notification-channels/pusher-push-notifications.svg?style=flat-square)](https://packagist.org/packages/laravel-notification-channels/pusher-push-notifications) -This package makes it easy to send [Pusher push notifications](https://pusher.com/docs/push_notifications) with Laravel 5.3. +This package makes it easy to send [Pusher push notifications](https://docs.pusher.com/push-notifications) with Laravel 5.5 and 5.6. If you are using Laravel 5.3 or 5.4, use 1.x branch. Since pusher dropped the support of API key and secret for push notification on new projects created on https://dashboard.pusher.com in favor of new https://dash.pusher.com, this package only support the latest version of pusher push notifications. ## Contents @@ -33,29 +33,31 @@ You can install the package via composer: composer require laravel-notification-channels/pusher-push-notifications ``` -You must install the service provider: - -```php -// config/app.php -'providers' => [ - ... - NotificationChannels\PusherPushNotifications\PusherPushNotificationsServiceProvider::class, -], -``` - ### Setting up your Pusher account Before using this package you should set up a Pusher account. Here are the steps required. -- Login to https://dashboard.pusher.com/ -- Select your app from the sidebar or create a new app. -- Click on the "Push Notifications" tab. +- Login to https://dash.pusher.com/ +- Add a new instance in Push Notifications service. - Upload your APNS Certificate or add your GCM API key. -- Now select the "App Keys" tab. -- Copy your `app_id`, `key`, and `secret`. -- Update the values in your `config/broadcasting.php` file under the pusher connection. +- Now select your instance in left navigation area. +- Copy your `instance_id`, and `secret`. +- Update the values in your `config/broadcasting.php` file under the pusher connection with name `push_notification_instance_id` and `push_notification_secret` - You're now good to go. +### Config + +In `config/broadcasting.php`, it should looks like this + +``` php + 'connections' => [ + 'pusher' => [ + 'push_notification_instance_id' => 'YOUR INSTANCE ID HERE', // Looks like a uuid + 'push_notification_secret' => "YOUR SECRET HERE", // 31 char string + ], + ], +``` + ## Usage Now you can use the channel in your `via()` method inside the Notification class. @@ -99,7 +101,7 @@ class AccountApproved extends Notification You can send a single message to an iOS device and an Android device at the same time using the `withiOS()` and `withAndroid()` method: -```php +``` php public function toPushNotification($notifiable) { $message = "Your {$notifiable->service} account was approved!"; @@ -147,6 +149,7 @@ Please see [CONTRIBUTING](CONTRIBUTING.md) for details. - [Marcel Pociot](https://github.com/mpociot) - [Freek Van der Herten](https://github.com/freekmurze) - [Sebastian De Deyne](https://github.com/sebastiandedeyne) +- [Hanlin Wang](https://github.com/wanghanlin) - [All Contributors](../../contributors) ## License diff --git a/composer.json b/composer.json index ebfc3a1..371095b 100644 --- a/composer.json +++ b/composer.json @@ -29,19 +29,31 @@ "name": "Sebastian De Deyne", "email": "sebastian@spatie.be", "homepage": "https://sebastiandedeyne.com" + }, + { + "name": "Hanlin Wang", + "email": "605231181@qq.com", + "homepage": "https://wanghanlin.com" } ], "require": { - "php": ">=5.6.4", - "illuminate/events": "5.3.* || 5.4.* || 5.5.* || 5.6.*", - "illuminate/notifications": "5.3.* || 5.4.* || 5.5.* || 5.6.*", - "illuminate/queue": "5.3.* || 5.4.* || 5.5.* || 5.6.*", - "illuminate/support": "5.3.* || 5.4.* || 5.5.* || 5.6.*", - "pusher/pusher-php-server": "2.6.*" + "php": ">=7.0", + "illuminate/events": "5.5.* || 5.6.*", + "illuminate/notifications": "5.5.* || 5.6.*", + "illuminate/queue": "5.5.* || 5.6.*", + "illuminate/support": "5.5.* || 5.6.*", + "pusher/pusher-push-notifications": "^0.10" }, "require-dev": { - "mockery/mockery": "^0.9.5", - "phpunit/phpunit": "4.*" + "mockery/mockery": "^1.0", + "phpunit/phpunit": "6.* || 7.*" + }, + "extra": { + "laravel": { + "providers": [ + "NotificationChannels\\PusherPushNotifications\\PusherPushNotificationsServiceProvider" + ] + } }, "autoload": { "psr-4": { diff --git a/src/PusherChannel.php b/src/PusherChannel.php index 6772232..e9ce9ee 100644 --- a/src/PusherChannel.php +++ b/src/PusherChannel.php @@ -2,15 +2,15 @@ namespace NotificationChannels\PusherPushNotifications; -use Pusher; use Illuminate\Events\Dispatcher; use Illuminate\Notifications\Notification; use Illuminate\Notifications\Events\NotificationFailed; +use Pusher\PushNotifications\PushNotifications; class PusherChannel { /** - * @var \Pusher + * @var \Pusher\PushNotifications\PushNotifications $pusher */ protected $pusher; @@ -20,9 +20,10 @@ class PusherChannel private $events; /** - * @param \Pusher $pusher + * @param \Pusher\PushNotifications\PushNotifications $pusher + * @param \Illuminate\Events\Dispatcher */ - public function __construct(Pusher $pusher, Dispatcher $events) + public function __construct(PushNotifications $pusher, Dispatcher $events) { $this->pusher = $pusher; $this->events = $events; @@ -40,16 +41,19 @@ public function send($notifiable, Notification $notification) { $interest = $notifiable->routeNotificationFor('PusherPushNotifications') ?: $this->interestName($notifiable); + + if(is_string($interest)) { + $interest = [$interest]; + } - $response = $this->pusher->notify( - $interest, - $notification->toPushNotification($notifiable)->toArray(), - true - ); - - if (! in_array($response['status'], [200, 202])) { + try { + $response = $this->pusher->publish( + $interest, + $notification->toPushNotification($notifiable)->toArray() + ); + }catch (\Exception $e) { $this->events->fire( - new NotificationFailed($notifiable, $notification, 'pusher-push-notifications', $response) + new NotificationFailed($notifiable, $notification, 'pusher-push-notifications') ); } } diff --git a/src/PusherMessage.php b/src/PusherMessage.php index 4440051..a1065b9 100644 --- a/src/PusherMessage.php +++ b/src/PusherMessage.php @@ -156,6 +156,7 @@ public function withiOS(PusherMessage $message) * * @param \NotificationChannels\PusherPushNotifications\PusherMessage $message * @return void + * @throws CouldNotCreateMessage */ private function withExtra(PusherMessage $message) { @@ -294,12 +295,12 @@ public function toiOS() public function toAndroid() { $message = [ - 'gcm' => [ + 'fcm' => [ 'notification' => [ 'title' => $this->title, 'body' => $this->body, 'sound' => $this->sound, - 'icon' => $this->icon, + 'icon' => $this->icon ?: 'icon', // without icon pusher api will return invalid json format error ], ], ]; diff --git a/src/PusherPushNotificationsServiceProvider.php b/src/PusherPushNotificationsServiceProvider.php index c67f170..75ab586 100644 --- a/src/PusherPushNotificationsServiceProvider.php +++ b/src/PusherPushNotificationsServiceProvider.php @@ -3,7 +3,8 @@ namespace NotificationChannels\PusherPushNotifications; use Illuminate\Support\ServiceProvider; -use Pusher; +use Illuminate\Broadcasting\BroadcastManager; +use Pusher\PushNotifications\PushNotifications; class PusherPushNotificationsServiceProvider extends ServiceProvider { @@ -13,15 +14,13 @@ class PusherPushNotificationsServiceProvider extends ServiceProvider public function boot() { $this->app->when(PusherChannel::class) - ->needs(Pusher::class) - ->give(function () { - $pusherConfig = config('broadcasting.connections.pusher'); - - return new Pusher( - $pusherConfig['key'], - $pusherConfig['secret'], - $pusherConfig['app_id'] - ); + ->needs(PushNotifications::class) + ->give(function() { + $config = config('broadcasting.connections.pusher'); + return new PushNotifications([ + "instanceId" => $config['push_notification_instance_id'], + "secretKey" => $config['push_notification_secret'], + ]); }); } } diff --git a/tests/ChannelTest.php b/tests/ChannelTest.php index 1402863..f2899c2 100644 --- a/tests/ChannelTest.php +++ b/tests/ChannelTest.php @@ -8,15 +8,17 @@ use NotificationChannels\PusherPushNotifications\PusherChannel; use Illuminate\Notifications\Notification; use NotificationChannels\PusherPushNotifications\PusherMessage; -use PHPUnit_Framework_TestCase; use Mockery; -use Pusher; +use PHPUnit\Framework\TestCase; +use Pusher\PushNotifications\PushNotifications; -class ChannelTest extends PHPUnit_Framework_TestCase +class ChannelTest extends TestCase { + use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration; + public function setUp() { - $this->pusher = Mockery::mock(Pusher::class); + $this->pusher = Mockery::mock(PushNotifications::class); $this->events = Mockery::mock(Dispatcher::class); @@ -41,7 +43,10 @@ public function it_can_send_a_notification() $data = $message->toArray(); - $this->pusher->shouldReceive('notify')->with('interest_name', $data, true)->andReturn(['status' => 202]); + $mockResponse = new \stdClass(); + $mockResponse->publishId = 'fake-id'; + + $this->pusher->shouldReceive('publish')->with(['interest_name'], $data)->andReturn($mockResponse); $this->channel->send($this->notifiable, $this->notification); } @@ -53,7 +58,7 @@ public function it_fires_failure_event_on_failure() $data = $message->toArray(); - $this->pusher->shouldReceive('notify')->with('interest_name', $data, true)->andReturn(['status' => 500]); + $this->pusher->shouldReceive('publish')->with(['interest_name'], $data)->andThrow(new \Exception); $this->events->shouldReceive('fire')->with(Mockery::type(NotificationFailed::class)); diff --git a/tests/MessageTest.php b/tests/MessageTest.php index 1346a64..2c7e4c3 100644 --- a/tests/MessageTest.php +++ b/tests/MessageTest.php @@ -5,9 +5,9 @@ use Illuminate\Support\Arr; use NotificationChannels\PusherPushNotifications\Exceptions\CouldNotCreateMessage; use NotificationChannels\PusherPushNotifications\PusherMessage; -use PHPUnit_Framework_TestCase; +use PHPUnit\Framework\TestCase; -class MessageTest extends PHPUnit_Framework_TestCase +class MessageTest extends TestCase { /** @var \NotificationChannels\PusherPushNotifications\PusherMessage */ protected $message; @@ -39,7 +39,7 @@ public function it_provides_a_create_method() public function by_default_it_will_send_a_message_to_ios() { $this->assertTrue(Arr::has($this->message->toArray(), 'apns')); - $this->assertFalse(Arr::has($this->message->toArray(), 'gcm')); + $this->assertFalse(Arr::has($this->message->toArray(), 'fcm')); } /** @test */ @@ -48,11 +48,11 @@ public function it_can_send_a_message_to_the_right_platform() $this->message->ios(); $this->assertTrue(Arr::has($this->message->toArray(), 'apns')); - $this->assertFalse(Arr::has($this->message->toArray(), 'gcm')); + $this->assertFalse(Arr::has($this->message->toArray(), 'fcm')); $this->message->android(); - $this->assertTrue(Arr::has($this->message->toArray(), 'gcm')); + $this->assertTrue(Arr::has($this->message->toArray(), 'fcm')); $this->assertFalse(Arr::has($this->message->toArray(), 'apns')); } @@ -69,7 +69,7 @@ public function it_can_set_the_title() $this->assertEquals('myTitle', Arr::get($this->message->toiOS(), 'apns.aps.alert.title')); - $this->assertEquals('myTitle', Arr::get($this->message->toAndroid(), 'gcm.notification.title')); + $this->assertEquals('myTitle', Arr::get($this->message->toAndroid(), 'fcm.notification.title')); } /** @test */ @@ -79,7 +79,7 @@ public function it_can_set_the_body() $this->assertEquals('myBody', Arr::get($this->message->toiOS(), 'apns.aps.alert.body')); - $this->assertEquals('myBody', Arr::get($this->message->toAndroid(), 'gcm.notification.body')); + $this->assertEquals('myBody', Arr::get($this->message->toAndroid(), 'fcm.notification.body')); } /** @test */ @@ -89,7 +89,7 @@ public function it_can_set_the_sound() $this->assertEquals('mySound', Arr::get($this->message->toiOS(), 'apns.aps.sound')); - $this->assertEquals('mySound', Arr::get($this->message->toAndroid(), 'gcm.notification.sound')); + $this->assertEquals('mySound', Arr::get($this->message->toAndroid(), 'fcm.notification.sound')); } /** @test */ @@ -105,13 +105,13 @@ public function it_can_set_the_icon() { $this->message->icon('myIcon'); - $this->assertEquals('myIcon', Arr::get($this->message->toAndroid(), 'gcm.notification.icon')); + $this->assertEquals('myIcon', Arr::get($this->message->toAndroid(), 'fcm.notification.icon')); } /** @test */ public function it_will_throw_an_exception_when_an_unsupported_platform_is_used() { - $this->setExpectedException(CouldNotCreateMessage::class); + $this->expectException(CouldNotCreateMessage::class); $this->message->platform('bla bla'); } @@ -122,6 +122,6 @@ public function it_can_send_message_to_multiple_platforms() $this->message->ios()->withAndroid(new PusherMessage()); $this->assertTrue(Arr::has($this->message->toArray(), 'apns')); - $this->assertTrue(Arr::has($this->message->toArray(), 'gcm')); + $this->assertTrue(Arr::has($this->message->toArray(), 'fcm')); } }