PHP code example of develandoo / yii2-push-notification

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

    

develandoo / yii2-push-notification example snippets


    'components' => [
         'push' => [
             'class' => 'develandoo\notification\Push',
             'options' => [
                 'returnInvalidTokens' => true //default false
             ],
             'apnsConfig' => [
                 'environment' => Push::APNS_ENVIRONMENT_PRODUCTION or Push::APNS_ENVIRONMENT_SANDBOX,
                 'pem' => 'PEM_FILE_ABS_URL',
                 'passphrase' => 'YOUR_PASS_PHRASE', //optional
             ],
             'gcmConfig' => [
                 'apiAccessKey' => 'YOUR_GCM_API_KEY'
             ]
         ]
     ]

$push = Yii::$app->push;

// ios signle token example
$push->ios()->send('token', [
    'custom-key' => 'custom-value',
    'aps' => [
        'alert' => [
            'loc-key' => 'i18n_key',
            'loc-args' => ['arg1'],
        ]
    ],
    'badge' => 1,
    'sound' => 'default'
]);

// ios multiple tokens example
$push->ios()->send(['token1,token2'], [
    'custom-key' => 'custom-value',
    'aps' => [
        'alert' => 'STRING_MESSAGE'
    ],
    'badge' => 1,
    'sound' => 'default'
]);

// android example
$push->android()->send(['token1,token2'], [
    'key' => 'i18n_key',
    'args' => ['arg1'],
    'custom-key'=>'custom-value'
]);

// mixed tokens example
$push->send([
    'ios-tokens1',
    'android-token1',
    'android-token2',
    'ios-token2'
], $payload);

// firebase (both ios and android are supported) multiple tokens example
$push->firebase()->send(['token1','token2'], [
    // Background (closed) application data.
    'notification' => [
        'body' => 'Background application message',
        'title' => 'AppName',
        'sound' => 'default',
    ],
    // Foreground (running) application data.
    'data' => [
        'custom-key' => 'Any custom data could be delivered into foreground application. '
        . 'In order to simulate push notification, this data should be used inside "local notification" by client application.',
    ],
]);