Pushnotifcation in Phongap android multiple regist

2019-01-20 20:21发布

问题:

I am trying to implement push notification using Phonegap in android using push plugin.Everything works fine ,but the problem with the device registration ids.Every time app launches it send an ajax request to the server to store the registration ids later it can be used for sending notification to the device.In server side i also checked if the device already registerd.The problem is same device have different registrion ids is generated so the server side checking fails (because same device have different registraion ids).Here is my push.js file

document.addEventListener("deviceready", onDeviceReady, false);

var pushNotification;

function onDeviceReady() {
    pushNotification = window.plugins.pushNotification;
    setupNotificationsForandroid();
}

function setupNotificationsForandroid() {

    if ( device.platform == 'android' || device.platform == 'Android' || device.platform == "amazon-fireos" ){

        pushNotification.register(
        successHandler,
        errorHandler,
        {
            "senderID":"527612045331",
            "ecb":"onNotification"
        });
    } else {
        pushNotification.register(
        tokenHandler,
        errorHandler,
        {
            "badge":"true",
            "sound":"true",
            "alert":"true",
            "ecb":"onNotificationAPN"
        });
    }
}

// Android
function onNotification(e) {
    switch( e.event )
    {
    case 'registered':
          if ( e.regid.length > 0 ){
           //set up the server call for storing registraion ids
             $.ajax({
                     url:'common_db.php',
                     data:'action='+'registergcm'+'&id='+e.regid,
                     success:function(data){

                            }
                     });
          }

    break;

    case 'message':
            // if this flag is set, this notification happened while we were in the foreground.
        if(e.foreground){
            var soundfile = e.soundname || e.payload.sound;
            var my_media = new Media("android/assets/www/"+ soundfile);
            my_media.play();
        }else{
           // otherwise we were launched because the user touched a notification in the notification tray.
        }


        break;

    case 'error':
           console.log("Error"+e.msg);
           break;

    default:
        console.log("An unknown event");
        return;
  }
}

in my php file

    //get id from application
     $reg_id=$_GET['id'];
//this check fails beacuse of differnt registration ids from the device
     $sql=mysql_query("select reg_id from tbl_insert_ids where id='$reg_id'");
     //check if already registerd
     if(mysql_num_rows($sql)==1){
       //do nothing
       echo '';

     }else{
       //store the registration ids in database table.
       $sql=mysql_query("insert into tbl_insert_ids values('','$reg_id')");
       if($sql){
         echo 'Success';
       }else{
         echo 'Error'
       }

     }

I am tested with one device my table has more than two rows of registrtion ids.Please help.