簡體   English   中英

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

[英]How to Send push notifications using One Signal + PHP + Server API?

我正在使用一個信號為 android 應用程序發送推送通知。 我的問題是

如何使用服務器休息 api 設置發送推送通知?

<?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");
?>

您可以隨時參考官方文檔:

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

  • “app_id”目前在 OneSignal Settings->Keys and IDs 中稱為 (OneSignal App ID)

  • 在“授權:基本 xxx...”中,通過 App ID 正下方的“REST API 密鑰”

我看到您已設置 isAndroid=true,但 OneSignal 返回一個錯誤,表明 ID 為eec33e8e-5774-4b74-9aae-37370778c4b2的應用程序未啟用 Android 通知。

確保您的應用 ID 正確,如果正確,請在 OneSignal 設置中啟用 Android 通知。

$to - 設備 ID

$title - 通知標題

$message - 通知消息

$img - 完整圖片鏈接

用法:

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

使用演示值:

sendnotification("Device_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;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM