autoxloo/apns

Apple 通知服务器

1.0.0 2020-05-07 06:53 UTC

This package is auto-updated.

Last update: 2024-09-15 13:38:21 UTC


README

通过 Apple 通知服务器发送推送通知

注意:此包不支持正常使用

安装

安装此扩展的首选方式是通过composer

运行以下命令之一:

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

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

或将

"autoxloo/apns": "*"

添加到您的composer.json文件的require部分。

配置

您必须安装支持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

使用方法

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

构造函数参数和默认值

use autoxloo\apns\AppleNotificationServer;

$appleCertPath = __DIR__ . '/wxv_cert.pem';

$apns = new AppleNotificationServer(
    $appleCertPath,
    $apiUrl = 'https://api.push.apple.com/3/device',
    $apiUrlDev = 'https://api.sandbox.push.apple.com/3/device',
    $apnsPort = 443,
    $pushTimeOut = 10,
    $topic = null,
    $expiration = null,
    $pushType = null
);

发送推送通知

use autoxloo\apns\AppleNotificationServer;

$appleCertPath = __DIR__ . '/wxv_cert.pem';
$token = 'some device token';
$payload = [
    'some key1' => 'some value1',
    'some key2' => 'some value2',
];

$apns = new AppleNotificationServer($appleCertPath);
$response = $apns->send($token, $payload);

如果您想向多个令牌发送

use autoxloo\apns\AppleNotificationServer;

$appleCertPath = __DIR__ . '/wxv_cert.pem';
$tokens = [
    'some device token',
    'some other device token',
];
$payload = [
    'some key1' => 'some value1',
    'some key2' => 'some value2',
];

$apns = new AppleNotificationServer($appleCertPath);
$response = $apns->sendToMany($tokens, $payload);

如果您想使用某些apns-push-type发送推送通知,则需要与此推送类型兼容的证书,并在构造函数中设置AppleNotificationServer::$pushType或使用set方法

use autoxloo\apns\AppleNotificationServer;

$appleCertPath = __DIR__ . '/wxv_cert.pem';
$token = 'some device token';
$payload = [
    'some key1' => 'some value1',
    'some key2' => 'some value2',
];

$apns = new AppleNotificationServer($appleCertPath);
$apns->setPushType(AppleNotificationServer::PUSH_TYPE_BACKGROUND);  // sets `apns-push-type` header.
// other available set methods:
$apns->setTopic('some topic');  // sets `apns-topic` header.
$apns->setExpiration(time() + 30);  // sets `apns-expiration` header.
$apns->setExpiration(0);  // sets `apns-expiration` header. If the value is 0, APNs attempts to deliver
                          // the notification only once and doesn’t store it.
$response = $apns->send($token, $payload);

AppleNotificationServer首先在$apiUrlhttps://api.push.apple.com/3/device)发送推送通知,如果失败(状态码不是200),则发送到$apiUrlDevhttps://api.sandbox.push.apple.com/3/device)。如果您不想在$apiUrlDev上发送推送通知,将其值设置为false。此外,如果您只想在开发url上发送推送通知,可以这样操作(将$apiUrl设置为开发url的值)

use autoxloo\apns\AppleNotificationServer;

$apns = new AppleNotificationServer($appleCertPath, 'https://api.sandbox.apple.com/3/device', false);

请参阅生成远程通知向APNs发送通知请求以获取更多详细信息。