android - Send gcm Notifications to specific user -


i working on gcm.

i able send notifications registered android devices local server.

i have saving regid , userid in database. requirement send notifications particular devices.how that?

am using java on server side. answers save me..i posted server side code

server side code  public class gcmnotification extends httpservlet {         private static final long serialversionuid = 1l;          // put google api server key here         private static final string google_server_key = "aizasydzldr2viv-eghbfzgpjwxcdoqh24wt9ye";         static final string message_key = "message";             static final string title_key = "title";             static final string image_key= "image";                          static final string order_key= "order";        private list<string> androidtargets = new arraylist<string>();          public gcmnotification() {                 super();         //  androidtargets.add(re);          }          protected void doget(httpservletrequest request,                 httpservletresponse response) throws servletexception, ioexception {             //dopost(request, response);             connection con=null;              try {                  class.forname("com.mysql.jdbc.driver");                 //get connection particular database                  con=drivermanager.getconnection(                           "jdbc:mysql://localhost:3306/my_db","root","root1");                    string sql;                  sql = "select regid, fname, email my_db.persons";                 statement stmt = con.createstatement();                 resultset rs = stmt.executequery(sql);                  while(rs.next()){                     //retrieve column name                     int id  = rs.getint("regid");                     string first = rs.getstring("fname");                     string last = rs.getstring("email");                     androidtargets.add(id);                 }                       rs.close();                   stmt.close();                   con .close();              } catch (sqlexception e) {                 // todo auto-generated catch block                 e.printstacktrace();             } catch (classnotfoundexception e) {                 // todo auto-generated catch block                 e.printstacktrace();             }          }        protected void dopost(httpservletrequest request,             httpservletresponse response) throws servletexception, ioexception {          doget(request, response);            response.setcontenttype("text/html");            printwriter pw = response.getwriter();       //multicastresult result = null;         //result result1=null;         writer writer=null;         connection con=null;          printwriter out = response.getwriter();         string share = request.getparameter("shareregid");          // gcm redgid of android device send push notification     string reg = "";     string emailid="";     string fname="";      if (share != null && !share.isempty()) {             reg = request.getparameter("regid");             emailid=request.getparameter("email");             fname=request.getparameter("name");             system.out.println("regid: " + reg);             system.out.println("mailid " + emailid);             system.out.println("name "+ fname);              try {                 //load driver connection                 class.forname("com.mysql.jdbc.driver");                 //get connection particular database                  con=drivermanager.getconnection(                           "jdbc:mysql://localhost:3306/my_db","root","root1");                    preparedstatement pst=con.preparestatement("insert my_db.persons(regid,email,fname) values(?,?,?)");                 pst.setstring(1,reg);                     pst.setstring(2,emailid);                           pst.setstring(3,fname);                        int = pst.executeupdate();                     if(i!=0){                       pw.println("<br>record has been inserted");                       }                     else{                       pw.println("failed insert data");                      }                    } catch (sqlexception e) {                     // todo auto-generated catch block                     e.printstacktrace();                 } catch (classnotfoundexception e) {                     // todo auto-generated catch block                     e.printstacktrace();                 }               multicastresult result1=null;                 string usermessage = request.getparameter("message");                 string imageurl = request.getparameter("image");                 string order1=request.getparameter("odt");                 string titl=request.getparameter("tit");                 sender sender = new sender(google_server_key);                 message message = new message.builder().timetolive(10000)                          .delaywhileidle(false)                          .adddata(title_key, titl)                          .adddata(message_key,usermessage)                          .adddata(image_key, imageurl)                          .adddata(order_key, order1)                          .build();                   try {                     // use multicast messages.  second parameter                     hashset<string> set = new hashset<string>(androidtargets);                      // create arraylist set.                     arraylist<string> result = new arraylist<string>(set);                      system.out.println("reg2:"+result);                     // of sender.send() need array of register ids.                  result1 = sender.send(message, result,1);                  /*  if (result1.getresults() != null) {                         int canonicalregid = result1.getcanonicalids();                         if (canonicalregid != 0) {                           }                     } else {                         int error = result1.getfailure();                         system.out.println("broadcast failure: " + error);                     }*/                  } catch (exception e) {                     e.printstacktrace();                 }                    request.setattribute("pushstatus", result1.tostring());              request.getrequestdispatcher("index.jsp")                     .forward(request, response);  }      } 

simply add clause sql query filter out exact user want deal with. if want filter user email use this..

 sql = "select regid, fname, email my_db.persons email = 'abc@gmail.com'"; 

Comments