OneSignal - 从API添加用户

时间:2018-05-26 19:36:24

标签: php onesignal

我想通过API为push-msg添加用户OneSignal。这是可能的 ?如果是的话 - 如何?

感谢答案

我的代码返回“No Push Token”,我不知道如何解决这个问题

<?

$fields = array( 
    'app_id' => "", 
    'language' => "ru", 
    'timezone' => "-28800", 
    'device_type' => "5", 
); 

$fields = json_encode($fields); 


$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, "https://onesignal.com/api/v1/players"); 
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json')); 
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["allresponses"] = $response; 
$return = json_encode( $return); 

print($return); 



?>

1 个答案:

答案 0 :(得分:-2)

https://documentation.onesignal.com/reference#create-notification https://documentation.onesignal.com/reference#section-example-code-create-notification
仔细阅读那里给出的示例代码。
这是用于发送给所有订阅者的:

    function sendMessage() {
    $content      = array(
        "en" => 'English Message'
    );
    $hashes_array = array();
    array_push($hashes_array, array(
        "id" => "like-button",
        "text" => "Like",
        "icon" => "http://i.imgur.com/N8SN8ZS.png",
        "url" => "https://yoursite.com"
    ));
    array_push($hashes_array, array(
        "id" => "like-button-2",
        "text" => "Like2",
        "icon" => "http://i.imgur.com/N8SN8ZS.png",
        "url" => "https://yoursite.com"
    ));
    $fields = array(
        'app_id' => "5eb5a37e-b458-11e3-ac11-000c2940e62c",
        'included_segments' => array(
            'All'
        ),
        'data' => array(
            "foo" => "bar"
        ),
        'contents' => $content,
        'web_buttons' => $hashes_array
    );

    $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 NGEwMGZmMjItY2NkNy0xMWUzLTk5ZDUtMDAwYzI5NDBlNjJj'
    ));
    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;
}

对于特定细分受众群:

$fields = array(
        'app_id' => "5eb5a37e-b458-11e3-ac11-000c2940e62c",
        'included_segments' => array('Active Users'),
        'data' => array("foo" => "bar"),
        'contents' => $content
    );

对于Onesignal播放器ID:

$fields = array(
        'app_id' => "5eb5a37e-b458-11e3-ac11-000c2940e62c",
        'include_player_ids' => array("6392d91a-b206-4b7b-a620-cd68e32c3a76","76ece62b-bcfe-468c-8a78-839aeaa8c5fa","8e0f21fa-9a5a-4ae7-a9a6-ca1f24294b86"),
        'data' => array("foo" => "bar"),
        'contents' => $content
    );