Skip to content

Commit b1ea0f6

Browse files
jessarcheratymic
authored andcommitted
Update to Pusher Beams (#42)
* Update to latest phpunit and mockery * Fix test expectations * Linting * Test with PHP 7.4 * Remove unnecessary test tear down * Switch to Beams SDK * Automatically register service provider * Update README * Remove empty values from payload * Update README to reflect the change to Pusher Beams * Rename channel class back to PusherChannel * Update Docblock
1 parent 330865c commit b1ea0f6

File tree

7 files changed

+71
-62
lines changed

7 files changed

+71
-62
lines changed

README.md

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Pusher push notifications channel for Laravel 5.5+ & 6.0
1+
# Pusher Beams push notifications channel for Laravel 5.5+ & 6.0
22

33
[![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)
44
[![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE.md)
@@ -9,7 +9,11 @@
99
[![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)
1010
[![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)
1111

12-
This package makes it easy to send [Pusher push notifications](https://pusher.com/docs/push_notifications) with Laravel.
12+
This package makes it easy to send [Pusher Beams push notifications](https://pusher.com/docs/beams) with Laravel.
13+
14+
Please note that this notification channel should not be confused with Pusher Channels.
15+
16+
Also please note that prior to version 2.0, this package integrated with Pusher's beta push notifications service that was part of Pusher Channels. Please see Pusher's [migration guide](https://www.pusher.com/docs/channels/push_notifications/migration-guide) for more information.
1317

1418
## Contents
1519

@@ -33,27 +37,24 @@ You can install the package via composer:
3337
composer require laravel-notification-channels/pusher-push-notifications
3438
```
3539

36-
You must install the service provider:
37-
38-
```php
39-
// config/app.php
40-
'providers' => [
41-
...
42-
NotificationChannels\PusherPushNotifications\PusherPushNotificationsServiceProvider::class,
43-
],
44-
```
45-
4640
### Setting up your Pusher account
4741

48-
Before using this package you should set up a Pusher account. Here are the steps required.
49-
50-
- Login to https://dashboard.pusher.com/
51-
- Select your app from the sidebar or create a new app.
52-
- Click on the "Push Notifications" tab.
53-
- Upload your APNS Certificate or add your GCM API key.
54-
- Now select the "App Keys" tab.
55-
- Copy your `app_id`, `key`, and `secret`.
56-
- Update the values in your `config/broadcasting.php` file under the pusher connection.
42+
Before using this package you should set up a Pusher Beams account. Here are the steps required.
43+
44+
- Login to https://dash.pusher.com/
45+
- Select the "Beams" product.
46+
- Select your instance from the list or create a new instance.
47+
- Click on the "Settings" tab.
48+
- Upload your APNS Certificate and/or add your FCM Server key.
49+
- Now select the "Credentials" tab.
50+
- Copy your `Instance Id`, and `Secret Key`.
51+
- Add a new entry to in your `config/services.php` file:
52+
```php
53+
'pusher' => [
54+
'beams_instance_id' => 'Your Instance Id',
55+
'beams_secret_key' => 'Your Secret Key',
56+
],
57+
```
5758
- You're now good to go.
5859

5960
## Usage
@@ -93,7 +94,7 @@ class AccountApproved extends Notification
9394
- `sound('')`: Accepts a string value for the notification sound file. Notice that if you leave blank the default sound value will be `default`.
9495
- `icon('')`: Accepts a string value for the icon file. (Android Only)
9596
- `badge(1)`: Accepts an integer value for the badge. (iOS Only)
96-
- `setOption($key, $value)`: Allows you to set any value in the message payload. For more information [check here for iOS](https://pusher.com/docs/push_notifications/ios/server), [or here for Android](https://pusher.com/docs/push_notifications/android/server).
97+
- `setOption($key, $value)`: Allows you to set any value in the message payload. See the [request body section of the Pusher Beam docs](https://pusher.com/docs/beams/reference/publish-api#request-body) for more information.
9798

9899
### Sending to multiple platforms
99100

composer.json

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
"illuminate/notifications": "~5.5 || ~6.0",
3838
"illuminate/queue": "~5.5 || ~6.0",
3939
"illuminate/support": "~5.5 || ~6.0",
40-
"pusher/pusher-php-server": "~3.0 || ~4.0"
40+
"pusher/pusher-push-notifications": "^1.1"
4141
},
4242
"require-dev": {
4343
"mockery/mockery": "^1.3",
@@ -58,5 +58,12 @@
5858
},
5959
"config": {
6060
"sort-packages": true
61+
},
62+
"extra": {
63+
"laravel": {
64+
"providers": [
65+
"NotificationChannels\\PusherPushNotifications\\PusherPushNotificationsServiceProvider"
66+
]
67+
}
6168
}
6269
}

src/PusherChannel.php

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,28 @@
55
use Illuminate\Events\Dispatcher;
66
use Illuminate\Notifications\Events\NotificationFailed;
77
use Illuminate\Notifications\Notification;
8-
use Pusher\Pusher;
8+
use Pusher\PushNotifications\PushNotifications;
9+
use Throwable;
910

1011
class PusherChannel
1112
{
1213
/**
13-
* @var Pusher
14+
* @var PushNotifications
1415
*/
15-
protected $pusher;
16+
protected $beamsClient;
1617

1718
/**
1819
* @var \Illuminate\Events\Dispatcher
1920
*/
2021
private $events;
2122

2223
/**
23-
* @param Pusher $pusher
24+
* @param PushNotifications $beamsClient
25+
* @param Dispatcher $events
2426
*/
25-
public function __construct(Pusher $pusher, Dispatcher $events)
27+
public function __construct(PushNotifications $beamsClient, Dispatcher $events)
2628
{
27-
$this->pusher = $pusher;
29+
$this->beamsClient = $beamsClient;
2830
$this->events = $events;
2931
}
3032

@@ -41,15 +43,14 @@ public function send($notifiable, Notification $notification)
4143
$interest = $notifiable->routeNotificationFor('PusherPushNotifications')
4244
?: $this->interestName($notifiable);
4345

44-
$response = $this->pusher->notify(
45-
is_array($interest) ? $interest : [$interest],
46-
$notification->toPushNotification($notifiable)->toArray(),
47-
true
48-
);
49-
50-
if (! in_array($response['status'], [200, 202])) {
51-
$this->events->fire(
52-
new NotificationFailed($notifiable, $notification, 'pusher-push-notifications', $response)
46+
try {
47+
$this->beamsClient->publishToInterests(
48+
is_array($interest) ? $interest : [$interest],
49+
$notification->toPushNotification($notifiable)->toArray()
50+
);
51+
} catch (Throwable $exception) {
52+
$this->events->dispatch(
53+
new NotificationFailed($notifiable, $notification, 'pusher-push-notifications')
5354
);
5455
}
5556
}

src/PusherMessage.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -294,13 +294,13 @@ public function toiOS()
294294
public function toAndroid()
295295
{
296296
$message = [
297-
'gcm' => [
298-
'notification' => [
297+
'fcm' => [
298+
'notification' => array_filter([
299299
'title' => $this->title,
300300
'body' => $this->body,
301301
'sound' => $this->sound,
302302
'icon' => $this->icon,
303-
],
303+
]),
304304
],
305305
];
306306

src/PusherPushNotificationsServiceProvider.php

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
namespace NotificationChannels\PusherPushNotifications;
44

55
use Illuminate\Support\ServiceProvider;
6-
use Pusher\Pusher;
6+
use Pusher\PushNotifications\PushNotifications;
77

88
class PusherPushNotificationsServiceProvider extends ServiceProvider
99
{
@@ -13,15 +13,14 @@ class PusherPushNotificationsServiceProvider extends ServiceProvider
1313
public function boot()
1414
{
1515
$this->app->when(PusherChannel::class)
16-
->needs(Pusher::class)
16+
->needs(PushNotifications::class)
1717
->give(function () {
18-
$pusherConfig = config('broadcasting.connections.pusher');
18+
$config = config('services.pusher');
1919

20-
return new Pusher(
21-
$pusherConfig['key'],
22-
$pusherConfig['secret'],
23-
$pusherConfig['app_id']
24-
);
20+
return new PushNotifications([
21+
'instanceId' => $config['beams_instance_id'],
22+
'secretKey' => $config['beams_secret_key'],
23+
]);
2524
});
2625
}
2726
}

tests/ChannelTest.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace NotificationChannels\PusherPushNotifications\Test;
44

5+
use Exception;
56
use Illuminate\Events\Dispatcher;
67
use Illuminate\Notifications\Events\NotificationFailed;
78
use Illuminate\Notifications\Notifiable;
@@ -10,13 +11,13 @@
1011
use Mockery\Adapter\Phpunit\MockeryTestCase;
1112
use NotificationChannels\PusherPushNotifications\PusherChannel;
1213
use NotificationChannels\PusherPushNotifications\PusherMessage;
13-
use Pusher\Pusher;
14+
use Pusher\PushNotifications\PushNotifications;
1415

1516
class ChannelTest extends MockeryTestCase
1617
{
1718
public function setUp(): void
1819
{
19-
$this->pusher = Mockery::mock(Pusher::class);
20+
$this->pusher = Mockery::mock(PushNotifications::class);
2021

2122
$this->events = Mockery::mock(Dispatcher::class);
2223

@@ -34,7 +35,7 @@ public function it_can_send_a_notification()
3435

3536
$data = $message->toArray();
3637

37-
$this->pusher->shouldReceive('notify')->with(['interest_name'], $data, true)->andReturn(['status' => 202]);
38+
$this->pusher->shouldReceive('publishToInterests')->once()->with(['interest_name'], $data);
3839

3940
$this->channel->send($this->notifiable, $this->notification);
4041
}
@@ -46,9 +47,9 @@ public function it_fires_failure_event_on_failure()
4647

4748
$data = $message->toArray();
4849

49-
$this->pusher->shouldReceive('notify')->with(['interest_name'], $data, true)->andReturn(['status' => 500]);
50+
$this->pusher->shouldReceive('publishToInterests')->once()->with(['interest_name'], $data)->andThrow(new Exception('Something happened'));
5051

51-
$this->events->shouldReceive('fire')->with(Mockery::type(NotificationFailed::class));
52+
$this->events->shouldReceive('dispatch')->once()->with(Mockery::type(NotificationFailed::class));
5253

5354
$this->channel->send($this->notifiable, $this->notification);
5455
}

tests/MessageTest.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public function it_provides_a_create_method()
3939
public function by_default_it_will_send_a_message_to_ios()
4040
{
4141
$this->assertTrue(Arr::has($this->message->toArray(), 'apns'));
42-
$this->assertFalse(Arr::has($this->message->toArray(), 'gcm'));
42+
$this->assertFalse(Arr::has($this->message->toArray(), 'fcm'));
4343
}
4444

4545
/** @test */
@@ -48,11 +48,11 @@ public function it_can_send_a_message_to_the_right_platform()
4848
$this->message->ios();
4949

5050
$this->assertTrue(Arr::has($this->message->toArray(), 'apns'));
51-
$this->assertFalse(Arr::has($this->message->toArray(), 'gcm'));
51+
$this->assertFalse(Arr::has($this->message->toArray(), 'fcm'));
5252

5353
$this->message->android();
5454

55-
$this->assertTrue(Arr::has($this->message->toArray(), 'gcm'));
55+
$this->assertTrue(Arr::has($this->message->toArray(), 'fcm'));
5656
$this->assertFalse(Arr::has($this->message->toArray(), 'apns'));
5757
}
5858

@@ -69,7 +69,7 @@ public function it_can_set_the_title()
6969

7070
$this->assertEquals('myTitle', Arr::get($this->message->toiOS(), 'apns.aps.alert.title'));
7171

72-
$this->assertEquals('myTitle', Arr::get($this->message->toAndroid(), 'gcm.notification.title'));
72+
$this->assertEquals('myTitle', Arr::get($this->message->toAndroid(), 'fcm.notification.title'));
7373
}
7474

7575
/** @test */
@@ -79,7 +79,7 @@ public function it_can_set_the_body()
7979

8080
$this->assertEquals('myBody', Arr::get($this->message->toiOS(), 'apns.aps.alert.body'));
8181

82-
$this->assertEquals('myBody', Arr::get($this->message->toAndroid(), 'gcm.notification.body'));
82+
$this->assertEquals('myBody', Arr::get($this->message->toAndroid(), 'fcm.notification.body'));
8383
}
8484

8585
/** @test */
@@ -89,7 +89,7 @@ public function it_can_set_the_sound()
8989

9090
$this->assertEquals('mySound', Arr::get($this->message->toiOS(), 'apns.aps.sound'));
9191

92-
$this->assertEquals('mySound', Arr::get($this->message->toAndroid(), 'gcm.notification.sound'));
92+
$this->assertEquals('mySound', Arr::get($this->message->toAndroid(), 'fcm.notification.sound'));
9393
}
9494

9595
/** @test */
@@ -105,7 +105,7 @@ public function it_can_set_the_icon()
105105
{
106106
$this->message->icon('myIcon');
107107

108-
$this->assertEquals('myIcon', Arr::get($this->message->toAndroid(), 'gcm.notification.icon'));
108+
$this->assertEquals('myIcon', Arr::get($this->message->toAndroid(), 'fcm.notification.icon'));
109109
}
110110

111111
/** @test */
@@ -122,6 +122,6 @@ public function it_can_send_message_to_multiple_platforms()
122122
$this->message->ios()->withAndroid(new PusherMessage());
123123

124124
$this->assertTrue(Arr::has($this->message->toArray(), 'apns'));
125-
$this->assertTrue(Arr::has($this->message->toArray(), 'gcm'));
125+
$this->assertTrue(Arr::has($this->message->toArray(), 'fcm'));
126126
}
127127
}

0 commit comments

Comments
 (0)