p2 project
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

95 lines
3.1 KiB

2 months ago
<?php
namespace app\utility;
use app\home\service\BaseHomeService;
use Pusher\PushNotifications\PushNotifications;
2 months ago
require_once __DIR__ . '/../../vendor/autoload.php';
2 months ago
class Pusher extends BaseHomeService
{
1 month ago
private static $client = null;
public static function getClient() {
if (self::$client === null) {
self::$client = new \Pusher\PushNotifications\PushNotifications(
array(
"instanceId" => env('PUSHER.INSTANCE_ID'),
"secretKey" => env('PUSHER.SECRET_KEY'),
)
);
}
return self::$client;
}
// 向一个或多个设备兴趣发布推送通知 $interest 必须是数组类型Array<string>,可以包含多个兴趣名称
2 months ago
public function publishToInterest($interest, $title, $body)
{
1 month ago
$beamsClient = self::getClient();
2 months ago
return $beamsClient->publishToInterests(
2 months ago
$interest, // 兴趣名称最多100个
2 months ago
[
"apns" => [
1 month ago
'aps' => [
2 months ago
"alert" => [
2 months ago
"title" => $title,
"body" => $body
2 months ago
]
]
],
"fcm" => [
"notification" => [
2 months ago
"title" => $title,
"body" => $body
2 months ago
]
],
"web" => [
"notification" => [
"title" => $title,
"body" => $body
]
]
]
2 months ago
);
}
2 months ago
// 发送到指定用户, 用户列表必须是数组,最大长度1000个用户
public function publishToUsers($userArr, $title, $body)
2 months ago
{
1 month ago
$beamsClient = self::getClient();
2 months ago
return $beamsClient->publishToUsers(
// array("user-001", "user-002"),
$userArr,
2 months ago
array(
"fcm" => array(
"notification" => array(
2 months ago
"title" => $title,
"body" => $body
2 months ago
)
),
"apns" => array("aps" => array(
"alert" => array(
2 months ago
"title" => $title,
"body" => $body
2 months ago
)
)),
"web" => array(
"notification" => array(
2 months ago
"title" => $title,
"body" => $body
2 months ago
)
)
2 months ago
)
);
2 months ago
}
2 months ago
1 month ago
// 生成Beams身份验证令牌,该令牌有效期为24小时 (客户端需要拿这个Token去请求Beams关联设备与用户ID) 注意:每个平台每个用户在任意时刻最多可关联100台设备,当用户退出应用时,可以调用客户端SDK中的.stop方法理解解除关联关系。
public function generateToken($userId)
{
1 month ago
if (is_numeric($userId)) {
$userId = strval($userId);
}
1 month ago
$beamsClient = self::getClient();
return $beamsClient->generateToken($userId);
}
2 months ago
2 months ago
}