bnbwebexpertise / laravel-push-notifications
Apple and Google Push Notification
Installs: 5 873
Dependents: 0
Suggesters: 0
Security: 0
Stars: 5
Watchers: 5
Forks: 5
Open Issues: 1
Requires
- php: >=5.4
- duccio/apns-php: ^1.0
- illuminate/support: 5.*
Requires (Dev)
- laravel/lumen-framework: 5.*
- phpunit/phpunit: >=4.1
README
Use Firebase Cloud Messaging
Installation
For Laravel 5.4 or older, add the provider in your config/app.php
:
'providers' => [ Bnb\PushNotifications\PushNotificationsServiceProvider::class, ],
Laravel 5.5 use Composer auto-discovery feature.
Configuration
Configuration via environment
To return the APNs payloads in the results array (debugging purpose) :
PUSH_RETURN_PAYLOADS=true
Apple Push Notification Service
Put your APNs certificate somewhere inside your project path. Add the relative path to your certificate in your .env
file :
PUSH_APNS_CERTIFICATE=config/push/certificate.pem
If your certificate is secured with a password you can specify it in the .env
file as :
PUSH_APNS_PASSWORD=changeme
You can also set the environnement to use (default to production
) :
PUSH_APNS_ENVIRONMENT=development
Google Cloud Messaging
Grap you API key from the Google Developer Console and add it to your .env
file :
PUSH_GCM_KEY=AIaeRtYiUoP-QsDfghQJK1lMWXCvBN23AZE4RT6u
Configuration via PHP
If you prefer to configure the module from a config
PHP file, publish it :
php artisan vendor:publish --provider=Bnb\\PushNotifications\\PushNotificationsServiceProvider --tag=config
Then set the desired configuration values :
<?php return [ 'apns' => [ 'environment' => 'production', 'certificate' => __DIR__ . '/push/certificate.pem', 'password' => 'changeme', ], 'gcm' => [ 'key' => 'AIaeRtYiUoP-QsDfghQJK1lMWXCvBN23AZE4RT6u', ], // the size of the chunk batch loop 'chunk' => 100, // set to true to return the APNs payloads in the results array 'payloads' => false, ];
Configuration at runtime
The GCM and APNs configuration can be changed at runtime via the setGcmOption($key, $value)
and setApnsOption($key, $value)
methods of the Notification
class.
GCM options
APNs options
Usage
A notification message holds the following properties where each one can be overridden by the device definition :
$notification = new Notification('title', 'message'); $notification ->badge(5) ->sound('sound') ->ttl(1234) ->metadata('key1', 'value1') ->metadata('key2', 'value2');
$device = Device::apns('a-token', 'a-unique-local-id'); $device ->title('deviceTitle') ->message('deviceMessage') ->badge(10) ->sound('deviceSound') ->ttl(4321) ->metadata('key1', 'deviceValue1') ->metadata('deviceKey2', 'value2');
Metadata
For APNs the props
custom property holds the list of the metadata keys including title
and message
.
For GCM the metadata
are bound to the appdata
object.
Example
$notification = new \Bnb\PushNotifications\Notification('Hello World !', 'This is a test message'); $notification->metadata('custom-id', 1234); $notification->push(\Bnb\PushNotifications\Device::gcm('test-token')->badge(3)->metadata('device-key','demoGcm')); $notification->push(\Bnb\PushNotifications\Device::apns('test-token')->badge(2)->metadata('device-key','demoApns')); $results = $notification->send(); // $results['errors'] // Contains the list of failed devices // $results['updates'] // Contains the list of updated token devices (GCM) // $results['payloads'] // Contains the messages payloads (APNs only) if config('push.payloads') is set to true foreach($results['errors'] as $data) { DbDevice::where('token', $data->token) ->delete(); } foreach($results['updates'] as $data) { DbDevice::where('token', $data['device']->token) ->update(['token' => $data['token']]); }
Commands
You can use the following Artisan command lines to send test messages :
Send to Android devices
php artisan push:gcm [options] [--] <token> <message> [<title>]
Arguments:
token the device token
message the notification message
title (optional) the notification title
Options:
--sender-id[=SENDER-ID] The GCM sender ID
Example :
php artisan push:gcm "ebizwJXzS7o:APA91bEa6tnBa-ZTkSf0fnsGNvU1BLdMnSi09GQ6BkFp-p99wSyVqb0f1nZpE3UEb-w3TzlrwhRGG1YQC0SV9N4DwO17RdceUX77ahAYtWcpFMgC4Xnc3NSkQ9PSqYfeFRPDL6D_KORM" "This is a test message"
Send to iOS devices
php artisan push:apns [options] [--] <token> <message> [<title>]
Arguments:
token the device token
message the notification message
title (optional) the notification title
Options:
--certificate[=CERTIFICATE] The Apple certificate path
--password[=PASSWORD] The Apple certificate password
--environment[=ENVIRONMENT] The Apple push environment (production or development)
Example :
php artisan push:apns "3c1c1c88428aeec68525a3e3d23c632bfef8c076c45e3af6769501b4ba493b1b" "This is a test message" "Hello World"