【发布时间】:2016-01-20 03:16:22
【问题描述】:
我正在使用 this push plugin 构建 Jquery (2.x)、Jquery mobile (1.4.5) 和针对 Android 设备的 cordova (5.1.1)。
插件工作正常,我已经设法通过 php 将用户的设备注册 ID 发送到我的服务器到 mysql 数据库中。 此外,我还成功地通过 GCM Google 服务向注册的 id 发送了通知。
我用来执行此操作的代码是:
var app = {
initialize: function() {
this.bindEvents();
},
bindEvents: function() {
document.addEventListener('deviceready', this.onDeviceReady, false);
},
onDeviceReady: function() {
var push = PushNotification.init({
"android": {
"senderID": "xxxxxxxxx"
},
"ios": {"alert": "true", "badge": "true", "sound": "true"},
"windows": {}
});
push.on('registration', function(data) {
console.log("registration event");
var gcm_regid = data.registrationId;
$.post('http://xxxxx.xxx/insert.php', {gcm_regid: gcm_regid});
return false;
});
insert.php 是
<?php
$con = mysql_connect("localhost","gcm","password");
if (!$con) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db("gcm", $con);
$gcm_regid=$_POST['gcm_regid'];
$query=mysql_query("INSERT INTO gcm_users(gcm_regid) VALUES('$gcm_regid')");
if($query){
echo "Data for $gcm_regid inserted successfully!";
}
else{ echo "An error occurred!"; }
?>
我的 SQL 数据库结构如下:
id -> int(11) --> AUTO_INCREMENT
gcm_regid text
created_at --> timestamp -> CURRENT_TIMESTAMP
问题在于,每当用户在他的设备上启动应用程序时,注册 ID 都会再次发送到 mysql 数据库中,从而为相同的 registrationId 创建重复条目。
如何防止这种情况发生?
如何防止设备再次将 id 发送到数据库中? 有什么想法吗?
【问题讨论】:
标签: php android mysql cordova push-notification