1. 程式人生 > >hbuilder APP服務器端(C#)推送

hbuilder APP服務器端(C#)推送

ica image manage sap on() 推送消息 exec receive company

實現推送有多種方法和技術手段,我這邊是使用三方“個推”去實現對特定用戶的推送。我自己是關聯業務,對下一步任務代辦人進行消息通知。

1 、個推賬號申請和配置

  1.1、IOS需要推送證書 參考網址:http://www.applicationloader.net/blog/zh/397.html

  1.2 、"Hbuilder”“ 個推”配置參考:http://ask.dcloud.net.cn/article/34

2、代碼處理

2.1 C#後端

  

  private static String HOST = "http://sdk.open.api.igexin.com/apiex.htm
"; //https的域名 //private static String HOST = "https://api.getui.com/apiex.htm"; private static String APPID = ConfigurationManager.AppSettings["TSAPPID"].ToString();//"djNkkiQUDN7fmNwS0Lhr1"; private static String APPKEY = ConfigurationManager.AppSettings["TSAPPKEY"].ToString();//
"FMxsRBtmx1As7wHtaPeb43"; private static String MASTERSECRET = ConfigurationManager.AppSettings["TSMASTERSECRET"].ToString();//"3KKqkvwLzW8zrLmCvNC0S7"; public static void GetClientid(string code, string title, string text) { WriteLog("*****************************APP信息推送*****************************************
"); WriteLog("用戶:" + code); WriteLog(title + "內容:" + text); using (var db = new DbBase()) { string sql = string.Format("select top 1 code,clientid,billDate,company from SYSTEM_PUSH_ALIAS where code=‘{0}‘ order by billDate desc", code); var dt = RepositoryBase.ExecuteDataTable(db, sql); if (dt.Rows.Count > 0) { foreach (DataRow item in dt.Rows) { PushMessage(item["clientid"].ToString(), title, text); } } else { WriteLog("用戶:" + code + " 無對應clientid"); } } } public static object PushMessage(string CLIENTID, string title, string text) { return JObject.Parse(PushMessageToSingle(CLIENTID, title, text)); } /// <summary> /// 用戶對於的 推送CLIENTID /// </summary> /// <param name="CLIENTID"></param> private static string PushMessageToSingle(string CLIENTID, string title, string text) { IGtPush push = new IGtPush(HOST, APPKEY, MASTERSECRET); TransmissionTemplate template = TransmissionTemplateDemo(title, text); // 單推消息模型 SingleMessage message = new SingleMessage(); message.IsOffline = true; // 用戶當前不在線時,是否離線存儲,可選 message.OfflineExpireTime = 1000 * 3600 * 12; // 離線有效時間,單位為毫秒,可選 message.Data = template; //判斷是否客戶端是否wifi環境下推送,2為4G/3G/2G,1為在WIFI環境下,0為不限制環境 message.PushNetWorkType = 0; com.igetui.api.openservice.igetui.Target target = new com.igetui.api.openservice.igetui.Target(); target.appId = APPID; target.clientId = CLIENTID; //target.alias = ALIAS; try { String pushResult = push.pushMessageToSingle(message, target); WriteLog("-----------------------------------------------"); WriteLog("----------------服務端返回結果:" + pushResult); return pushResult; } catch (RequestException e) { String requestId = e.RequestId; //發送失敗後的重發 String pushResult = push.pushMessageToSingle(message, target, requestId); WriteLog("-----------------------------------------------"); WriteLog("----------------服務端返回結果:" + pushResult); return pushResult; } } //通知透傳模板動作內容 public static NotificationTemplate NotificationTemplateDemo(string title, string text) { NotificationTemplate template = new NotificationTemplate(); template.AppId = APPID; template.AppKey = APPKEY; //通知欄標題 template.Title = title; //通知欄內容 template.Text = text; //通知欄顯示本地圖片 template.Logo = ""; //通知欄顯示網絡圖標 template.LogoURL = ""; //應用啟動類型,1:強制應用啟動 2:等待應用啟動 template.TransmissionType = "1"; //透傳內容 template.TransmissionContent = text; //接收到消息是否響鈴,true:響鈴 false:不響鈴 template.IsRing = true; //接收到消息是否震動,true:震動 false:不震動 template.IsVibrate = true; //接收到消息是否可清除,true:可清除 false:不可清除 template.IsClearable = true; //APNPayload apnpayload = new APNPayload(); //SimpleAlertMsg alertMsg = new SimpleAlertMsg(text); //apnpayload.AlertMsg = alertMsg; //// apnpayload.Badge = 11; //apnpayload.ContentAvailable = 1; //apnpayload.addCustomMsg("payload", "payload"); //APN高級推送 APNPayload apnpayload = new APNPayload(); DictionaryAlertMsg alertMsg = new DictionaryAlertMsg(); alertMsg.Body = text; alertMsg.ActionLocKey = "ActionLocKey"; alertMsg.LocKey = "LocKey"; alertMsg.addLocArg("LocArg"); alertMsg.LaunchImage = "LaunchImage"; //iOS8.2支持字段 alertMsg.Title = title; alertMsg.TitleLocKey = "TitleLocKey"; alertMsg.addTitleLocArg("TitleLocArg"); apnpayload.AlertMsg = alertMsg; //apnpayload.Badge = 10; apnpayload.ContentAvailable = 1; //apnpayload.Category = ""; //apnpayload.Sound = "test1.wav"; apnpayload.addCustomMsg("payload", "payload"); template.setAPNInfo(apnpayload); template.setAPNInfo(apnpayload); //設置通知定時展示時間,結束時間與開始時間相差需大於6分鐘,消息推送後,客戶端將在指定時間差內展示消息(誤差6分鐘) String begin = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); String end = DateTime.Now.AddDays(1).ToString("yyyy-MM-dd HH:mm:ss"); template.setDuration(begin, end); return template; } //透傳模板動作內容 public static TransmissionTemplate TransmissionTemplateDemo(string title, string text) { TransmissionTemplate template = new TransmissionTemplate(); template.AppId = APPID; template.AppKey = APPKEY; //應用啟動類型,1:強制應用啟動 2:等待應用啟動 template.TransmissionType = "2"; //透傳內容 template.TransmissionContent = "{\"title\":\"" + title + "\",\"content\":\"" + text + "\",\"payload\":\"payload\"}"; //設置通知定時展示時間,結束時間與開始時間相差需大於6分鐘,消息推送後,客戶端將在指定時間差內展示消息(誤差6分鐘) //設置客戶端展示時間 //iOS簡單推送 APNPayload apnpayload = new APNPayload(); SimpleAlertMsg alertMsg = new SimpleAlertMsg(text); apnpayload.AlertMsg = alertMsg; // apnpayload.Badge = 11; apnpayload.ContentAvailable = 1; //apnpayload.Category = ""; apnpayload.Sound = "default"; apnpayload.addCustomMsg("payload", "payload"); template.setAPNInfo(apnpayload); //APN高級推送 //APNPayload apnpayload = new APNPayload(); //DictionaryAlertMsg alertMsg = new DictionaryAlertMsg(); //apnpayload.ContentAvailable = 1; //alertMsg.Body = "您有訂單需要審核"; //alertMsg.ActionLocKey = "ActionLocKey"; //alertMsg.LocKey = "LocKey"; //alertMsg.addLocArg("LocArg"); //alertMsg.LaunchImage = "LaunchImage"; //iOS8.2支持字段 //alertMsg.Title = "訂單提醒"; //alertMsg.TitleLocKey = "TitleLocKey"; //alertMsg.addTitleLocArg("TitleLocArg"); template.setAPNInfo(apnpayload); String begin = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); String end = DateTime.Now.AddDays(1).ToString("yyyy-MM-dd HH:mm:ss"); template.setDuration(begin, end); return template; } private static void WriteLog(string log) { CommonHelper.WriteLog(log, "_pushLog.txt"); }

2.2 hbuilder app端代碼:

    登錄後必須給每個用戶綁定一個“clientId”,此ID要存儲在服務器。在服務器給APP推送消息的時候需要更加用戶信息找到“clientId”:

      

ClientInfo =plus.push.getClientInfo();	
mui.getJSON(Url,{code:user.code,cli:ClientInfo.clientid},function(json){
		
    });
    

  

    IOS 應用在前臺的時候,在服務器推送消息APP端是沒有提示的。需要在代碼上做些處理。在登錄後的第一個頁面添加西門監聽事件:

if (mui.os.ios) {
			mui.plusReady(function(){				
    			plus.push.addEventListener("receive", function( msg ) {
        			if (msg.payload !=null && msg.payload!="") {          				
            			plus.push.createMessage(msg.content)
        			} 
    			}, false );
				
			});
			
		}

  

    

  

hbuilder APP服務器端(C#)推送