本文介绍了OneSignal - 从 API 添加用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用用于 push-msg 的 API 将用户添加到 OneSignal.这是可能的 ?如果是 - 如何?

I wanna add user to OneSignal with API for push-msg. Its possible ? If yes - how to ?

感谢回答

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

my code return "No Push Token" and i dont't khow how to fix this problem

<?

$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); 



?>

推荐答案

https://documentation.onesignal.com/reference#create-notificationhttps://documentation.onesignal.com/reference#section-example-代码创建通知
浏览那里给出的示例代码.
这是发送给所有订阅者:

https://documentation.onesignal.com/reference#create-notificationhttps://documentation.onesignal.com/reference#section-example-code-create-notification
Go through the sample code given there.
This is for Sending to all subscribers :

    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:

For Onesignal player IDs:

$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
    );

这篇关于OneSignal - 从 API 添加用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-17 12:23