irice / yii2-fcm-manager

Yii2 的 FCM 通知管理器

安装次数: 308

依赖项: 0

建议者: 0

安全性: 0

星标: 1

关注者: 2

分支: 0

公开问题: 0

类型:yii2-extension

0.2.1 2019-10-21 10:45 UTC

This package is auto-updated.

Last update: 2024-09-21 22:11:19 UTC


README

安装

Composer 安装

composer require irice/yii2-fcm-manager

数据库迁移

config/console.php

return [
    'controllerMap' => [
        'migrate' => [
            'class' => 'yii\console\controllers\MigrateController',
            'migrationPath' => null,
            'migrationNamespaces' => [
                'fcm\manager\migrations',
                ...
            ],
        ],
    ],
    ...
];

然后运行迁移命令

php yii migrate

配置

config/web.php

return [
    'modules' => [
        'fcm' => [
            'class' => 'fcm\manager\Module',
            ...
        ],
        ...
    ],
    ...
    'components' => [
        'fcm' => [
            'class' => 'fcm\manager\components\Connection',
            // you can download config file on firebase console 'https://console.firebase.google.com/u/1/project/<your_project>/settings/serviceaccounts/adminsdk'
            'configPath' => __DIR__ . '/<your_project>-firebase-adminsdk.json',
        ],
        ...
    ],
    ...
];

基本用法

将设备注册到数据库中

$result = Yii::$app->fcm->deviceRegisterClass::registerDevice(
    '<registed_device_token>', // or ['<registed_device_token1>', '<registed_device_token2>']
    <user_id> // user identity id
);

将设备订阅到主题

$result = Yii::$app->fcm->subscribeToTopic(
    '<topic-name>',
    '<registed_device_token>' // or ['<registed_device_token1>', '<registed_device_token2>']
);

将设备从主题取消订阅

$result = Yii::$app->fcm->unSubscribeFromTopic(
    '<topic-name>',
    '<registed_device_token>' // or ['<registed_device_token1>', '<registed_device_token2>']
);

向主题发送消息

$result = Yii::$app->fcm->sendToTopic(
    '<topic-name>',
    '<message title>',
    '<message content>',
    '<message image url>' // optional
);

向设备发送消息

$result = Yii::$app->fcm->sendToTokens(
    '<registed_device_token>' // or ['<registed_device_token1>', '<registed_device_token2>']
    '<message title>',
    '<message content>',
    '<message image url>' // optional
);

计划发送消息

该功能由 Yii2-queue 扩展实现。

config/console.php (config/web.php)

'components' => [
    'queue' => [
        'class' => 'yii\queue\db\Queue',
        'serializer' => 'fcm\manager\components\PhpSerializer',
        'deleteReleased' => false,
        'as log' => 'yii\queue\LogBehavior',
    ],
    'mutex' => [
        'class' => 'yii\mutex\PgsqlMutex',
    ],
    'fcm' => [
        'class' => 'fcm\manager\components\Connection',
        'configPath' => __DIR__ . '/<your_project>-firebase-adminsdk.json',
    ],
    ...
]

计划向主题发送消息

$job = new \fcm\manager\jobs\SendJob([
    'notification' => [
        'title' => '<message-title>',
        'body' => '<message-body>',
        'target' => [
            'type' => \fcm\manager\jobs\SendJob::TYPE_TOPIC,
            'value' => '<topic-name>',
        ],
    ],
]);

$delayTime = strtotime('2019-12-31 00:00:00') - time(); //Send message on specified time.
//$delayTime = strtotime('next Saturday') - time(); //Send message on next weekend.
//$delayTime = 60 * 60 * 24; //Send message after 24 hours.

$queueId = Yii::$app->queue->delay($delayTime)->push($job);

发送后自动更新 fcm 进度

在自定义类上实现 NotificationInterface

class Notification extends \yii\db\ActiveRecord implements \fcm\manager\models\NotificationInterface
{
    ... 
    public function getSuccessStatus()
    {
        return static::STATUS['SENDED'];
    }

    public function getFailStatus()
    {
        return static::STATUS['FAILED'];
    }

    public function updateStatus($value)
    {
        $this->status = $value;
        return $this->save();
    }
}

待办事项

通知 GUI 管理器

要求

Yii2-queue