autoxloo/yii2-fcm

Yii2 wrap on autoxloo/fcm

Installs: 3 186

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 3

Forks: 0

Open Issues: 0

Type:yii2-extension

dev-master 2018-12-18 15:50 UTC

This package is auto-updated.

Last update: 2024-04-19 20:19:41 UTC


README

Yii2 wrap of autoxloo/fcm

Note: This package is not supported properly

Installation

The preferred way to install this extension is through composer.

Either run

php composer.phar require --prefer-dist autoxloo/yii2-fcm "*"

or

composer require --prefer-dist autoxloo/yii2-fcm "*"

or add

"autoxloo/yii2-fcm": "*"

to the require section of your composer.json file.

Config

In your application config add:

// ...
'components' => [
        // ...
        'firebaseNotification' => [
            'class' => \autoxloo\yii2\fcm\FirebaseCMNotification::class,
            'projectId' => 'project-id',
            'serviceAccountFile' => __DIR__ . '/service_account.json',
        ],
],

Usage

Once the extension is installed, simply use it in your code by:

$token = 'some device token';
$name = 'Some name';
$title = 'Some title';
$body = 'Some body';
$data = [
    'some key1' => 'some value1',
    'some key2' => 'some value2',
]; 

// sending push notification:

$target = FCMFacade::createTargetToken($token);     // only target is required
$notification = FCMFacade::createNotification($title, $body);
$androidConfig = FCMFacade::createAndroidConfig([AndroidConfig::FIELD_PRIORITY => AndroidConfig::PRIORITY_HIGH]);

$message = FCMFacade::createMessage();
$message->setTarget($target)
    ->setName($name)
    ->setData($data)
    ->setNotification($notification)
    ->setAndroidConfig($androidConfig);

$response = Yii::$app->firebaseNotification->send($message);   // $response is instance of \GuzzleHttp\Psr7\Response

Or

$messageConfig = [
    // required one of: token, topic or condition
    Message::FIELD_TOKEN => $token,     // or Message::FIELD_TOPIC => $topic or Message::FIELD_CONDITION => $condition

    // not required values:
    Message::FIELD_NAME => $name,
    Message::FIELD_DATA => $data,
    Message::FIELD_NOTIFICATION => FCMFacade::createNotification($title, $body),
    Message::FIELD_ANDROID => FCMFacade::createAndroidConfig([
        AndroidConfig::FIELD_PRIORITY => AndroidConfig::PRIORITY_HIGH
    ]),
];

$message = FCMFacade::createMessage($messageConfig);

$response = Yii::$app->firebaseNotification->send($message);   // $response is instance of \GuzzleHttp\Psr7\Response

Target

You can use target one of:

  • TargetToken
  • TargetTopic
  • TargetCondition

To create use facade:

$targetToken = FCMFacade::createTargetToken('some token');
$targetTopic = FCMFacade::createTargetTopic('some topic');
$targetCondition = FCMFacade::createTargetConsition('some condition');

See autoxloo/fcm for more details.