private void SendGCM() { try { GCMSender sender = new GCMSender ( this.record.AppId, // API Key, 透過 google player API developer console 申請 this.record.SenderId // sender id, 透過 google player API developer console 申請 ); GCMNotificationData data = new GCMNotificationData(); data.Message = "有一則推播通知"; int status_code = -1; GCMResponse response = sender.SendNotificationV2 ( this.record.DeviceTokens, "", data, // send message, 大小不可超過4k 60 * 60 * 24 * 1, // 定義訊息在Server端可存活多久, 單位為秒 out status_code ); if (status_code == 200) { MessageBox.Show("send succeed"); } else { MessageBox.Show("get response failed: " + status_code); } } catch (Exception ex) { MessageBox.Show("error happen:" + ex.ToString()); } }
public bool ChkPhoneGCM(BaseInfo baseInfo) { PhoneGCMInfo info = (PhoneGCMInfo)baseInfo; JSONInfo Info1 = new JSONInfo(); subJSONInfo Info2 = new subJSONInfo(); ArrayList al = new ArrayList(); ArrayList a2 = new ArrayList(); ArrayList a3 = new ArrayList(); //ArrayList a3 = new ArrayList(); al.Add(info.AlertRuleID); a2.Add(info.DeviceID); IDataReader reader1 = DataProvider.Instance().ChkPhoneDriverInfoDB(a2);//FMS_PhoneDriverInfo_Fetch;return CarNo,Driver if (reader1.Read()) { Info1.type = "check"; //Info1.data.CarNo = reader1.GetString(0); //Info1.data.Driver = reader1.GetString(1); Info2.CarNo = reader1.GetString(0); Info2.Driver = reader1.GetString(1); } CBO.CloseDataReader(reader1, true); a2.Clear(); a2.Add(info.AlertRuleID); IDataReader reader2 = DataProvider.Instance().ChkPhoneRuleDB(a2);//FMS_PhoneRule_Fetch;return RuleName,AlarmType,Severity if (reader2.Read()) { //Info1.data.AlertDate = info.EventTime; //Info1.data.RuleName = reader2.GetString(0); //Info1.data.AlarmType = reader2.GetString(1); //Info1.data.Severity = reader2.GetString(2); Info2.AlertDate = info.EventTime; Info2.RuleName = reader2.GetString(0); Info2.AlarmType = reader2.GetString(1); Info2.Severity = reader2.GetString(2); } CBO.CloseDataReader(reader2, true); a2.Clear(); //string output2 = JsonConvert.SerializeObject(Info2); Info1.data = new string[] { JsonConvert.SerializeObject(Info2) }; string output = JsonConvert.SerializeObject(Info1); output = output.Replace("\\", "").Replace("[\"", "[").Replace("\"]", "]"); //Info1.type = "alert"; string output1 = output.Replace("check", "alert"); al.Add(info.DeviceID); IDataReader reader = DataProvider.Instance().ChkGCMDB(al);//FMS_PhoneGCM_Fetch;return UserId,RegisterID,Phone_Mail_Logic GCMSender gcm = null; string strResultJson, strResultJson1; while (reader.Read()) { info.userID = reader.GetInt32(0).ToString(); info.RegID = reader.GetString(1); info.Phone_Mail_Logic = reader.GetString(2); gcm = new GCMSender(info.RegID , "AIzaSyAI7vpx-Q3gQ1l-2R0jH00IqnVEbaQgSR8"); strResultJson = gcm.Send(output); if (strResultJson.IndexOf("registration_id") == -1) { strResultJson1 = gcm.Send(output1); //save log record CreatePushNoticeLog(info, output1, "Google"); } else { JObject obj = (JObject)JsonConvert.DeserializeObject(strResultJson); obj = (JObject)obj["results"][0]; if (obj["registration_id"] != null) { string newRegId = obj["registration_id"].ToString(); PhoneGCMInfo newGcmInfo = new PhoneGCMInfo(); newGcmInfo.RegID = info.RegID; newGcmInfo.newRegID = newRegId; newGcmInfo.userID = info.userID; newGcmInfo.Phone_Mail_Logic = info.Phone_Mail_Logic; newGcmInfo.WorkListAlertID = info.WorkListAlertID; newGcmInfo.DeviceID = info.DeviceID; newGcmInfo.AlertRuleID = info.AlertRuleID; a3.Add(newGcmInfo); } } } if (a3.Count > 0) { for (int i = 0; i < a3.Count; i++) { PhoneGCMInfo newGcmInfo = a3[i] as PhoneGCMInfo; a2.Clear(); a2.Add(newGcmInfo.RegID); a2.Add(newGcmInfo.newRegID); a2.Add(newGcmInfo.userID); a2.Add(newGcmInfo.Phone_Mail_Logic); int reader3 = DataProvider.Instance().ChkPhoneOverRegIDDB(a2); gcm = new GCMSender(newGcmInfo.newRegID, "AIzaSyAI7vpx-Q3gQ1l-2R0jH00IqnVEbaQgSR8"); strResultJson1 = gcm.Send(output1); //save log record newGcmInfo.RegID = newGcmInfo.newRegID; CreatePushNoticeLog(info, output1, "Google"); } } CBO.CloseDataReader(reader, true); return true; }