如何在curl php中发送推送通知时获取注册ID

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

基本上......这里有两个文件,一个是curlphp脚本,另一个是angular1 js文件。

在js文件中,当管理员用户点击“发送通知”时,会触发一个事件,以便通过函数调用curl来发送消息。

该功能看起来像这样

    $scope.notify = function(title, content, ¿¿ userId ??){

    $.ajax({
        url: 'app/backend/src/curl-service.php',
        type: 'POST',
        data: {
            userId: 'the problem is here', 
            title: title, 
            message: content
        },
        success: function(data) {
            console.log('time to use curl service');
        },
        error: function(){
            console.log('Error! you can't use curl service');
        }
    });

    };

正如您所看到的,我使用ajax传递一些数据来填充将由此curl-service.php文件推送的通知内容

<?php
// Incluimos el api asignada al app
define('API_ACCESS_KEY', 'AIzaSyAJvT_Tx7vwZzViWkwUcQHdhx2osTiSXHA');

$registrationIds = array($_POST['userId']);
$title = array($_POST['title']);
$message = array($_POST['message']);

// preparamos los array
$msg = array
(
    'title'     => $title,
    'message'   => $message,
    'sound'     => default,
);
$fields = array
(
    'registration_ids'  => $registrationIds,
    'data'          => $msg
);

$headers = array
(
    'Content-Type: application/json',
    'Authorization: key=' . API_ACCESS_KEY
);

//iniciamos el servicio conectando con la url
$ch = curl_init();
curl_setopt( $ch,CURLOPT_URL, 'https://fcm.googleapis.com/fcm/send');
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);
echo $result;

//ejecutamos el servicio
$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

//verificamos posibles errores y se genera la respuesta 
if ($err) {
    echo "Se ha producido el siguiente error:" . $err;
} else {
    echo $response;
}
?> 

我真正需要知道的是,我如何获得注册ID,以便我也可以在我的php文件中使用它

javascript php angularjs ajax curl
1个回答
1
投票

你在做错了就是这里的代码:

$registrationIds = array($_POST['userId']);
$title = array($_POST['title']);
$message = array($_POST['message']);

// preparamos los array
$msg = array
(
    'title'     => $title,
    'message'   => $message,
    'sound'     => default,
);
$fields = array
(
    'registration_ids'  => $registrationIds,
    'data'          => $msg
)

您正在从POST数据创建数组,然后在之后使用字符串作为字符串,如果您将第一位更改为:

$registrationIds = $_POST['userId'];
$title = $_POST['title'];
$message = $_POST['message'];

或者更好的安全性:

$registrationIds = filter_input(INPUT_POST, 'userId', FILTER_SANITIZE_STRING);
$title = filter_input(INPUT_POST, 'title', FILTER_SANITIZE_STRING);
$message = filter_input(INPUT_POST, 'message', FILTER_SANITIZE_STRING);

你应该好好去

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