Skip to content

real support of laravel 5.6 #27

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
language: php

php:
- 5.6
- 7.0
- 7.1

Expand Down
41 changes: 22 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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

Expand All @@ -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.
Expand Down Expand Up @@ -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!";
Expand Down Expand Up @@ -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
Expand Down
28 changes: 20 additions & 8 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,31 @@
"name": "Sebastian De Deyne",
"email": "[email protected]",
"homepage": "https://sebastiandedeyne.com"
},
{
"name": "Hanlin Wang",
"email": "[email protected]",
"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": {
Expand Down
28 changes: 16 additions & 12 deletions src/PusherChannel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
Expand All @@ -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')
);
}
}
Expand Down
5 changes: 3 additions & 2 deletions src/PusherMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ public function withiOS(PusherMessage $message)
*
* @param \NotificationChannels\PusherPushNotifications\PusherMessage $message
* @return void
* @throws CouldNotCreateMessage
*/
private function withExtra(PusherMessage $message)
{
Expand Down Expand Up @@ -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
],
],
];
Expand Down
19 changes: 9 additions & 10 deletions src/PusherPushNotificationsServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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'],
]);
});
}
}
17 changes: 11 additions & 6 deletions tests/ChannelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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);
}
Expand All @@ -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));

Expand Down
22 changes: 11 additions & 11 deletions tests/MessageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 */
Expand All @@ -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'));
}

Expand All @@ -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 */
Expand All @@ -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 */
Expand All @@ -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 */
Expand All @@ -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');
}
Expand All @@ -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'));
}
}