我无法使用PHP,MySQL将消息发送给我的客户端

时间:2017-03-28 06:37:30

标签: php mysql mysqli

我参考了http://www.androidhive.info/2016/02/android-push-notifications-using-gcm-php-mysql-realtime-chat-app-part-1/ 我有一个关于向用户发送单个通知的问题

当我点击将显示Sorry! Unable to post message

的按钮时
$('input#send_to_single_user').on('click', function () {
                    var msg = $('#send_to_single').val();
                    var to = $('.select_single').val();
                    if (msg.trim().length === 0) {
                        alert('Enter a message');
                        return;
                    }                                      

                    $('#send_to_single').val('');
                    $('#loader_single').show();


                    $.post("v1/users/" + to + '/message',
                            {user_id: user_id, message: msg},
                    function (data) {                                             
                        if (data.error === false) {
                            $('#loader_single').hide();
                            alert('Push notification sent successfully! You should see a Toast message on device.');
                        } else {                                                     
                            alert('Sorry! Unable to post message');
                        }
                    }).done(function () {

                    }).fail(function () {                          
                        alert('Sorry! Unable to send message');
                    }).always(function () {
                        $('#loader_single').hide();
                    });
                });

我在PostMan中测试了API,api很好 enter image description here

所以我检查API函数,问题可能是DataBase $response = $db->addMessage($from_user_id, $to_user_id, $message);

/*
 * Sending push notification to a single user
 * We use user's gcm registration id to send the message
 */
$app->post('/users/:id/message', function($to_user_id) {
    global $app;
    $db = new DbHandler();


    verifyRequiredParams(array('message'));

    $from_user_id = $app->request->post('user_id');
    $message = $app->request->post('message');

    $response = $db->addMessage($from_user_id, $to_user_id, $message);

    if ($response['error'] == false) {
        require_once __DIR__ . '/../libs/gcm/gcm.php';
        require_once __DIR__ . '/../libs/gcm/push.php';
        $gcm = new GCM();
        $push = new Push();

        $user = $db->getUser($to_user_id);

        $data = array();
        $data['user'] = $user;
        $data['message'] = $response['message'];
        $data['image'] = '';

        $push->setTitle('"Google Cloud Messaging"');
        $push->setIsBackground(FALSE);
        $push->setFlag(PUSH_FLAG_USER);
        $push->setData($data);

        //sending push message to single user
        $gcm->send($user['gcm_registration_id'], $push->getPush());

        $response['user'] = $user;
        $response['error'] = false;
    }
    echoRespnse(200, $response);

});

此处出现API错误消息:

//messagind in a chat room / to personal message
    public function addMessage($user_id, $chat_room_id, $message) {
        $response = array();

        $stmt = $this->conn->prepare("INSERT INTO messages (chat_room_id, user_id, message) values(?, ?, ?)");
        $stmt->bind_param("iis", $chat_room_id, $user_id, $message);

        $result = $stmt->execute();

        if ($result) {
            $response["error"] = false;
            //get the message
            $message_id = $this->conn->insert_id;
            $stmt = $this->conn->prepare("SELECT message_id, user_id, chat_room_id, message, created_at FROM messages WHERE message_id = ?");
            $stmt->bind_param("i", $message_id);
            if ($stmt->execute()) {
                $stmt->bind_result($message_id, $user_id, $chat_room_id, $message, $created_at);
                $stmt->fetch();
                $tmp = array();
                $tmp['message_id'] = $message_id;
                $tmp['chat_room_id'] = $chat_room_id;
                $tmp['message'] = $message;
                $tmp['created_at'] = $created_at;
                $response['message'] = $tmp;
            }
        } else {
            $response['error'] = true;
            $response['message'] = "Failed send message";
        }
        return $response;
    }

我认为问题是这段代码$stmt = $this->conn->prepare("INSERT INTO messages (chat_room_id, user_id, message),但我无法找到问题所在。

是否有人可以给我一些建议,谢谢。

这是关于消息的数据库: enter image description here

1 个答案:

答案 0 :(得分:0)

您好先生Gcm已停止其服务,现在您必须使用谷歌的Fcm。enter link description here

Fcm与Gcm相同,用于推送通知。 感谢