如何根据用户位置一个信号发送通知

问题描述 投票:1回答:1

所以我有这个功能,我希望它使用OneSignal REST API根据用户位置发送通知,这是我第一次处理通知和一个信号,因此我检查https://documentation.onesignal.com/docs我花了最后7个小时尝试:( ,我发现我可以使用过滤器对听众进行排序,所幸,我不知道如何使用它,对不起,如果我的英语不太清楚,请先谢谢my_function

public function sendNotification(){
   $content = array(
        "en" => 'Test Message'
        );

    $fields = array(
        'app_id' => "My_app_id",
        'filters' => array(array("field" => "location", "key" => "radius", "relation" => "=", "value" => "50"),array("operator" => "or"),array("field" => "location", "key" => "lat", "relation" => "=", "value" => "3.1946047"),array("operator" => "or"),array("field" => "location","key"=>"long", "relation" => "=", "value" => "30.2402744")),
        'data' => array("foo" => "bar"),
        '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 NmZhZDk4MDMtZTJlZ'));
    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 $return = json_encode( $return);


}
php laravel onesignal
1个回答
0
投票
您的代码未按照文档中的要求进行格式化:https://documentation.onesignal.com/reference#send-to-users-based-on-filters

此更正后的'filters'将过滤特定位置范围内的所有用户:

$fields = array( 'app_id' => "My_app_id", 'filters' => [ [ "field" => "location", "radius" => "50", "lat" => "3.1946047", "long" => "30.2402744" ] ], 'data' => array("foo" => "bar"), 'contents' => $content );

© www.soinside.com 2019 - 2024. All rights reserved.