【问题标题】:How to Send push notifications using One Signal + PHP + Server API?如何使用 One Signal + PHP + Server API 发送推送通知?
【发布时间】:2016-08-14 12:38:16
【问题描述】:

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

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

【问题讨论】:

  • 他们的网站上有一个 PHP 示例:documentation.onesignal.com/v2.0/docs/…。这就是你要找的吗?
  • offcourse,根据我完成的文件,但它显示这样的错误
  • 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\":[\"所有包含的玩家都没有订阅\"],\"warnings\":[ \"如果您想向 Android 播放器发送消息,您必须在 OneSignal 设置中配置 Android 通知。\"]}"}

标签: php android api server onesignal


【解决方案1】:
<?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");
?>

【讨论】:

  • 你好 Kishan...你能帮我做点什么吗?你是怎么得到那个 'data' => array("foo" => "bar") 的?它在 app.js 中吗?在哪里?我需要做什么?问候
  • @Japa 对我迟到的重播感到抱歉。我可以帮忙告诉我你不明白的地方。
  • 嗨,Kishan...谢谢你的回答,我明白了,我现在明白了,谢谢
  • 如何将通知标题设置为“内容”是正文?
  • @Alberto 是的,您可以查看 onesignal 的文档。
【解决方案2】:

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

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

  • 'app_id' 当前在 OneSignal 中称为 (OneSignal App ID) 设置->密钥和 ID

  • 在“授权:基本 xxx...”中,在应用 ID 下方的“REST API 密钥”之后

【讨论】:

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

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

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

【讨论】:

  • 如何将通知标题设置为“内容”是正文?
【解决方案4】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-19
    • 1970-01-01
    • 1970-01-01
    • 2015-11-23
    • 2018-12-31
    相关资源
    最近更新 更多