博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
百度云推送
阅读量:6127 次
发布时间:2019-06-21

本文共 8696 字,大约阅读时间需要 28 分钟。

展开
折叠using Baidu.Push.Api;using Baidu.Push.Api.Domain;using Baidu.Push.Api.Parser;using Baidu.Push.Api.Request;using Baidu.Push.Api.Util;using System;using System.Collections.Generic;using System.Configuration;using System.Linq;using System.Text;namespace RB.Common{    public class PushMessage    {        ///         /// 标题        ///         public string Title;        ///         /// 描述,描写;类型;说明书        ///         public string Desc;        ///         /// 发送时间        ///         public string Key = PushUtils.GetCurrentTimeMillis(DateTime.Now).ToString();        ///         /// 自定义内容        ///         public Dictionary
CustomContent; } public enum DeviceType : uint { ///
/// 浏览器设备 /// Browser = 1, ///
/// PC设备 /// PC = 2, ///
/// Andriod设备 /// Andriod = 3, iOS = 4, WindowsPhone = 5, } ///
/// 百度云推送类 /// public static class BaiDuPushHelper { ///
/// IOS状态 1 开发状态 2 生产状态 /// private static readonly string _apiKey = ConfigurationManager.AppSettings["ApiKey"]; private static readonly string _secretKey = ConfigurationManager.AppSettings["SecretKey"]; //DeployStatus:IOS部署状态 private static uint DEPLOY_STATUS = uint.Parse(ConfigurationManager.AppSettings["DeployStatus"]); public static readonly IPushClient PushClient; static BaiDuPushHelper() { PushClient = new DefaultPushClient(_apiKey, _secretKey); } public static List
GetBindList(string user_id) { var req = new QueryBindlistRequest(); req.UserId = user_id; var res = PushClient.Execute(req); return res.IsError ? new List
() : res.ResponseParams.Binds; } #region tag public static List
GetTags(string name = "") { var req = new FetchTagRequest() { Name = name }; var res = PushClient.Execute(req); return res.IsError ? new List
() : res.ResponseParams.Tags; } public static bool SetTag(string tag, string user_id = "") { var req = new SetTagRequest(); req.Tag = tag; req.UserId = user_id; var res = PushClient.Execute(req); return !res.IsError; } public static bool DeleteTag(string tag, string user_id = "") { var req = new DeleteTagRequest(); req.Tag = tag; req.UserId = user_id; var res = PushClient.Execute(req); return !res.IsError; } public static List
QueryUserTags(string user_id) { var req = new QueryUserTagsRequest() { UserId = user_id }; var res = PushClient.Execute(req); return res.IsError ? new List
() : res.ResponseParams.Tags; } #endregion #region push msg by tag public static int PushNotificationByTag(DeviceType deviceType, PushMessage msg, string tag) { var req = new PushMsgRequest(); req.Tag = tag; req.DeviceType = (uint)deviceType; req.PushType = 2; req.MessageType = 1; req.MsgKeys = msg.Key; req.Messages = GetNotification(deviceType, msg); if (deviceType == DeviceType.iOS) req.DeployStatus = DEPLOY_STATUS; //IOS需指定部署状态 var res = PushClient.Execute(req); return res.IsError ? 0 : res.ResponseParams.SuccessAmount; } public static int PushMessageByTag(DeviceType deviceType, PushMessage msg, string tag) { //IOS仅支持通知 if (deviceType == DeviceType.iOS) return PushNotificationByTag(deviceType, msg, tag); var req = new PushMsgRequest(); req.Tag = tag; req.DeviceType = (uint)deviceType; req.PushType = 2; req.MessageType = 0; req.MsgKeys = msg.Key; req.Messages = msg.Desc; var res = PushClient.Execute(req); return res.IsError ? 0 : res.ResponseParams.SuccessAmount; } #endregion #region push msg by user public static int PushMessage(string user_id, string msg, string key) { var user = GetBindList(user_id).FirstOrDefault(); return user != null ? PushMessage(user, msg, key) : 0; } ///
/// 推送消息 /// public static int PushMessage(QueryBindItem user, string msg, string key) { //IOS仅支持通知 if (user.DeviceType == (uint)DeviceType.iOS) return PushNotification(user, msg, key); var req = new PushMsgRequest(); req.UserId = user.UserId; req.DeviceType = user.DeviceType; req.PushType = 1; req.MsgKeys = key; req.Messages = msg; if (user.DeviceType == (uint)DeviceType.Andriod) { //Android可以指定某一个特定client req.ChannelId = user.ChannelId; } var res = PushClient.Execute(req); return res.IsError ? 0 : res.ResponseParams.SuccessAmount; } public static int PushNotification(string user_id, PushMessage msg) { var user = GetBindList(user_id).FirstOrDefault(); return user != null ? PushNotification(user, msg) : 0; } ///
/// 推送通知 /// public static int PushNotification(QueryBindItem user, PushMessage msg) { string message = GetNotification((DeviceType)user.DeviceType, msg); return PushNotification(user, message, msg.Key); } private static int PushNotification(QueryBindItem user, string msg, string key) { var req = new PushMsgRequest(); req.UserId = user.UserId; req.DeviceType = user.DeviceType; req.PushType = 1; req.MessageType = 1; req.Messages = msg; req.MsgKeys = key; if (user.DeviceType == (uint)DeviceType.Andriod) { //Android可以指定某一个特定client req.ChannelId = user.ChannelId; } else if (user.DeviceType == (uint)DeviceType.iOS) { //IOS需指定部署状态 req.DeployStatus = DEPLOY_STATUS; } var res = PushClient.Execute(req); return res.IsError ? 0 : res.ResponseParams.SuccessAmount; } #endregion #region push all ///
/// 推送通知给所有人 /// public static JsonResponse PushAllNotification(DeviceType deviceType, PushMessage msg) { var req = new PushMsgRequest(); req.DeviceType = (uint)deviceType; req.PushType = 3; req.MessageType = 1; req.MsgKeys = msg.Key; req.Messages = GetNotification(deviceType, msg); if (deviceType == DeviceType.iOS) req.DeployStatus = DEPLOY_STATUS; //IOS需指定部署状态 var res = PushClient.Execute(req); JsonResponse jsonResponse = new JsonResponse(); if (res.IsError == false) { jsonResponse.status = 0; jsonResponse.message = "成功"; jsonResponse.data = res.Body; } else { jsonResponse.status = 1; jsonResponse.message = "失败"; jsonResponse.data = res.Body; } return jsonResponse; //return res.IsError ? 0 : res.ResponseParams.SuccessAmount; } ///
/// 推送消息给所有人 /// //public static int PushAllMessage(DeviceType deviceType, PushMessage msg) //{ // //IOS仅支持通知 // if (deviceType == DeviceType.iOS) return PushAllNotification(deviceType, msg); // var req = new PushMsgRequest(); // req.DeviceType = (uint)deviceType; // req.PushType = 3; // req.MessageType = 0; // req.MsgKeys = msg.Key; // req.Messages = msg.Desc; // var res = PushClient.Execute(req); // return res.IsError ? 0 : res.ResponseParams.SuccessAmount; //} #endregion #region GetNotificationString internal static string GetNotification(DeviceType deviceType, PushMessage msg) { return GetNotification(deviceType, msg.Title, msg.Desc, msg.CustomContent); } internal static string GetNotification(DeviceType deviceType, string title, string desc, Dictionary
dic) { if (deviceType == DeviceType.Andriod) { var android = new AndroidNotification(); android.Title = title; android.Description = desc; android.NotificationBuilderId = 0; android.NotificationBasicStyle = 0x07; android.CustomContent = dic; return PushJsonParser.Serialize(android); } else { if (dic == null) dic = new Dictionary
(); if (!string.IsNullOrEmpty(title)) dic["title"] = title; dic["description"] = desc; return PushJsonParser.Serialize(dic); } } #endregion }}

 

调用方法

///         /// 推送通知给所有人        ///         public static JsonResponse PushAllNotification(DeviceType deviceType, PushMessage msg)        {            var req = new PushMsgRequest();            req.DeviceType = (uint)deviceType;            req.PushType = 3;            req.MessageType = 1;            req.MsgKeys = msg.Key;            req.Messages = GetNotification(deviceType, msg);            if (deviceType == DeviceType.iOS) req.DeployStatus = DEPLOY_STATUS; //IOS需指定部署状态            var res = PushClient.Execute(req);            JsonResponse jsonResponse = new JsonResponse();            if (res.IsError == false)            {                jsonResponse.status = 0;                jsonResponse.message = "成功";                jsonResponse.data = res.Body;            }            else            {                jsonResponse.status = 1;                jsonResponse.message = "失败";                jsonResponse.data = res.Body;            }            return jsonResponse;            //return res.IsError ? 0 : res.ResponseParams.SuccessAmount;        }

 

参考:

转载地址:http://gmfua.baihongyu.com/

你可能感兴趣的文章
利用tornado实现表格文件预览
查看>>
微信自定义分享
查看>>
Spring 中获取 request 的几种方法,及其线程安全性分析
查看>>
SpiderData 2019年2月14日 DApp数据排行榜
查看>>
PAT A1104
查看>>
软件测试的艺术第六章总结
查看>>
leetcode394. Decode String
查看>>
iOS开发之有间距的UITableViewCell
查看>>
区块链教程Fabric1.0源代码分析Peer peer根命令入口及加载子命令一
查看>>
SSH框架之SpringMVC文件上传功能代码
查看>>
08.自定义方法以及直接访问java类方法---《Beetl视频课程》
查看>>
FFmpeg在Intel GPU上的硬件加速与优化
查看>>
PDCA + GTD + 番茄工作法
查看>>
阿里云超算揭秘:虚拟机的心脏,物理机的肌肉
查看>>
Edge 浏览器有秘密白名单,允许 Facebook 运行 Flash 代码
查看>>
css 居中几种方法
查看>>
vue双向绑定的实现原理
查看>>
03.HTML5(音频)
查看>>
FileZilla Client 3.41.1 发布,流行的 FTP 解决方案
查看>>
中国唯一的“国际数字化转型专家”,阿里云获Forrester认可
查看>>