2017 © Pedro Peláez
 

library push-notification

Laravel 5 Package to send push notifications to Android and IOS devices. (GCM,FCM,APN)

image

edujugon/push-notification

Laravel 5 Package to send push notifications to Android and IOS devices. (GCM,FCM,APN)

  • Friday, June 29, 2018
  • by edujugon
  • Repository
  • 16 Watchers
  • 176 Stars
  • 47,388 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 34 Forks
  • 3 Open issues
  • 23 Versions
  • 21 % Grown

The README.md

PushNotification Package

Build Status Total Downloads Latest Stable Version License, (*1)

This is an easy to use package to send push notification., (*2)

Push Service Providers Available:

  • GCM
  • FCM
  • APN

Installation

Laravel version below 5.8

type in console:, (*3)

composer require "edujugon/push-notification:^v3.0.0"

Laravel 5.8/6 and higher

type in console:, (*4)

composer require edujugon/push-notification

The package will automatically register its service provider., (*5)

Publish the package's configuration file to the application's own config directory, (*6)

php artisan vendor:publish --provider="Edujugon\PushNotification\Providers\PushNotificationServiceProvider" --tag="config"

Go to laravel facade sample directly., (*7)

Configuration

After publishing the configuration, you can find the Push service config in config/pushnotification.php, (*8)

The default configuration parameters for GCM and FCM are :, (*9)

  • priority => normal
  • dry_run => false
  • apiKey => Your ApiKey

You can dynamically update those values or adding new ones calling the method setConfig like so:, (*10)

$push->setConfig([
    'priority' => 'high',
    'dry_run' => true,
    'time_to_live' => 3
]);

The default configuration parameters for APN are:, (*11)

  • certificate => __DIR__ . '/iosCertificates/yourCertificate.pem'
  • passPhrase => 'MyPassPhrase'
  • passFile => __DIR__ . '/iosCertificates/yourKey.pem' //Optional
  • dry_run => false

(Make sure to set dry_run to true if you're using development *.pem certificate, and false for production), (*12)

Also you can update those values and add more dynamically, (*13)

$push->setConfig([
    'passPhrase' => 'NewPass',
    'custom' => 'MycustomValue',
    'dry_run' => true
]);

Even you may update the url of the Push Service dynamically like follows:, (*14)

$push->setUrl('http://newPushServiceUrl.com');

Not update the url unless it's really necessary., (*15)

You can specify the number of client-side attempts to APN before giving up. The default amount is 3 attempts. You can override this value by specifying connection_attempts in setConfig() assoc-array. Keep in mind the default number of requested attempts is 3., (*16)

If you prefer to retry indefinitely, set connection_attempts to zero., (*17)

$push->setConfig([
    'passPhrase' => 'NewPass',
    'custom' => 'MycustomValue',
    'connection_attempts' => 0,
    'dry_run' => true
]);

Usage

$push = new PushNotification;

By default it will use GCM as Push Service provider., (*18)

For APN Service:, (*19)

$push = new PushNotification('apn');

For FCM Service:, (*20)

$push = new PushNotification('fcm');

Now you may use any method that you need. Please see the API List., (*21)

API List

Only for Gcm and Fcm

Only for Fcm

Go to Usage samples directly., (*22)

setService

setService method sets the push service to be used, which you pass the name through parameter as a string., (*23)

Syntax, (*24)

object setService($name)

setMessage

setMessage method sets the message parameters, which you pass the values through parameter as an array., (*25)

Syntax, (*26)

object setMessage(array $data)

setApiKey

Only for gcm and fcm, (*27)

setApiKey method sets the API Key of your App, which you pass the key through parameter as a string., (*28)

Syntax, (*29)

object setApiKey($api_key)

setDevicesToken

setDevicesToken method sets the devices' tokens, which you pass the token through parameter as array or string if it was only one., (*30)

Syntax, (*31)

object setDevicesToken($deviceTokens)

send

send method sends the notification., (*32)

Syntax, (*33)

object send()

getFeedback

getFeedback method gets the notification response, which you may use it chaining it to send method or call it whenever after sending a notification., (*34)

Syntax, (*35)

object getFeedback()

getUnregisteredDeviceTokens

getUnregisteredDeviceTokens method gets the devices' tokens that couldn't receive the notification because they aren't registered to the Push service provider. You may use it chaining it to send method or call it whenever after sending a notification., (*36)

Syntax, (*37)

array getUnregisteredDeviceTokens()

setConfig

setConfig method sets the Push service configuration, which you pass the name through parameter as an array., (*38)

Syntax, (*39)

object setConfig(array $config)

setUrl

setUrl method sets the Push service url, which you pass the name through parameter as a string., (*40)

Syntax, (*41)

object setUrl($url)

Not update the url unless it's really necessary., (*42)

sendByTopic

Only for fcm, (*43)

sendBytopic method sends a message by topic. It also accepts the topic condition. more details here, (*44)

If isCondition is true, $topic will be treated as an expression, (*45)

Syntax, (*46)

object sendByTopic($topic,$isCondition)

Usage samples

You can chain the methods., (*47)

GCM sample:, (*48)

$push->setMessage([
        'notification' => [
                'title'=>'This is the title',
                'body'=>'This is the message',
                'sound' => 'default'
                ],
        'data' => [
                'extraPayLoad1' => 'value1',
                'extraPayLoad2' => 'value2'
                ]
        ])
        ->setApiKey('Server-API-Key')
        ->setDevicesToken(['deviceToken1','deviceToken2','deviceToken3'...]);

APN sample:, (*49)

$push->setMessage([
            'aps' => [
                'alert' => [
                    'title' => 'This is the title',
                    'body' => 'This is the body'
                ],
                'sound' => 'default',
                'badge' => 1

            ],
            'extraPayLoad' => [
                'custom' => 'My custom data',
            ]
        ])
    ->setDevicesToken(['deviceToken1','deviceToken2','deviceToken3'...]);

or do it separately, (*50)

$push->setMessage([
       'notification' => [
               'title'=>'This is the title',
               'body'=>'This is the message',
               'sound' => 'default'
               ],
       'data' => [
               'extraPayLoad1' => 'value1',
               'extraPayLoad2' => 'value2'
               ]
       ]);
$push->setApiKey('Server-API-Key');
$push->setDevicesToken(['deviceToken1'
    ,'deviceToken2',
    'deviceToken3'
]);

If you want send the notification to only 1 device, you may pass the value as string., (*51)

$push->setDevicesToken('deviceToken');

Send the Notification

Method send() can be also chained to the above methods., (*52)

$push->setMessage([
       'notification' => [
               'title'=>'This is the title',
               'body'=>'This is the message',
               'sound' => 'default'
               ],
       'data' => [
               'extraPayLoad1' => 'value1',
               'extraPayLoad2' => 'value2'
               ]
       ])
    ->setApiKey('Server-API-Key')
    ->setDevicesToken(['deviceToken1','deviceToken2','deviceToken3'...])
    ->send();

Send the Notification by Topic (FCM only)

$push = new PushNotification('fcm');
$response = $push->setMessage(['message'=>'Hello World'])
            ->setApiKey('YOUR-API-KEY')
            ->setConfig(['dry_run' => false])
            ->sendByTopic('dogs');

or with a condition:, (*53)

$push = new PushNotification('fcm');
$response = $push->setMessage(['message'=>'Hello World'])
            ->setApiKey('YOUR-API-KEY')
            ->setConfig(['dry_run' => false])
            ->sendByTopic("'dogs' in topics || 'cats' in topics",true);

Understanding Gcm and Fcm Message Payload

Notification Message

Add a notification key when setting the message in setMessage method. like follows:, (*54)

$push->setMessage([
           'notification' => [
                   'title'=>'This is the title',
                   'body'=>'This is the message',
                   'sound' => 'default'
                   ]
           );

You may add some extra payload adding a data key when setting the message in setMessage method., (*55)

$push->setMessage([
           'notification' => [
                   'title'=>'This is the title',
                   'body'=>'This is the message',
                   'sound' => 'default'
                   ],
           'data' => [
                   'extraPayLoad1' => 'value1',
                   'extraPayLoad2' => 'value2'
                   ]
           ]);

Data Message

By default, this package sends the notification as Data Message. So no need to add a data key., (*56)

$push->setMessage([
           'title'=>'This is the title',
           'body'=>'This is the message',
           'myCustomVAlue' => 'value'
       ]);

The above example is like you were sending the following:, (*57)

$push->setMessage([
           'data' => [
                   'title'=>'This is the title',
                  'body'=>'This is the message',
                  'myCustomVAlue' => 'value'
                   ]
           ]);

For more details, have a look at gcm/fcm notification paypload support and the concept options, (*58)

Getting the Notification Response

If you want to get the push service response, you can call the method getFeedback:, (*59)

    $push->getFeedback();

Or again, chain it to the above methods:, (*60)

    $push->setMessage(['body'=>'This is the message','title'=>'This is the title'])
                        ->setApiKey('Server-API-Key')
                        ->setDevicesToken(['deviceToken1','deviceToken2','deviceToken3'...])
                        ->send()
                        ->getFeedback();

It will return an object with the response., (*61)

APN Server Feedback and package Feedback

Any time you send a notification, it will check if APN server has any feedback for your certificate. If so, the responses are merged to our feedback like below:, (*62)

class stdClass#21 (4) {
  public $success =>
  int(0)
  public $failure =>
  int(1)
  public $tokenFailList =>
  array(1) {
    [0] =>
    string(64) "c55741656e6c3185f3474291aebb5cf878b8719288e52bf4c497292b320312c5"
  }
  public $apnsFeedback =>
  array(1) {
    [0] =>
    class stdClass#16 (3) {
      public $timestamp =>
      int(1478272639)
      public $length =>
      int(32)
      public $devtoken =>
      string(64) "c55741656e6c3185f3474291aebb5cf878b8719288e52bf4c497292b320312c5"
    }
  }
}

Get Unregistered Devices tokens

After sending a notification, you may retrieve the list of unregistered tokens, (*63)

$push->getUnregisteredDeviceTokens();

This method returns an array of unregistered tokens from the Push service provider. If there isn't any unregistered token, it will return an empty array., (*64)

Laravel Alias Facade

After register the Alias Facade for this Package, you can use it like follows:, (*65)

PushNotification::setService('fcm')
                        ->setMessage([
                             'notification' => [
                                     'title'=>'This is the title',
                                     'body'=>'This is the message',
                                     'sound' => 'default'
                                     ],
                             'data' => [
                                     'extraPayLoad1' => 'value1',
                                     'extraPayLoad2' => 'value2'
                                     ]
                             ])
                        ->setApiKey('Server-API-Key')
                        ->setDevicesToken(['deviceToken1','deviceToken2','deviceToken3'...])
                        ->send()
                        ->getFeedback();

It would return the Push Feedback of the Notification sent., (*66)

Notification channels

Formatting Push Notifications

If a notification supports being sent as an push message, you should define toApn and/or toFcm/toGcm methods on the notification class. This method will receive a $notifiable entity and should return a Edujugon\PushNotification\Messages\PushMessage instance:, (*67)

public function toApn($notifiable)
{
    return new PushMessage('Hello world');
}

Customizing The Title and Body

public function toApn($notifiable)
{
    return (new PushMessage)
        ->title('Hello world')
        ->body('...');
}

Customizing The Notification Sound

public function toApn($notifiable)
{
    return (new PushMessage)
        ->body('Hello world')
        ->sound('default');
}

Customizing The Badge Number

public function toApn($notifiable)
{
  return (new PushMessage)
        ->body('Hello world')
        ->sound('default')
        ->badge(7);
}

Passing Service Config

public function toApn($notifiable)
{
    return (new PushMessage)
        ->body('Hello world')
        ->config(['dry_run' => false]);
}

Add it to the notification channels

public function via($notifiable)
{
    return [ApnChannel::class];
}

Don't forget the use statement at the top of the class, (*68)

Routing Push Notifications

Just define routeNotificationForApn and/or routeNotificationForFcm/routeNotificationForGcm methods on the entity, (*69)

/**
 * Route notifications for the Apn channel.
 *
 * @return string|array
 */
public function routeNotificationForApn()
{
    return $this->ios_push_token;
}

The Versions

29/06 2018

dev-master

9999999-dev

Laravel 5 Package to send push notifications to Android and IOS devices. (GCM,FCM,APN)

  Sources   Download

MIT

The Requires

 

The Development Requires

by Eduardo Marcos

laravel notification push gcm apn priority fcm high

29/06 2018

v3.0.2

3.0.2.0

Laravel 5 Package to send push notifications to Android and IOS devices. (GCM,FCM,APN)

  Sources   Download

MIT

The Requires

 

The Development Requires

by Eduardo Marcos

laravel notification push gcm apn priority fcm high

20/01 2018

2.2.x-dev

2.2.9999999.9999999-dev

Laravel 5 Package to send push notifications to Android and IOS devices. (GCM,FCM,APN)

  Sources   Download

MIT

The Requires

 

The Development Requires

by Eduardo Marcos

laravel notification push gcm apn priority fcm high

20/01 2018

v2.2.3

2.2.3.0

Laravel 5 Package to send push notifications to Android and IOS devices. (GCM,FCM,APN)

  Sources   Download

MIT

The Requires

 

The Development Requires

by Eduardo Marcos

laravel notification push gcm apn priority fcm high

20/01 2018

v3.0.1

3.0.1.0

Laravel 5 Package to send push notifications to Android and IOS devices. (GCM,FCM,APN)

  Sources   Download

MIT

The Requires

 

The Development Requires

by Eduardo Marcos

laravel notification push gcm apn priority fcm high

13/01 2018

v3.0.0

3.0.0.0

Laravel 5 Package to send push notifications to Android and IOS devices. (GCM,FCM,APN)

  Sources   Download

MIT

The Requires

 

The Development Requires

by Eduardo Marcos

laravel notification push gcm apn priority fcm high

04/01 2018

dev-notification-channel

dev-notification-channel

Laravel 5 Package to send push notifications to Android and IOS devices. (GCM,FCM,APN)

  Sources   Download

MIT

The Requires

 

The Development Requires

by Eduardo Marcos

laravel notification push gcm apn priority fcm high

18/11 2017

v2.2.2

2.2.2.0

Laravel 5 Package to send push notifications to Android and IOS devices. (GCM,FCM,APN)

  Sources   Download

MIT

The Requires

 

The Development Requires

by Eduardo Marcos

laravel notification push gcm apn priority fcm high

18/11 2017

dev-dev

dev-dev

Laravel 5 Package to send push notifications to Android and IOS devices. (GCM,FCM,APN)

  Sources   Download

MIT

The Requires

 

The Development Requires

by Eduardo Marcos

laravel notification push gcm apn priority fcm high

29/10 2017

v2.2.1

2.2.1.0

Laravel 5 Package to send push notifications to Android and IOS devices. (GCM,FCM,APN)

  Sources   Download

MIT

The Requires

 

The Development Requires

by Eduardo Marcos

laravel notification push gcm apn priority fcm high

29/08 2017

2.2.0

2.2.0.0

Laravel 5 Package to send push notifications to Android and IOS devices. (GCM,FCM,APN)

  Sources   Download

MIT

The Requires

 

The Development Requires

by Eduardo Marcos

laravel notification push gcm apn priority fcm high

01/03 2017

v2.1.7

2.1.7.0

Laravel 5 Package to send push notifications to Android and IOS devices. (GCM,FCM,APN)

  Sources   Download

MIT

The Requires

 

The Development Requires

by Eduardo Marcos

laravel notification push gcm apn priority fcm high

04/11 2016

dev-apnSocket

dev-apnSocket

Laravel 5 Package to send push notifications to Android and IOS devices. (GCM,FCM,APN)

  Sources   Download

MIT

The Requires

 

The Development Requires

by Eduardo Marcos

laravel notification push gcm apn priority fcm high

02/11 2016

dev-laravel-5

dev-laravel-5

Laravel 5 Package to send push notifications to Android and IOS devices. (GCM,FCM,APN)

  Sources   Download

MIT

The Requires

 

The Development Requires

by Eduardo Marcos

laravel notification push gcm apn priority fcm high

20/10 2016

dev-php_5.5

dev-php_5.5

Laravel 5 Package to send push notifications to Android and IOS devices. (GCM,FCM,APN)

  Sources   Download

MIT

The Requires

 

The Development Requires

by Eduardo Marcos

laravel notification push gcm apn priority fcm high

20/10 2016

v2.1.6

2.1.6.0

Laravel 5 Package to send push notifications to Android and IOS devices. (GCM,FCM,APN)

  Sources   Download

MIT

The Requires

 

The Development Requires

by Eduardo Marcos

laravel notification push gcm apn priority fcm high

14/10 2016

v2.1.5

2.1.5.0

Laravel 5 Package to send push notifications to Android and IOS devices. (GCM,FCM,APN)

  Sources   Download

MIT

The Requires

 

The Development Requires

by Eduardo Marcos

laravel notification push gcm apn priority fcm high

26/09 2016

v2.1.3

2.1.3.0

Laravel 5 Package to send push notifications to Android and IOS devices. (GCM,FCM,APN)

  Sources   Download

MIT

The Requires

 

The Development Requires

by Eduardo Marcos

laravel notification push gcm apn priority fcm high

26/09 2016

v2.1.4

2.1.4.0

Laravel 5 Package to send push notifications to Android and IOS devices. (GCM,FCM,APN)

  Sources   Download

MIT

The Requires

 

The Development Requires

by Eduardo Marcos

laravel notification push gcm apn priority fcm high

21/09 2016

v2.1.2

2.1.2.0

Laravel 5 Package to send push notifications to Android and IOS devices. (GCM,FCM,APN)

  Sources   Download

MIT

The Requires

 

The Development Requires

by Eduardo Marcos

laravel notification push gcm apn priority fcm high

12/07 2016

v2.1.0

2.1.0.0

Package to send push notifications to Android and IOS devices

  Sources   Download

MIT

The Requires

 

The Development Requires

by Eduardo Marcos

laravel notification push priority high

08/07 2016

v2.0

2.0.0.0

Package to send push notifications to Android and IOS devices

  Sources   Download

MIT

The Requires

 

The Development Requires

by Eduardo Marcos

laravel notification push priority high

07/07 2016

v1.0

1.0.0.0

Package to send push notifications to Android and IOS devices

  Sources   Download

MIT

The Requires

 

The Development Requires

by Eduardo Marcos

laravel notification push priority high