PHP code example of weelis / notification

1. Go to this page and download the library: Download weelis/notification library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

weelis / notification example snippets


// config/app.php
'providers' => [
    ...
    Weelis\Notification\NotificationServiceProvider::class,
    ...
];
'aliases' => [
    ...
    'NotificationHelper'=> \Weelis\Notification\Facade\NotificationHelper::class
    ...
]

Route::group(['prefix' => 'device'], function () {
    Route::post('register', '\Weelis\Notification\Controller\DevicesController@registerDevice');
    Route::post('unregister', '\Weelis\Notification\Controller\DevicesController@unregisterDevice');
});

use Weelis\Notification\Base\Notifiable;
use Weelis\Notification\Model\NotificationModel;

class User extends Authenticatable
{
    use Notifiable, NotificationModel;
    ...
}

$request => ['os'         => ' '			'did'        => ' '

FCM_API_KEY=legacy key

return [
    /*
     * Add the Firebase API key
     */
    'fcm' => [
        'api_key' => ''
    ],
];

public function via($notifiable)
{
    return ['fcm'];
}

public function toFcm($notifiable) 
{
    $message = new Weelis\Notification\Fcm\FcmMessage();
    $message->content([
        'title'        => 'Foo', 
        'body'         => 'Bar', 
        'sound'        => '', // Optional 
        'icon'         => '', // Optional
        'click_action' => '' // Optional
    ])->data([
        'param1' => 'baz' // Optional
    ])->priority(Weelis\Notification\Fcm\FcmMessage::PRIORITY_HIGH); // Optional - Default is 'normal'.
    
    return $message;
}

/**
 * Route notifications for the FCM channel.
 *
 * @return string
 */
public function routeNotificationForFcm()
{
    return $this->device_token;
}

public function toFcm($notifiable) 
{
    $message = new Weelis\Notification\Fcm\FcmMessage();
    $message->to('the-topic', $recipientIsTopic = true)
    ->content([...])
    ->data([...]);
    
    return $message;
}

APN_KEY_DEV={"user":{"cert":"/storage/path/file","pass":"passphrase"},"worker":{"cert":"/storage/path/file","pass":"passphrase"}}
APN_KEY_PRO={"user":{"cert":"/storage/path/file","pass":"passphrase"},"worker":{"cert":"/storage/path/file","pass":"passphrase"}}

use Weelis\Notification\Apn\ApnMessage;
use Illuminate\Notifications\Notification;

class AccountApproved extends Notification
{
    public function via($notifiable)
    {
        return ['apn'];
    }

    public function toApn($notifiable)
    {
        return ApnMessage::create()
            ->badge(1)
            ->title('Account approved')
            ->body("Your {$notifiable->service} account was approved!");
    }
}

public function routeNotificationForApn()
{
    return $this->apn_token;
}

ESMS_API_KEY=<key>
ESMS_SECRET_KEY=<secrect>
ESMS_SMS_TYPE=6
ESMS_BRAND_NAME=
ESMS_URL=http://rest.esms.vn/MainService.svc/json/SendMultipleMessage_V4_get
ESMS_DAY_MAX=5

use Illuminate\Notifications\Notification;

class AccountApproved extends Notification
{
    public function via($notifiable)
    {
        return ['esms'];
    }

    public function toApn($notifiable)
    {
        return [
            "sms" => <your sms message>
        ];
    }
}

/**
 * Route notifications for the FCM channel.
 *
 * @return string
 */
public function routeNotificationForEsms()
{
    return $this->phone;
}

use Weelis\Notification\Base\NotificationToUser;

$user->notify(new NotificationToUser([
    'scope' => "user",
    'types' => ['email', 'esms', 'apn', 'fcm'],
    'title' => "Foo",
    'body' => "Bar",
    'icon' => "", // Optional
    'sound' => "", // Optional
    'type' => "Foo Bar",
    'type_slug' => "foo-bar",
    'email_view' => 'foo.bar', // Optional
    'custom' => [ // use for email view param also
        'foo' => '',
        'bar' => '',
        'click_action' => ""
    ] // Optional
]), $notifitable_model  // Optional);
 bash
php artisan vendor:publish --provider="Weelis\Notification\NotificationServiceProvider"
bash
php artisan make:notification SomeNotification