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
When i click the button that will show 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
So i check the API function , may be the problem is 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:
//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.
Is anyone can give me some suggestions , thanks.
Hello sir Gcm has stopped its services , now you have to use Fcm from the google. enter link description here
the Fcm is Same as the Gcm and it is used for push notification. thanks
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.