rich2k / pusher-beams
Pusher Beams is a push notification service from Pusher.
Installs: 10 270
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 3
Forks: 11
Open Issues: 0
Requires
- php: >=7.1
- illuminate/events: ^6.0 || ^7.0 || ^8.0 || ^9.0 || ^10.0
- illuminate/notifications: ^6.0 || ^7.0 || ^8.0 || ^9.0 || ^10.0
- illuminate/queue: ^6.0 || ^7.0 || ^8.0 || ^9.0 || ^10.0
- illuminate/support: ^6.0 || ^7.0 || ^8.0 || ^9.0 || ^10.0
- pusher/pusher-push-notifications: ^2.0
Requires (Dev)
- mockery/mockery: ^0.9.5
- phpunit/phpunit: 7.*
README
This package makes it easy to send Pusher push notifications with Laravel (should work with other non-laravel PHP projects). It's based off this package by Mohamed Said.
This fork exists to allow us to run both the Pusher Beams and old Pusher Channels code side by side.
Contents
Installation
You can install the package via composer:
composer require rich2k/pusher-beams
You must install the service provider:
// config/app.php 'providers' => [ ... Rich2k\PusherBeams\PusherBeamsServiceProvider::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://dash.pusher.com/
- Select Beams from the side bar, and click Create from the right to create your Instance.
- Go to the settings tab (you can close the wizard)
- Upload your iOS .p8 auth key (they guides you through this), your iOS Team Id and/or your CM server key.
- Now select the Credentials tab.
- Copy your instanceId and SecretKey.
- Update the values in your
config/broadcasting.php
file under the pusher connection, see below. - You're now good to go.
Configuration
In config/broadcasting.php
'connections' => [ ... 'pusher' => [ 'beams' => [ 'secret_key' => env('PUSHER_BEAMS_SECRET'), 'instance_id' => env('PUSHER_BEAMS_INSTANCE_ID'), ], ], ],
Usage
Now you can use the channel in your via()
method inside the Notification
class.
use Rich2k\PusherBeams\PusherBeams; use Rich2k\PusherBeams\PusherBeamsMessage; use Illuminate\Notifications\Notification; class AccountApproved extends Notification { public function via($notifiable) { return [PusherBeams::class]; } public function toPusherBeamsNotification($notifiable) { return PusherBeamsMessage::create() ->iOS() ->badge(1) ->sound('success') ->body("Your {$notifiable->service} account was approved!"); } }
Available Message methods
platform('')
: Accepts a string value ofiOS
orAndroid
.iOS()
: Sets the platform value to iOS.android()
: Sets the platform value to Android.title('')
: Accepts a string value for the title.body('')
: Accepts a string value for the body.sound('')
: Accepts a string value for the notification sound file. Notice that if you leave blank the default sound value will bedefault
.icon('')
: Accepts a string value for the icon file. (Android Only)badge(1)
: Accepts an integer value for the badge. (iOS Only)setOption($key, $value)
: Allows you to set any value in the message payload. For more information check here for iOS, or here for Android.withiOS(PusherBeamsMessage $message)
: Set an extra message to be sent to iOSwithAndroid(PusherBeamsMessage $message)
: Set an extra message to be sent to Android
Sending to multiple platforms
You can send a single message to an iOS device and an Android device at the same time using the withiOS()
and withAndroid()
method:
use Rich2k\PusherBeams\PusherBeams; use Rich2k\PusherBeams\PusherBeamsMessage; use Illuminate\Notifications\Notification; class AccountApproved extends Notification { public function via($notifiable) { return [PusherBeams::class]; } public function toPusherBeamsNotification($notifiable) { return PusherBeamsMessage::create() ->android() ->sound('success') ->body("Your {$notifiable->service} account was approved!") ->withiOS(PusherBeamsMessage::create() ->body("Your {$notifiable->service} account was approved!") ->badge(1) ->sound('success') ); } }
Routing a message
By default the Pusher beams "interest" messages will be sent to will be defined using the {notifiable}.{id} convention, for example App.User.1
, however you can change this behaviour by including a routeNotificationForPusherPushNotifications()
in the notifiable class method that returns the interest name.
Whatever interest you set in the app is the interest you should register for within your mobil
Testing
$ composer test
Security
If you discover any security related issues, please email rhyland@gmail.com instead of using the issue tracker.
Contributing
Please see CONTRIBUTING for details.
Credits
- Richard Hyland
- Neo Ighodaro
- Mohamed Said
- All Contributors
License
The MIT License (MIT). Please see License File for more information.