如何根据用户位置发送通知 oneSignal

how to send notification based on users location oneSignal

所以我有这个功能,我希望它使用 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);


}

您的代码格式不符合文档中的要求: 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
        );