PHP code example of mhd-elawi / notification

1. Go to this page and download the library: Download mhd-elawi/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/ */

    

mhd-elawi / notification example snippets


'default_language' => 'en',

'languages' => ['en', 'ar', 'de', 'fr'],

'save_notifications' => true, // Set false to disable

'send_notifications' => true,

use MhdElawi\Notification\Utils\Notification;

Notification::for($user)
    ->setTitle('Hello, World!')
    ->setBody('This is a test notification.')
    ->send();

Notification::for($user)
    ->setTitle('Bonjour, Monde!', 'fr')
    ->setBody('Ceci est une notification de test.', 'fr')
    ->send();

    Notification::for($user)
    ->setTitleWithTranslation('notification.title', ['name' => $user->name])
    ->setBodyWithTranslation('notification.body', ['name' => $user->name])
    ->send();

Notification::for($users)
    ->setTitle('Batch Notification')
    ->setBody('This notification is sent to multiple users.')
    ->send();

$relatedModel = Post::query()->first();

Notification::for($user, $relatedModel)
    ->setTitle('New Comment on Your Post')
    ->setBody('Someone has commented on your post.')
    ->setIcon('comment-icon')
    ->send();

$relatedModel = Post::query()->first();

Notification::for($user)
->setRelatedModel($relatedModel) // Set the related model explicitly
->setTitle('New Comment on Your Post')
->setBody('Someone has commented on your post.')
->setIcon('comment-icon')
->send();

Notification::for($user)
    ->setTitle('New Comment on Your Post')
    ->setBody('Someone has commented on your post.')
    ->setIcon('comment-icon')
    ->setExtraFields(['sound' => 'something'])
    ->send();

use MhdElawi\Notification\Contracts\HasNotification;

class User extends Authenticatable implements HasNotification
{
    public function getDeviceTokens(): array
    {
        return $this->device_tokens ?? [];
    }
}

'save_notifications' => false,

Notification::for($user)
    ->setTitle('Title')
    ->setBody('Body')
    ->send(false);

'send_notifications' => false,

Notification::for($users)
    ->setTitle('Hello, World!')
    ->setBody('This is a test notification.')
    ->disableSendNotification()
    ->send();
)

[comment]: <> ('batch_size' => 1000,)

[comment]: <> (
bash
php artisan vendor:publish --tag=config
bash
php artisan vendor:publish --tag=migration
bash
php artisan migrate