onesignal / onesignal-php-api
本包最新版本(dev-main)没有提供许可证信息。
一种强大的方法,可以在大规模上发送个性化消息并构建有效的客户参与策略。更多详情请访问 onesignal.com
dev-main
2023-08-02 00:01 UTC
Requires
- php: ^7.3 || ^8.0
- ext-curl: *
- ext-json: *
- ext-mbstring: *
- guzzlehttp/guzzle: ^7.3
- guzzlehttp/psr7: ^1.7 || ^2.0
Requires (Dev)
This package is not auto-updated.
Last update: 2024-09-13 03:30:33 UTC
README
一种强大的方法,可以在大规模上发送个性化消息并构建有效的客户参与策略。更多详情请访问 onesignal.com
更多信息,请访问 https://onesignal.com.
安装与使用
需求
PHP 7.3 及以上版本。
Composer
要通过 Composer 安装绑定,请将以下内容添加到 composer.json
{ "repositories": [ { "type": "vcs", "url": "https://github.com/OneSignal/onesignal-php-api.git" } ], "require": { "OneSignal/onesignal-php-api": "*@dev" } }
然后运行 composer install
使用示例
导入
use DateTime; use onesignal\client\api\DefaultApi; use onesignal\client\Configuration; use onesignal\client\model\GetNotificationRequestBody; use onesignal\client\model\Notification; use onesignal\client\model\StringMap; use onesignal\client\model\Player; use onesignal\client\model\UpdatePlayerTagsRequestBody; use onesignal\client\model\ExportPlayersRequestBody; use onesignal\client\model\Segment; use onesignal\client\model\FilterExpressions; use PHPUnit\Framework\TestCase; use GuzzleHttp;
常量
const APP_ID = '<YOUR_APP_ID>'; const APP_KEY_TOKEN = '<YOUR_APP_KEY_TOKEN>'; const USER_KEY_TOKEN = '<YOUR_USER_KEY_TOKEN>';
配置授权
$config = Configuration::getDefaultConfiguration() ->setAppKeyToken(APP_KEY_TOKEN) ->setUserKeyToken(USER_KEY_TOKEN); $apiInstance = new DefaultApi( new GuzzleHttp\Client(), $config );
通知
创建通知模型
function createNotification($enContent): Notification { $content = new StringMap(); $content->setEn($enContent); $notification = new Notification(); $notification->setAppId(APP_ID); $notification->setContents($content); $notification->setIncludedSegments(['Subscribed Users']); return $notification; }
立即发送通知
$notification = createNotification('PHP Test notification'); $result = $apiInstance->createNotification($notification); print_r($result);
安排24小时后发送通知
$notification = self::createNotification('PHP Test scheduled notification'); $dt = new DateTime(); $dt->modify('+1 day'); $notification->setSendAfter($dt); $scheduledNotification = $apiInstance->createNotification($notification); print_r($scheduledNotification);
使用筛选器发送通知
只向未在 IAP 上花费任何美元的用户发送此通知。
$notification = createNotification('PHP Test filtered notification'); $filter1 = new Filter(); $filter1->setField('amount_spent'); $filter1->setRelation('='); $filter1->setValue('0'); $notification->setFilters([$filter1]); $result = $apiInstance->createNotification($notification); print_r($result);
立即发送通知
$notification = createNotification('PHP Test notification'); $result = $apiInstance->createNotification($notification); print_r($result);
检索通知
$scheduledNotification = $apiInstance->getNotification(APP_ID, $scheduledNotification->getId()); print_r($scheduledNotification);
按应用 ID 列出通知
$getResult = $apiInstance->getNotifications(APP_ID); print_r($getResult->getNotifications());
获取通知历史记录
$requestBody = new GetNotificationRequestBody(); $requestBody ->setAppId(APP_ID) ->setEvents('sent'); $getResult = $apiInstance->getNotificationHistory($scheduledNotification->getId(), $requestBody); print_r($getResult->getSuccess());
玩家
创建新的 Player 模型
function createPlayerModel($playerId): Player { $player = new Player(); $player->setAppId(APP_ID); $player->setIdentifier($playerId); $player->setDeviceType(1); return $player; }
创建 Player
$player = createPlayerModel('php_test_player_id'); $createPlayerResult = $apiInstance->createPlayer($player); print_r($createPlayerResult);
获取 Player
$getPlayerResult = $apiInstance->getPlayer(APP_ID, 'php_test_player_id'); print_r($getPlayerResult);
获取 Player 列表
$limit = 10; $getPlayersResult = $apiInstance->getPlayers(APP_ID, $limit); print_r($getPlayersResult->getPlayers());
删除玩家
$deletePlayerResult = $apiInstance->deletePlayer(APP_ID, 'php_test_player_id'); print_r($deletePlayerResult->getSuccess());
将玩家导出为 CSV 电子表格
$exportPlayersRequestBody = new ExportPlayersRequestBody(); $exportPlayersRequestBody->setExtraFields([]); $exportPlayersRequestBody->setSegmentName(''); $exportPlayersResult = $apiInstance->exportPlayers(APP_ID, $exportPlayersRequestBody); print_r($exportPlayersResult->getCsvFileUrl());
段
创建段
// Settings up the filter. Filters determine a segment. $filterExpressions = new FilterExpressions(); $filterExpressions->setField('session_count'); $filterExpressions->setRelation('>'); $filterExpressions->setValue('1');
设置段本身
$segment = new Segment(); $segment->setName('test_segment_name'); $segment->setFilters([$filterExpressions]); $createSegmentResponse = $apiInstance->createSegments(APP_ID, $segment); print_r($createSegmentResponse);
删除段
$deleteSegmentResponse = $apiInstance->deleteSegments(APP_ID, $createSegmentResponse->getId()); print_r($deleteSegmentResponse->getSuccess());
与应用程序协同工作
获取应用程序
$getAppResponse = $apiInstance->getApp(APP_ID); print_r($getAppResponse);
获取应用程序列表
$getAppsResponse = $apiInstance->getApps(); print_r($getAppsResponse);
更新应用程序
$getAppResponse = $apiInstance->getApp(APP_ID); $getAppResponse->setName('php_test_app_name'); $updateAppResponse = $apiInstance->updateApp(APP_ID, $getAppResponse); print_r($updateAppResponse);
结果
$outcomeNames = "os__session_duration.count,os__click.count"; $outcomeTimeRange = "1d"; $outcomePlatforms = "5"; $outcomeAttribution = "direct"; $outcomesResponse = $apiInstance->getOutcomes(APP_ID, $outcomeNames, null, $outcomeTimeRange, $outcomePlatforms, $outcomeAttribution); print_r($outcomesResponse->getOutcomes());
实时活动
开始实时活动
$activityId = "activity_id_example"; $beginLiveActivityRequest = new BeginLiveActivityRequest(array( 'push_token' => "push_token_example", 'subscription_id' => "player_id_example" )); self::$apiInstance->beginLiveActivity(APP_ID, $activityId, $beginLiveActivityRequest);
更新实时活动
$activityId = "activity_id_example"; $updateLiveActivityRequest = new UpdateLiveActivityRequest(array( 'event' => 'update', 'name' => 'contents', 'event_updates' => array('data' => 'test') )); self::$apiInstance->updateLiveActivity(APP_ID, $activityId, $updateLiveActivityRequest);
结束实时活动
$activityId = "activity_id_example"; $subscriptionId = "player_id_example"; self::$apiInstance->endLiveActivity(APP_ID, $activityId, $subscriptionId);
用户
创建用户
// Create user model $user = new User(); $aliasLabel = '<ALIAS_LABEL>'; $aliasId = '<ALIAS_ID>'; $pushToken = '<DEVICE_PUSH_TOKEN>'; $subscriptionObject = new SubscriptionObject(); $subscriptionObject->setType('iOSPush'); $subscriptionObject->setToken($pushToken); $user->setIdentity(array($aliasLabel => $aliasId)); $user->setSubscriptions([$subscriptionObject]); // Send model to API $createUserResponse = self::$apiInstance->createUser(APP_ID, $user);
通过别名获取用户
$fetchUserResponse = self::$apiInstance->fetchUser(APP_ID, $aliasLabel, $aliasId);
更新用户
$updateUserRequest = new UpdateUserRequest(array( 'properties' => array( 'language' => 'fr' )) ); $updateUserResponse = self::$apiInstance->updateUser(APP_ID, $aliasLabel, $aliasId, $updateUserRequest);
删除用户
self::$apiInstance->deleteUser(APP_ID, $aliasLabel, $aliasId);
创建订阅
$createSubscriptionRequestBody = new CreateSubscriptionRequestBody(); $subscriptionObject = new SubscriptionObject(); $subscriptionObject->setType('AndroidPush'); $subscriptionObject->setToken('DEVICE_PUSH_TOKEN'); $createSubscriptionRequestBody->setSubscription($subscriptionObject); $createSubscriptionResponse = self::$apiInstance->createSubscription(APP_ID, $aliasLabel, $aliasId, $createSubscriptionRequestBody);
更新订阅
$updateSubscriptionRequestBody = new UpdateSubscriptionRequestBody(); $subscriptionObject = new SubscriptionObject(); $subscriptionObject->setType('AndroidPush'); $subscriptionObject->setToken('DEVICE_PUSH_TOKEN' $updateSubscriptionRequestBody->setSubscription($subscriptionObject); self::$apiInstance->updateSubscription(APP_ID, $subscriptionId, $updateSubscriptionRequestBody);
删除订阅
self::$apiInstance->deleteSubscription(APP_ID, '<SUBSCRIPTION_ID>');
删除别名
self::$apiInstance->deleteAlias(APP_ID, '<ALIAS_LABEL>', '<ALIAS_ID>', '<ALIAS_LABEL_TO_DELETE>');
通过订阅 ID 获取别名
$fetchAliasesResponse = self::$apiInstance->fetchAliases(APP_ID, '<SUBSCRIPTION_ID>');
通过另一个别名获取别名
$fetchAliasesResponse = self::$apiInstance->fetchUserIdentity(APP_ID, '<ALIAS_LABEL>', '<ALIAS_ID>');
通过订阅 ID 识别用户
$userIdentityRequestBody = new UserIdentityRequestBody(); $userIdentityRequestBody->setIdentity(array( '<NEW_ALIAS_LABEL>' => '<NEW_ALIAS_ID>' )); // Act $fetchAliasesResponse = self::$apiInstance->identifyUserBySubscriptionId( APP_ID, '<SUBSCRIPTION_ID>', $userIdentityRequestBody);
通过另一个别名识别用户
$userIdentityRequestBody = new UserIdentityRequestBody(); $userIdentityRequestBody->setIdentity(array( '<NEW_ALIAS_LABEL>' => '<NEW_ALIAS_ID>' )); // Act $fetchAliasesResponse = self::$apiInstance->identifyUserByAlias( APP_ID, '<ALIAS_LABEL>', '<ALIAS_ID>', $userIdentityRequestBody);
将订阅转移到另一个用户
$transferSubscriptionRequestBody = new TransferSubscriptionRequestBody(); $transferSubscriptionRequestBody->setIdentity(array('<USER_FROM_ALIAS_LABEL>' => '<USER_FROM_ALIAS_ID>')); // Act $transferSubscriptionResponse = self::$apiInstance->transferSubscription( APP_ID, '<USER_TO_SUBSCRIPTION_ID>', $transferSubscriptionRequestBody);
获取应用内消息
$getEligibleIamsResponse = self::$apiInstance->getEligibleIams(APP_ID, '<SUBSCRIPTION_ID>');
API 端点
所有 URI 都相对于 https://onesignal.com/api/v1
模型
- App
- BasicNotification
- BasicNotificationAllOf
- BasicNotificationAllOfAndroidBackgroundLayout
- BeginLiveActivityRequest
- Button
- CancelNotificationSuccessResponse
- CreateNotificationSuccessResponse
- CreatePlayerSuccessResponse
- CreateSegmentConflictResponse
- CreateSegmentSuccessResponse
- CreateSubscriptionRequestBody
- CreateUserConflictResponse
- CreateUserConflictResponseErrorsInner
- CreateUserConflictResponseErrorsItemsMeta
- DeletePlayerNotFoundResponse
- DeletePlayerSuccessResponse
- DeleteSegmentNotFoundResponse
- DeleteSegmentSuccessResponse
- DeliveryData
- ExportEventsSuccessResponse
- ExportPlayersRequestBody
- ExportPlayersSuccessResponse
- Filter
- FilterExpressions
- GenericError
- GenericErrorErrorsInner
- GetNotificationRequestBody
- InlineResponse200
- InlineResponse2003
- InlineResponse201
- InlineResponse202
- InvalidIdentifierError
- Notification
- Notification200Errors
- NotificationAllOf
- NotificationHistorySuccessResponse
- NotificationSlice
- NotificationTarget
- NotificationWithMeta
- NotificationWithMetaAllOf
- Operator
- OutcomeData
- OutcomesData
- PlatformDeliveryData
- PlatformDeliveryDataEmailAllOf
- PlatformDeliveryDataSmsAllOf
- Player
- PlayerNotificationTarget
- PlayerNotificationTargetIncludeAliases
- PlayerSlice
- PropertiesDeltas
- PropertiesObject
- Purchase
- RateLimiterError
- Segment
- SegmentNotificationTarget
- StringMap
- SubscriptionObject
- TransferSubscriptionRequestBody
- UpdateLiveActivityRequest
- UpdateLiveActivitySuccessResponse
- UpdatePlayerSuccessResponse
- UpdatePlayerTagsRequestBody
- UpdatePlayerTagsSuccessResponse
- UpdateSubscriptionRequestBody
- UpdateUserRequest
- 用户
- UserIdentityRequestBody
- UserIdentityResponse
- 用户订阅选项
授权
所有OneSignal端点都需要app_key或user_key令牌进行授权。建议在初始配置初始化期间设置这两个密钥,这样您就不必担心哪个端点需要app_key,哪个端点需要user_key。您可以从应用仪表板和用户设置页面获取这些密钥的值。
app_key
- 类型:Bearer身份验证
user_key
- 类型:Bearer身份验证
作者
- API版本:
1.2.2
- 包版本:
2.0.2
- 包版本: