OneSignal PHP SDK v2 版本实现指南
本文介绍了使用OneSignal v2 PHP SDK实现推送通知功能的方法。主要内容包括:1)安装SDK并配置初始化;2)实现发送推送通知功能,包括全量推送、指定用户推送和带按钮通知;3)管理用户设备信息;4)查看和取消通知状态;5)提供完整使用示例和错误处理最佳实践。文中还强调了API调用限制、错误处理、用户隐私和测试等注意事项,为开发者提供了完整的OneSignal推送功能实现方案。
OneSignal v2 API 提供了更强大的推送通知功能。以下是 PHP SDK 的实现指南:
1. 安装 OneSignal PHP SDK
composer require onesignal/onesignal-php-sdk
2. 基础配置和初始化
<?php
require_once 'vendor/autoload.php';use OneSignal\Config;
use OneSignal\OneSignal;class OneSignalService {private $client;public function __construct() {$config = new Config();$config->setApplicationId('your-app-id');$config->setApplicationAuthKey('your-rest-api-key');$config->setUserAuthKey('your-user-auth-key'); // 可选$this->client = new OneSignal($config);}// 更多方法...
}
?>
3. 发送推送通知
发送给所有用户
public function sendToAllUsers($message, $data = []) {try {$notification = $this->client->notifications->add(['contents' => ['en' => $message],'included_segments' => ['All'],'data' => $data,'isAnyWeb' => true, // 针对Web用户'isChromeWeb' => true,'isFirefox' => true]);return $notification;} catch (Exception $e) {error_log('OneSignal Error: ' . $e->getMessage());return false;}
}
发送给特定用户
public function sendToSpecificUsers($message, $playerIds, $data = []) {try {$notification = $this->client->notifications->add(['contents' => ['en' => $message],'include_player_ids' => $playerIds,'data' => $data]);return $notification;} catch (Exception $e) {error_log('OneSignal Error: ' . $e->getMessage());return false;}
}
发送带按钮的通知
public function sendWithButtons($message, $buttons, $segments = ['All']) {try {$notification = $this->client->notifications->add(['contents' => ['en' => $message],'included_segments' => $segments,'buttons' => $buttons,'web_buttons' => $buttons // 针对Web的按钮]);return $notification;} catch (Exception $e) {error_log('OneSignal Error: ' . $e->getMessage());return false;}
}
4. 管理用户和设备
获取设备信息
public function getDeviceInfo($deviceId) {try {$device = $this->client->devices->getOne($deviceId);return $device;} catch (Exception $e) {error_log('OneSignal Error: ' . $e->getMessage());return false;}
}
更新设备信息
public function updateDevice($deviceId, $data) {try {$result = $this->client->devices->update($deviceId, $data);return $result;} catch (Exception $e) {error_log('OneSignal Error: ' . $e->getMessage());return false;}
}
5. 查看通知状态
public function getNotificationStatus($notificationId) {try {$status = $this->client->notifications->getOne($notificationId);return $status;} catch (Exception $e) {error_log('OneSignal Error: ' . $e->getMessage());return false;}
}public function getNotifications($limit = 50, $offset = 0) {try {$notifications = $this->client->notifications->getAll($limit, $offset);return $notifications;} catch (Exception $e) {error_log('OneSignal Error: ' . $e->getMessage());return false;}
}
6. 取消通知
public function cancelNotification($notificationId) {try {$result = $this->client->notifications->cancel($notificationId);return $result;} catch (Exception $e) {error_log('OneSignal Error: ' . $e->getMessage());return false;}
}
7. 完整的使用示例
<?php
// 配置
define('ONESIGNAL_APP_ID', 'your-app-id');
define('ONESIGNAL_REST_API_KEY', 'your-rest-api-key');// 初始化服务
$oneSignalService = new OneSignalService(ONESIGNAL_APP_ID, ONESIGNAL_REST_API_KEY);// 发送给所有用户
$notification = $oneSignalService->sendToAllUsers('Hello World!', ['custom_data' => 'value']
);// 发送给特定用户
$playerIds = ['player-id-1', 'player-id-2'];
$notification = $oneSignalService->sendToSpecificUsers('Personalized message',$playerIds,['url' => 'https://example.com']
);// 发送带按钮的通知
$buttons = [['id' => 'btn1', 'text' => 'Button 1', 'icon' => 'icon1'],['id' => 'btn2', 'text' => 'Button 2', 'icon' => 'icon2']
];
$notification = $oneSignalService->sendWithButtons('Message with buttons',$buttons
);if ($notification) {echo "Notification sent successfully. ID: " . $notification['id'];
} else {echo "Failed to send notification";
}
?>
8. 错误处理最佳实践
class OneSignalService {// ... 其他代码private function handleException(Exception $e) {// 记录详细错误信息error_log('OneSignal API Error: ' . $e->getMessage());// 根据错误类型进行不同处理if ($e instanceof \OneSignal\Exception\OneSignalException) {// OneSignal 特定错误return ['error' => 'OneSignal error: ' . $e->getMessage()];} else {// 网络或其他错误return ['error' => 'Service temporarily unavailable'];}}public function sendNotificationSafely($params) {try {return $this->client->notifications->add($params);} catch (Exception $e) {return $this->handleException($e);}}
}
注意事项
- API 限制:OneSignal 有发送频率限制,请确保不要超过限制
- 错误处理:始终实现适当的错误处理机制
- 用户隐私:确保遵守相关隐私法规
- 测试:在发送大量通知前,先进行测试
这个实现提供了 OneSignal v2 PHP SDK 的核心功能,你可以根据具体需求进行扩展和调整。