简体   繁体   English

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

[英]I can't send the message to my client in use PHP、MySQL

I take a reference with http://www.androidhive.info/2016/02/android-push-notifications-using-gcm-php-mysql-realtime-chat-app-part-1/ I have a issue about sending single notification to a user 我参考http://www.androidhive.info/2016/02/android-push-notifications-using-gcm-php-mysql-realtime-chat-app-part-1/我有一个关于发送单一的问题通知用户

When i click the button that will show Sorry! Unable to post message 当我点击将显示Sorry! Unable to post message的按钮Sorry! Unable to post message 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();
                    });
                });

I had tested the API in PostMan , the api is fine 我在PostMan中测试了API,api很好 在此输入图像描述

So i check the API function , may be the problem is DataBase $response = $db->addMessage($from_user_id, $to_user_id, $message); 所以我检查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);

});

The API error message is over here: 这里有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;
    }

I though that the problem is this code $stmt = $this->conn->prepare("INSERT INTO messages (chat_room_id, user_id, message) , but i can't find where the issue is. 我虽然问题是这个代码$stmt = $this->conn->prepare("INSERT INTO messages (chat_room_id, user_id, message) ,但我找不到问题所在。

Is anyone can give me some suggestions , thanks. 有人能给我一些建议吗,谢谢。

Here is my database about messages: 这是关于消息的数据库: 在此输入图像描述

Hello sir Gcm has stopped its services , now you have to use Fcm from the google. 你好,先生Gcm已停止服务,现在你必须使用谷歌的Fcm。 enter link description here 在此输入链接描述

the Fcm is Same as the Gcm and it is used for push notification. Fcm与Gcm相同,用于推送通知。 thanks 谢谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM