PHP code example of bnbwebexpertise / laravel-push-notifications

1. Go to this page and download the library: Download bnbwebexpertise/laravel-push-notifications 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/ */

    

bnbwebexpertise / laravel-push-notifications example snippets


    'providers' => [
        Bnb\PushNotifications\PushNotificationsServiceProvider::class,
    ],



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,

];

$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');

$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']]);
}