如何使用One Signal + PHP + Server API发送推送通知?

时间:2016-04-21 10:54:16

标签: php android api server onesignal

我正在使用一个信号来发送Android应用的推送通知。我的问题是

如何使用server rest api设置发送推送通知?

4 个答案:

答案 0 :(得分:9)

<?PHP
function sendMessage(){
    $content = array(
        "en" => 'Testing Message'
        );

    $fields = array(
        'app_id' => "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxxx",
        'included_segments' => array('All'),
        'data' => array("foo" => "bar"),
        'large_icon' =>"ic_launcher_round.png",
        '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 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'));
    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");
?>

答案 1 :(得分:2)

我看到您设置了isAndroid = true,但OneSignal返回的错误显示标识为eec33e8e-5774-4b74-9aae-37370778c4b2的应用程序未启用Android通知。

确保您的应用ID是正确的,如果是,则在您的OneSignal设置中启用了Android通知。

答案 2 :(得分:2)

您可以随时参考官方文档:

https://documentation.onesignal.com/reference#section-example-code-create-notification

  • &#39; APP_ID&#39;目前在OneSignal中称为(OneSignal App ID) 设置 - &gt;键和ID

  • &#39;授权:基本xxx ......&#39;超过&#34; REST API密钥&#34;就在App ID

  • 下方

答案 3 :(得分:1)

$ to-设备ID

$ title-通知标题

$ message-通知消息

$ img-完整图片链接

用法:

sendnotification($ to,$ title,$ message,$ img);

具有演示值:

sendnotification(“设备ID”,“测试通知”,“测试消息”,“ https://www.google.co.in/images/branding/googleg/1x/googleg_standard_color_128dp.png”);

function sendnotification($to, $title, $message, $img)
{
    $msg = $message;
    $content = array(
        "en" => $msg
    );
    $headings = array(
        "en" => $title
    );
    if ($img == '') {
        $fields = array(
            'app_id' => 'YOUR_APP_ID',
            "headings" => $headings,
            'include_player_ids' => array($to),
            'large_icon' => "https://www.google.co.in/images/branding/googleg/1x/googleg_standard_color_128dp.png",
            'content_available' => true,
            'contents' => $content
        );
    } else {
        $ios_img = array(
            "id1" => $img
        );
        $fields = array(
            'app_id' => 'YOUR_APP_ID',
            "headings" => $headings,
            'include_player_ids' => array($to),
            'contents' => $content,
            "big_picture" => $img,
            'large_icon' => "https://www.google.co.in/images/branding/googleg/1x/googleg_standard_color_128dp.png",
            'content_available' => true,
            "ios_attachments" => $ios_img
        );

    }
    $headers = array(
        'Authorization: key=**APP_KEY**',
        'Content-Type: application/json; charset=utf-8'
    );
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, 'https://onesignal.com/api/v1/notifications');
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
    $result = curl_exec($ch);
    curl_close($ch);
    return $result;
}