我想在oneSignal中创建分段,而不必访问仪表板,所以我想知道它们是否有API或其他方式可以让我完成它
我想在oneSignal中创建分段,而不必访问仪表板,所以我想知道它们是否有API或其他方式可以让我完成它
目前OneSignal没有任何用于创建分段的API,但是如果您想向特定组发送通知,可以使用标签。
是的,您可以向特定标签发送通知,标签可用作段的替代品。如果您想向A段发送通知,请为该用户设置标签为{user:A},并可以使用此PHP请求发送通知。
$fields = array(
'app_id' => YOUR_ONE_SIGNAL_APP_ID,
//'included_segments' => array('plant_a'),
'filters' => array(array("field" => "tag", "key" => "user", "relation" => "=", "value" => "A")),
'data' => array("foo" => "bar"),
'contents' => $content
);
<?PHP
function sendMessage(){
$content = array(
"en" => 'English Message'
);
$fields = array(
'app_id' => YOUR_ONE_SIGNAL_APP_ID,
'filters' => array(array("field" => "tag", "key" => "user", "relation" => "=", "value" => "A")),
'data' => array("foo" => "bar"),
'contents' => $content
);
$fields = json_encode($fields);
print("\nJSON sent:\n");
print($fields);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://onesignal.com/api/v1/notifications");
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json; charset=utf-8',
'Authorization: Basic REST_API_KEY'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
$response = sendMessage();
$return["allresponses"] = $response;
$return = json_encode( $return);
print("\n\nJSON received:\n");
print($return);
print("\n");
?>
是的!这可以通过 OneSignal REST API 的 Create Segments 端点实现(参考)。
以下是一个示例:
curl -XPOST -H'Authorization: Basic YOUR_REST_API_KEY' -H'Content-Type: application/json' https://onesignal.com/api/v1/apps/YOUR_APP_ID/segments
-d '{"name": "1", "filters": [{"field": "session_count", "relation": ">", "value": "1"},{"operator": "AND"}, {"field": "tag", "relation": "!=", "key": "tag_key", "value": "1"},{"operator": "OR"}, {"field": "last_session", "relation": "<", "value": "30"}]}'
响应将会是这样的:
// Always true, "id" is uuid of created segment
{"success": true, "id": "7ed2887d-bd24-4a81-8220-4b256a08ab19"}