我参考了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();
});
});
所以我检查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)
,但我无法找到问题所在。
是否有人可以给我一些建议,谢谢。