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

9
我正在使用One Signal发送安卓应用的推送通知。我的问题是,如何设置使用服务器REST API发送推送通知?

他们的网站上有一个PHP示例:https://documentation.onesignal.com/v2.0/docs/notifications-create-notification。这是你要找的吗? - Schore
当然,根据文档我已经完成了,但它显示出以下错误。 - user3544256
发送的JSON数据:{"app_id":"eec33e8e-5774-4b74-9aae-37370778c4b2","included_segments":["All"],"send_after":"Fri May 02 2014 00:00:00 GMT-0700 (PDT)","data":{"foo":"bar"},"isAndroid":true,"contents":{"en":"English Message"}} 接收的JSON数据:{"allresponses":"{"id":"","recipients":0,"errors":["All included players are not subscribed"],"warnings":["You must configure Android notifications in your OneSignal settings if you wish to send messages to Android players."]}"} - user3544256
1
新链接为:https://documentation.onesignal.com/reference - Jason
4个回答

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

2
你好Kishan...你能帮我一下吗?你是怎么得到那个 'data' => array("foo" => "bar") 的?它在app.js里面吗?在哪里?我需要做什么?谢谢。 - Japa
@Japa 抱歉回复晚了。我可以帮忙,告诉我你哪里不明白。 - Kishan
嗨Kishan...谢谢你的回答,我明白了,无论如何还是谢谢。 - Japa
如何在通知中设置标题为“内容”,而消息正文为“内容”? - Alberto
@Alberto 是的,你可以查看 OneSignal 的文档。 - Kishan

4

文档包括许多语言的代码示例。例如“基于过滤器/标签发送 - 创建通知”。 Shell、JSON、PHP、C#(.NET标准)、C#(ASP.NET)、Ruby(Rails)、Python、NodeJS、Perl、Parse Cloud、GameSparks、Java。 - Alexey Muravyov

2

我看到你设置了isAndroid=true,但OneSignal返回一个错误,显示ID为eec33e8e-5774-4b74-9aae-37370778c4b2的应用程序没有启用Android通知。

请确保您的应用程序ID正确,并且如果正确,则在OneSignal设置中启用Android通知。


如何设置通知的标题为“内容”,而正文消息是什么? - Alberto

2

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接