2017 © Pedro Peláez
 

library laravel-push-notification

Laravel package to send push notification across all platforms (apns, fcm)

image

meddev/laravel-push-notification

Laravel package to send push notification across all platforms (apns, fcm)

  • Tuesday, October 18, 2016
  • by meddev
  • Repository
  • 1 Watchers
  • 2 Stars
  • 105 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 2 Open issues
  • 2 Versions
  • 1 % Grown

The README.md

Install

Add follow line into "require" section in your composer.json:, (*1)

"meddev/laravel-push-notification": "1.0.0"

Update composer with command:, (*2)

"composer update"

Provider and Facade

Like all providers, put this follow lines in your config/app.php, (*3)

'providers' => [
    ...
    MedDev\PushNotification\PushNotificationProvider::class,
],
'aliases' => [
    ...
    'PushNotificationFacade' => MedDev\PushNotification\Facades\PushNotificationFacade::class,
],

Configuration

Finally you need to generate a configuration file for this package. Run follow composer command:, (*4)

php artisan vendor:publish --provider="MedDev\PushNotification\PushNotificationProvider"

This command will generate pushnotification.php file in your config directory., (*5)

return [

    "aps" => [
            /*
             * A valid PEM certificate generated from Apple Push Service certificate
             */
            "certificate"   => storage_path('app')."/aps.pem",

            /*
             * Password used to generate a certificate
             */
            "passPhrase"    => "",

            /*
             * Server used to send push notifications
             */
            "server"    => "https://api.development.push.apple.com",

            /*
             * Set to TRUE if HTTP/2 Is Enabled for Your SSL application
             */
            "useApi"    => false
        ],

        "fcm" => [
            /*
             * Google FCM api server key
             * You can retrieve your key in Firebase Console
             */
            "apiKey"        => "",

            /*
             * Server used to send push notifications
             */
            "server"    => "https://fcm.googleapis.com/fcm/send"
        ]

];

Remember to add your FCM api key and PEM certificate path., (*6)

Tokens

You should have a model to store devices information into your database. To fit your model to be used directly from PushNotification Package you simply need to add use TokenTrait:, (*7)

use MedDev\PushNotification\TokenTrait;

class YourDevicesTable extends Model
{
    use TokenTrait;
}

To works automatically with PushNotification Package your table needs two columns, one that contains platform name, and the other one that contains device token., (*8)

By default this two columns names are considered "platform" and "device_token", (*9)

+--------------+
| Field        |
+--------------+
| id           |
| platform     | enum (android, ios, web)
| device_token |
| created_at   |
| updated_at   |
+--------------+

You can design your table as you want, only this two fields are mandatory to work with PushNotification Package., (*10)

Payload

You just create a class that implements MedDev\PushNotification\Contracts\Payload and overwrite apsPayload and fcmPayload properties with your payload content., (*11)

namespace App\Payload;

use App\User;
use MedDev\PushNotification\Contracts\Payload;

class MessagePayload extends Payload
{
    /**
     * Generate Notification Payload
     *
     * @param User $user
     * @return void
     */
    public function __construct(User user)
    {
        //IOS payload format    
        $this->apsPayload = [
                "alert" => [
                    "title" => "Someone has sent you a message",
                    "body"  => "This is the notification body text",
                ],
        ];

        //Android payload format
        $this->fcmPayload = [
                "title" => "Someone has sent you a message",
                "body"  => "This is the notification body text",
        ];
    }
}

You can proceed to create your payload collection for every event or message that you want send to your users., (*12)

Send

Now you are able to get a list of devices tokens from your DB and you have a payload for your specific event. To send a payload to a list of devices you can use send method inherited from Payload class., (*13)

Regular send

//Create payload
$payload = new MessagePayload(User::findOrFail(1));

//Retrieve devices list with your own criteria
$tokens = YourDevicesTable::all();

//send directly
$payload->send($tokens);

Send by Queue

Package use Queue to send notification natively to execute your send task in background. You need to set your Queue provider in config/queue.php and your payload will be sent on queue., (*14)

With second param of "send" method you can schedule job in a specific queue., (*15)

//push in queue
$payload->send($payload, $tokens, "your-queue-name");

The Versions

18/10 2016

1.0.0.x-dev

1.0.0.9999999-dev

Laravel package to send push notification across all platforms (apns, fcm)

  Sources   Download

MIT

by Hamid Medgjonaj

laravel notification push apns fcm

30/09 2016

dev-master

9999999-dev

Laravel package to send push notification across all platforms (apns, fcm)

  Sources   Download

MIT

by Hamid Medgjonaj

laravel notification push apns fcm