using com.xiaomi.xmpush.server;
using java.io;
using java.lang;
using java.net;
using java.text;
using Newtonsoft.Json;
using org.json.simple;
using sun.misc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using System.IO;
//小米的引用文件正在上传
namespace MIPushCSharpSample
{
class Program
{
private static string appSecret = "这里需要修改"; //用户在华为开发者联盟申请Push服务获取的服务参数
private static string appId = "这里需要修改"; //用户在华为开发者联盟申请Push服务获取的服务参数
private static string tokenUrl = "https://login.cloud.huawei.com/oauth2/v2/token"; //获取认证Token的URL
private static string apiUrl = "https://api.push.hicloud.com/pushsend.do"; //应用级消息下发API
private static string accessToken = string.Empty; //下发通知消息的认证Token
private static long tokenExpiredTime; //的accessToken的过期时间
static void Main(string[] args)
{
//MiPush(); //小米推送
string result = refreshToken(); //华为推送
System.Console.WriteLine(result);
}
private static string refreshToken()
{
Dictionary<string, string> dic = new Dictionary<string, string>();
dic.Add("grant_type", "client_credentials");
dic.Add("client_secret", URLEncoder.encode(appSecret, "UTF-8"));
dic.Add("client_id", appId);
var content = new FormUrlEncodedContent(dic);
var http = new HttpClient();
var response = http.PostAsync(tokenUrl, content);
string Text = response.Result.Content.ReadAsStringAsync().Result;
//Text = "{\"access_token\":\"CF1uIuDzo6B8+BjcR8F26 + n5deRC1SfBtAINGia6DbJLJ2pSHP10lthlXg8DGJyAFG9aUVqGSdkL2qXfvmNr4A == \",\"expires_in\":3600,\"token_type\":\"Bearer\"}";
HWclass hw = JsonConvert.DeserializeObject<HWclass>(Text);
accessToken = hw.access_token;
JSONArray deviceTokens = new JSONArray();
deviceTokens.add("这里需要修改成前端app获取到的华为手机的token")
;
JSONObject body = new JSONObject();
body.put("title", "这是标题");//消息标题
body.put("content", "这里是内容");
JSONObject param = new JSONObject();
param.put("appPkgName", "com.companyname.Raccoon");
JSONObject action = new JSONObject();
action.put("type", 3);//类型3为打开APP,其他行为请参考接口文档设置
action.put("param",param); //消息点击动作参数
JSONObject msg = new JSONObject();
msg.put("type", 3);//3: 通知栏消息,异步透传消息请根据接口文档设置
msg.put("action", action);//消息点击动作
msg.put("body", body);//通知栏消息body内容示例代码
JSONObject ext = new JSONObject();//扩展信息,含BI消息统计,特定展示风格,消息折叠。
ext.put("biTag", "Trump");//设置消息标签,如果带了这个标签,会在回执中推送给CP用于检测某种类型消息的到达率和状态
JSONObject temp = new JSONObject();
temp.put("season", "Spring");
temp.put("weather", "raining");
JSONArray customize = new JSONArray();
customize.add(temp);
ext.put("customize", customize);
JSONObject hps = new JSONObject();//华为PUSH消息总结构体
hps.put("msg", msg);
hps.put("ext", ext);
JSONObject payload = new JSONObject();
payload.put("hps", hps);
Dictionary<string, string> dics = new Dictionary<string, string>();
dics.Add("access_token",accessToken);
dics.Add("nsp_svc","openpush.message.api.send");
long currentTicks = DateTime.Now.Ticks;
DateTime dtFrom = new DateTime(1970, 1, 1, 0, 0, 0, 0);
long currentMillis = (currentTicks - dtFrom.Ticks) / 10000;
dics.Add("nsp_ts", (currentMillis / 1000).ToString());
dics.Add("device_token_list", deviceTokens.toString());
dics.Add("payload", payload.toString());
string postUrl = apiUrl +"?nsp_ctx="+URLEncoder.encode("{\"ver\":\"1\",\"appId\":\"100854197\"}", "UTF-8"); //如果推送不成功请把这行代码修改成 string postUrl = apiUrl +"?nsp_ctx"+URLEncoder.encode("={\"ver\":\"1\",\"appId\":\"100854197\"}", "UTF-8"); 让后跑一遍程序 再改成原来的,因为可能存在缓存原因
string url = URLDecoder.decode(postUrl, "UTF-8");
var contents = new FormUrlEncodedContent(dics);
var responses = http.PostAsync(postUrl, contents);
Text = responses.Result.Content.ReadAsStringAsync().Result;
return Text;
}
private static void MiPush()
{
try
{
Constants.useOfficial();//正式环境
//Constants.useSandbox();//测试环境,只针对IOS
string messagePayload = "这是一个消息";
string title = "通知标题";
string description = "通知说明" + DateTime.Now;
#region 安卓发送
Sender androidSender = new Sender("这里需要修改");//你的小米推送AppSecret
com.xiaomi.xmpush.server.Message androidMsg = new com.xiaomi.xmpush.server.Message.Builder()
.title(title)
.description(description)//通知栏展示的通知描述
.payload(messagePayload)//透传消息
.passThrough(0)//设置是否透传1:透传, 0通知栏消息
.notifyId(new java.lang.Integer(Convert.ToInt32((DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0)).TotalSeconds)))//取时间戳,避免通知覆盖
.restrictedPackageName("com.companyname.Raccoon")//包名
.notifyType(new java.lang.Integer(1)) //使用默认提示音提示
.notifyType(new java.lang.Integer(2)) //使用默认震动
.notifyType(new java.lang.Integer(3)) //使用默认LED灯光
.timeToLive(3600000 * 336)//服务器默认保留两周(毫秒)
.extra("data", "测试extra11111")//字符数不能超过1024最多十组
.build();
//广播
// com.xiaomi.xmpush.server.Result androidPushResult = androidSender.broadcastAll(androidMsg, 3);
//针对每个用户注册的registerid
string regId = "WqeDJkrMPcH/SSourlIWB+5QfE7vRFZ9I2j59p+1VAx0yjlT3s+rSw/IC3ge1lAF"; //修改成前端获取到的小米regId
com.xiaomi.xmpush.server.Result androidPushResult1 = androidSender.send(androidMsg, regId, 3);
#endregion
//result.rows = androidPushResult;
}
catch (System.Exception exception)
{
System.Console.WriteLine(exception.Message);
}
}
}
public class HWclass
{
public string access_token { get; set; }
public int expires_in { get; set; }
public string token_type { get; set; }
}
}