簡體   English   中英

我無法使用PHP,MySQL將消息發送給我的客戶端

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

我參考http://www.androidhive.info/2016/02/android-push-notifications-using-gcm-php-mysql-realtime-chat-app-part-1/我有一個關於發送單一的問題通知用戶

當我點擊將顯示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();
                    });
                });

我在PostMan中測試了API,api很好 在此輸入圖像描述

所以我檢查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) ,但我找不到問題所在。

有人能給我一些建議嗎,謝謝。

這是關於消息的數據庫: 在此輸入圖像描述

你好,先生Gcm已停止服務,現在你必須使用谷歌的Fcm。 在此輸入鏈接描述

Fcm與Gcm相同,用於推送通知。 謝謝

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM