autoxloo/yii2-apns

Yii2对autoxloo/apns的封装

安装: 78

依赖: 0

建议者: 0

安全: 0

星星: 0

关注者: 3

分支: 0

开放问题: 0

类型:yii2-extension

1.0.0 2019-12-10 08:05 UTC

This package is auto-updated.

Last update: 2024-09-15 13:13:08 UTC


README

autoxloo/apns的Yii2封装

注意:此包不支持得很好

安装

通过 composer 安装此扩展是首选方法。

运行

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

或者

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

或者在您的 composer.json 文件的 require 部分添加

"autoxloo/yii2-apns": "*"

配置

要发送推送通知,您应该有苹果的 .pem 证书。

在您的应用程序配置中添加

// ...
'components' => [
        // ...
        'apnsNotification' => [
            'class' => \autoxloo\yii2\apns\APNSNotification::class,
            'appleCertPath' => __DIR__ . '/wxv_cert.pem',
            'apiUrl' => 'https://api.push.apple.com/3/device',                  // default
            'apiUrlDev' => 'https://api.development.push.apple.com/3/device',   // default
            'apnsPort' => 443,                                                  // default
            'pushTimeOut' => 10,                                                // default
        ],
],

AppleNotificationServer 首先在 $apiUrl (https://api.push.apple.com/3/device) 上发送推送通知,如果失败(状态码不是 200),则发送到 $apiUrlDev (https://api.development.push.apple.com/3/device)。如果您不想在 $apiUrlDev 上发送推送通知,将其值设置为 false

// ...
'components' => [
        // ...
        'apnsNotification' => [
            'class' => \autoxloo\yii2\apns\APNSNotification::class,
            'appleCertPath' => __DIR__ . '/wxv_cert.pem',
            'apiUrl' => 'https://api.push.apple.com/3/device',                  // default
            'apiUrlDev' => false,
            'apnsPort' => 443,                                                  // default
            'pushTimeOut' => 10,                                                // default
        ],
],

如果您只想在 dev url 上发送推送通知,您可以通过将 apiUrl 设置为 dev url 来实现

// ...
'components' => [
        // ...
        'apnsNotification' => [
            'class' => \autoxloo\yii2\apns\APNSNotification::class,
            'appleCertPath' => __DIR__ . '/wxv_cert.pem',
            'apiUrl' => 'https://api.development.push.apple.com/3/device',
            'apiUrlDev' => false,
            'apnsPort' => 443,                                                  // default
            'pushTimeOut' => 10,                                                // default
        ],
],

您必须安装支持http2的curl

cd ~
sudo apt-get install build-essential nghttp2 libnghttp2-dev libssl-dev
wget https://curl.haxx.se/download/curl-7.58.0.tar.gz
tar -xvf curl-7.58.0.tar.gz
cd curl-7.58.0
./configure --with-nghttp2 --prefix=/usr/local --with-ssl=/usr/local/ssl
make
sudo make install
sudo ldconfig
sudo reboot

信息来自https://askubuntu.com/questions/884899/how-do-i-install-curl-with-http2-support

如果没有帮助,请尝试https://serversforhackers.com/c/curl-with-http2-support

使用方法

发送推送通知

$token = 'some device token';
$payload = [
    'some key1' => 'some value1',
    'some key2' => 'some value2',
];

$response = \Yii::$app->apnsNotification->send($token, $payload);

或者如果您想发送给多个令牌

$tokens = [
    'some device token',
    'some other device token',
];
$payload = [
    'some key1' => 'some value1',
    'some key2' => 'some value2',
];

$response = \Yii::$app->apnsNotification->sendToMany($tokens, $payload);

有关更多详细信息,请参阅autoxloo/apns