将谷歌注册id存储到我的数据库中

store Google register id to my database

本文关键字:我的 数据库 存储 谷歌 注册 id      更新时间:2023-09-26

你好,我是phonegap的新手,我想实现通知,所以我使用了cordova插件添加https://github.com/phonegap-build/PushPlugin.git为了实现,现在我获得了注册id和成功状态OK,但我想存储该REG_id以备将来使用我在获得注册id后就使用了ajax发布方法,但我无法发布请帮我

<!DOCTYPE HTML>
<html>
<head>
<script type="text/javascript" charset="utf-8" src="cordova.js"></script>
<script type="text/javascript" charset="utf-8" src="jquery_1.5.2.min.js"></script>
<script type="text/javascript" src="PushNotification.js"></script>
<script type="text/javascript">
 var pushNotification;
  function onLoad() {
     document.addEventListener("deviceready", onDeviceReady, false);
  }
function onDeviceReady(){
    pushNotification = window.plugins.pushNotification;
    if ( device.platform == 'android' || device.platform == 'Android' ){
    pushNotification.register(
        successHandler,
        errorHandler, {
            "senderID":"1234560",
            "ecb":"onNotificationGCM"
        });
    }
}
// Android
function onNotificationGCM(e) {
    $("#app-status-ul").append('<li>EVENT -> RECEIVED:' + e.event + '</li>');
    switch( e.event )
    {
    case 'registered':
        if ( e.regid.length > 0 )
        {
                    $.ajax({
                    url: "http://abcd.com/store.php",
                    context: document.body,
                  cache : false,
                  data: {
                       username : e.regid,
                  },
                  success: function ( data ) {
                    alert("regID  success");
                 }
           });
                $("#app-status-ul").append('<li>REGISTERED ID is : -> REGID:' + e.regid + "</li>");
                console.log("regID = " + e.regid);
        }
        break;
    case 'message':
        if ( e.foreground )
        {
            $("#app-status-ul").append('<li>--INLINE NOTIFICATION--' + '</li>');
            var my_media = new Media("/android_asset/www/"+e.soundname);
            my_media.play();
        }
        else
        { 
            if ( e.coldstart )
            {
                $("#app-status-ul").append('<li>--COLDSTART NOTIFICATION--' + '</li>');
            }
            else
            {
                $("#app-status-ul").append('<li>--BACKGROUND NOTIFICATION--' + '</li>');
            }
        }
        $("#app-status-ul").append('<li>MESSAGE -> MSG: ' + e.payload.message + '</li>');
        $("#app-status-ul").append('<li>MESSAGE -> MSGCNT: ' + e.payload.msgcnt + '</li>');
    break;
    case 'error':
        $("#app-status-ul").append('<li>ERROR -> MSG:' + e.msg + '</li>');
    break;
    default:
        $("#app-status-ul").append('<li>EVENT -> Unknown, an event was received
     and we do not know what it is</li>');
    break;
  }
}
function successHandler (result) {
                $("#app-status-ul").append('<li>success ID:'+ result +'</li>');

}
function errorHandler (error) {
        $("#app-status-ul").append('<li>error:'+ error +'</li>');
}
</script>
<title>com.PhoneGap.c2dm</title>
</head>
<body onload="onLoad()">
<div id="home">
    <div id="app-status-div">
        <ul id="app-status-ul">
            <li>Cordova PushNotification Plugin Demo</li>
        </ul>
    </div>
</div>

试试这个

$.ajax({
        url:'http://abcd.com/store.php,
        data:'key='+e.regid,
        success:function(data){
        alert(data);
      }
});

在您的店铺中.php

//read the regid from device
    $key=$_GET['key'];
    //perform your query
    $ins_qry=mysql_query();  
   if($insq_qry)
    echo 'Registration success';
   else
   echo 'Error occured';