PHP code example of kobir / larapush-notification

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

    

kobir / larapush-notification example snippets


$push->setConfig([
    'priority' => 'high',
    'firebase_project_id' => 'my-project-id',
    'certificate' => 'path/to/fcm-admin-sdk.json'
    'token_cache_time' => 3000
]);

$push = new PushNotification;

$push = new PushNotification('fcm');

object setNotification(array $data)

object setData(array $data)

object setProjectId($projectId)

object setJsonCredential($filePath)

object setConfig(array $config)

object setUrl($url)

object setDevicesToken($deviceTokens)

object send()

object getFeedback()

array getFailedDeviceTokens()

object sendByTopic($topic, $isCondition)

object addDeviceTokenToTopic(string $topic)

object removeDeviceTokenFromTopic(string $topic)

object getTopicInfo(string $deviceToken)

object setTopicAddUrl($url)

object setTopicRemoveUrl($url)

object setTopicInfoUrl($url)

$push = new PushNotification();
/***
 * (string) title
 * (string) body
 * (string) image, optional field image size must be less than 1mb
*/
$push->setNotification([
        'title' => (string) 'Title goes here',
        'body' => (string) 'Body text goes here',
        'image' => (string) 'image-url', //optional,
        ])
        ->setDevicesToken(['deviceToken1', 'deviceToken2', ...])
        ->send();

/***
 * (string) id, if haven't id place '0'
*/
$feedback = $push->setNotification([
                    'title' => (string) 'Title goes here',
                    'body' => (string) 'Body text goes here',
                    'image' => (string) 'image-url', //optional
                    ])
                    ->setData([
                    'id' => (string) 'dynamic_id',
                    'type' => 'OFFER',
                    'sound' => 'default',
                    'click_action' => 'NOTIFICATION_CLICK',
                    ])
                    ->setDevicesToken(['deviceToken1', 'deviceToken2', ...])
                    ->send()
                    ->getFeedback();


$feedback = $push->setNotification([
                'title' => (string) 'Title goes here',
                'body' => (string) 'Body text goes here',
                'image' => (string) 'image-url', //optional
                ])
                ->setDevicesToken(['deviceToken1', 'deviceToken2', ...])
                ->send()
                ->getFeedback();

$failedTokens = $push->setNotification([
                    'title' => (string) 'Title goes here',
                    'body' => (string) 'Body text goes here',
                    'image' => (string) 'image-url', //optional
                    ])
                    ->setDevicesToken(['deviceToken1', 'deviceToken2', ...])
                    ->send()
                    ->getFailedDeviceTokens();

$feedback = $push->setNotification([
                'title' => (string) 'Title goes here',
                'body' => (string) 'Body text goes here',
                'image' => (string) 'image-url', //optional
                ])
                ->setDevicesToken('deviceToken')
                ->send()
                ->getFeedback();


/***
 * (string) topic, name like 'foo', 'bar'
*/
$feedback = $push->setNotification([
                'title' => (string) 'Title goes here',
                'body' => (string) 'Body text goes here',
                'image' => (string) 'image-url', //optional
                ])
                ->sendByTopic('bar')
                ->getFeedback();


/***
 * you must have pass the additional parameter as 'true'
*/
$feedback = $push->setNotification([
                'title' => (string) 'Title goes here',
                'body' => (string) 'Body text goes here',
                'image' => (string) 'image-url', //optional
                ])
                ->sendByTopic("'foo' in topics || 'bar' in topics", true)
                ->getFeedback();


$response = $push->setDevicesToken(['deviceToken1', 'deviceToken2', ...])
                ->addDeviceTokenToTopic('foo');


$response = $push->setDevicesToken(['deviceToken1', 'deviceToken2', ...])
                ->removeDeviceTokenFromTopic('bar');


$response = $push->getTopicInfo('deviceToken1');

//successful response will be like
{
    "applicationVersion": "19",
    "application": "com.iid.example",
    "authorizedEntity": "123456782354",
    "platform": "ANDROID",
    "rel": {
        "topics": {
            "cats": {
                "addDate": "2024-10-20"
            },
            "dogs": {
                "addDate": "2024-10-20"
            }
        }
    }
}
sh
php artisan vendor:publish --provider="Kobir\PushNotification\Providers\PushNotificationServiceProvider" --tag="config"