极光推送是一款不错的开源免费的产品,能满足一般需求,但是如果想要特殊需求,还是需要付费的。极光推送系统需要配合手机端实现消息的推送,所以能不能到达,也要看收集端的配置了,关于极光推送的如何配置,请阅读:(https://www.cnblogs.com/zpy1993-09/p/14750604.html)。
下面就是直接上代码了:
也就要一个消息推送的通用类:
using Jiguang.JPush; using Jiguang.JPush.Model; using System; using System.Collections.Generic; using System.Text; namespace Core.Net.Common.Core.Net.Core.JiGuang { public class registration_i { public List<string> registration_id = new List<string>(); } public class JiGuangHelper { // 极光推送 private static JPushClient client = new JPushClient("4f82eabcec25693495e4b346", "8dff2c43b3770418c9029092"); /// <summary> /// 极光推送传递一个参数 /// </summary> /// <param name="sTitle">标题</param> /// <param name="sContent">内容</param> /// <param name="name">参数字段</param> /// <param name="value">参数值</param> /// <param name="sAudience">all 代表广推 如果不是all,那么就是根据激光Id给特定人推送</param> /// <returns></returns> public static string PushMsg(string sTitle, string sContent, string name, string value, string sAudience) { object toUser = sAudience; if (sAudience != "all") { registration_i registration = new registration_i(); List<string> users = new List<string>(); users.Add(sAudience); registration.registration_id = users; toUser = registration; } PushPayload pushPayload = new PushPayload() { Platform = new List<string> { "android" }, Audience = toUser, Notification = new Notification { Alert = sContent, Android = new Android { Alert = sContent, Title = sTitle, Extras = new Dictionary<string, object> { [name] = value } } }, Message = new Message { Title = sTitle, Content = sContent, Extras = new Dictionary<string, string> { [name] = value } }, }; var response = client.SendPush(pushPayload); return response.Content; } /// <summary> /// 极光推送传递一个参数 /// </summary> /// <param name="sTitle">标题</param> /// <param name="sContent">内容</param> /// <param name="name">参数字段</param> /// <param name="value">参数值</param> /// <param name="users"></param> /// <returns></returns> public static string PushWarnMsg(string sTitle, string sContent, string name, string value, List<string> users) { registration_i registration = new registration_i(); registration.registration_id = users; PushPayload pushPayload = new PushPayload() { Platform = new List<string> { "android" }, Audience = registration, Notification = new Notification { Alert = sContent, Android = new Android { Alert = sContent, Title = sTitle, Extras = new Dictionary<string, object> { [name] = value } } }, Message = new Message { Title = sTitle, Content = sContent, Extras = new Dictionary<string, object> { [name] = value } } }; var response = client.SendPush(pushPayload); //FileHelper.AddSaveFile("UpFile/Content/Send.txt", DateTime.Now.ToString() + "调用极光推送: " + response.Content, "utf-8"); return response.Content; } public static string NewPushWarnMsg(string sTitle, string sContent, List<string> users) { registration_i registration = new registration_i(); registration.registration_id = users; PushPayload pushPayload = new PushPayload() { Platform = new List<string> { "android" }, Audience = registration, Notification = new Notification { Alert = sContent, Android = new Android { Alert = sContent, Title = sTitle, } }, Message = new Message { Title = sTitle, Content = sContent, } }; var response = client.SendPush(pushPayload); return response.Content; } } }