uni-app 推送功能unipush的.net详细用法讲解

一推送API

个推为开发者提供了如下3种消息推送方式:

  • toSingle :简称“单推”,指向单个用户推送消息

  • toList:简称“批量推”,指向指定的一批用户推送消息

  • toApp:简称“群推”,指向APP符合筛选条件的所有用户推送消息,支持定速推送、定时推送,支持条件的交并补功能

  • 【toSingle】执行cid单推

  • 向单个用户推送消息,可根据cid指定用户

  • 接口地址: BaseUrl/push/single/cid

  • 请求方式: POST

  • Request请求说明:

  • Header参数说明

  • 名称 类型 是否必须 默认值 说明
    token String 接口访问凭据,获取方式请参考获取鉴权token
  • body参数说明

  • content-type:application/json;charset=utf-8

  • 参数示例

    { "request_id":"xxx", "settings":{ "ttl":3600000 }, "audience":{ "cid":[ "xxx" ] }, "push_message":{ "notification":{ "title":"请填写通知标题", "body":"请填写通知内容", "click_type":"url", "url":"https//:xxx" } }}

uni-app 推送功能unipush的.net详细用法讲解

二推送具体示例C

创建控制台应用程序

uni-app 推送功能unipush的.net详细用法讲解

1.baseRequest.cs 代码段基础方法类

using Newtonsoft.Json;using System;using System.Collections.Generic;using System.IO;using System.Net;using System.Security.Cryptography;using System.Text;using System.Threading;using System.Threading.Tasks;
namespace uniPush_v2.uniPush{ public class baseRequest {
//timestamp 毫秒时间戳,请使用当前毫秒时间戳,误差太大可能出错 public static string getTimeStamp()//获取当前时间戳 { return Convert.ToInt64((DateTime.Now.ToUniversalTime().Ticks - 621355968000000000) / 10000).ToString(); }

//sign 签名,加密算法: SHA256 格式: sha256(appkey+timestamp+mastersecret) public static string sha256(string data) { byte[] bytes = Encoding.UTF8.GetBytes(data); byte[] hash = SHA256Managed.Create().ComputeHash(bytes);
StringBuilder builder = new StringBuilder(); for (int i = 0; i < hash.Length; i++) { builder.Append(hash[i].ToString("X2")); }
return builder.ToString(); }
string appkey = "";// 创建应用时生成的appkey

//执行方法 public static string getReturnData(string jsonData, string url, string token) { string responseFromServer = ""; //定义request并设置request的路径 HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.Method = "POST"; //请求方式 byte[] byteArray = Encoding.UTF8.GetBytes(jsonData); //设置request的MIME类型及内容长度 request.ContentType = "application/json;charset=utf-8;"; request.ContentLength = byteArray.Length; request.Headers["token"] = token; Stream dataStream = request.GetRequestStream(); dataStream.Write(byteArray, 0, byteArray.Length); dataStream.Close(); HttpWebResponse response = (HttpWebResponse)request.GetResponse(); dataStream = response.GetResponseStream(); StreamReader reader = new StreamReader(dataStream); responseFromServer = reader.ReadToEnd();//读取所有 return responseFromServer; }



public static string GetResponsestr(string url, string AccessData, string token1) { string responseFromServer = ""; //定义request并设置request的路径 HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.Method = "POST"; //请求方式 byte[] byteArray = Encoding.UTF8.GetBytes(AccessData); //设置request的MIME类型及内容长度 request.ContentType = "application/json;charset=utf-8;"; request.ContentLength = byteArray.Length; request.Headers["token"] = token1; Stream dataStream = request.GetRequestStream(); dataStream.Write(byteArray, 0, byteArray.Length); dataStream.Close(); HttpWebResponse response = (HttpWebResponse)request.GetResponse(); dataStream = response.GetResponseStream(); StreamReader reader = new StreamReader(dataStream); responseFromServer = reader.ReadToEnd();//读取所有 return responseFromServer; }


private static readonly char[] Constant = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' };

public static string generateRandomName(int length) { var newRandom = new System.Text.StringBuilder(62); var rd = new Random(); var leng = rd.Next(10, length);//10-32随机生成 for (var i = 0; i < leng; i++) { newRandom.Append(Constant[rd.Next(62)]); } return newRandom.ToString();        } }}

2.uniToken.cs 获取Token方法

using Newtonsoft.Json;using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;
namespace uniPush_v2.uniPush{ public class uniToken { //获取公用的Token public static string getToken() { string APPID = "XXX"; string APPKEY = "XXX"; string MASTERSECRET = "XXX";
string timestamp = baseRequest.getTimeStamp();//获取时间戳 string sign = baseRequest.sha256(APPKEY + timestamp + MASTERSECRET);//获取签名

//带传入的参数 string jsonData = "{ \"sign\":\"" + sign + "\",\"timestamp\":\"" + timestamp + "\",\"appkey\":\"" + APPKEY + "\"}"; //请求路径 string url = "https://restapi.getui.com/v2/" + APPID + "/auth"; //传入参数得到数据 string responseFromServer = baseRequest.getReturnData(jsonData, url, null); dynamic data = JsonConvert.DeserializeObject(responseFromServer);
string tok = ""; if (data != null) { tok = data.data.token; } return tok; }
}}

3.uniToSingle.cs 获取官网ToSingle单推具体执行方法

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;
namespace uniPush_v2.uniPush{ public class uniToSingle {

public static bool toSingle(string title, string content, string[] list) //List - 遍历集合单推 {

bool flag = false; string APPID = "XXXXX"; string responseFromServer = ""; int i = 0; try { string token = uniToken.getToken(); //获取不重复的请求ID string request_id = baseRequest.generateRandomName(30); string url = "https://restapi.getui.com/v2/" + APPID + "/push/single/cid";
foreach (var li in list) { StringBuilder jsonData = new StringBuilder(); jsonData.Append("{"); jsonData.Append(" \"request_id\":\"" + baseRequest.generateRandomName(30) + "\","); jsonData.Append(" \"audience\":{ \"cid\":[\"" + li.ToString() + "\"] }, "); jsonData.Append(" \"settings\":{ "); jsonData.Append(" \"ttl\":3600000, "); jsonData.Append(" \"strategy\": { "); jsonData.Append("\"default\":1,"); jsonData.Append(" \"ios\":1, ");//1或者4都可以 jsonData.Append(" \"op\":1 "); jsonData.Append(" }"); jsonData.Append(" },"); jsonData.Append(" \"push_message\":{ "); jsonData.Append(" \"notification\":{"); jsonData.Append(" \"title\":\"请填写标题\","); jsonData.Append(" \"body\":\"请填写内容\","); jsonData.Append(" \"click_type\":\"startapp\""); jsonData.Append(" , \"url\":\"https://www.json.cn/\""); jsonData.Append(" }"); jsonData.Append(" }, "); jsonData.Append(" \"push_channel\":{ "); jsonData.Append(" \"android\":{ "); jsonData.Append(" \"ups\":{ "); jsonData.Append(" \"notification\":{ "); jsonData.Append(" \"title\":\"henry请填写android标题\", "); jsonData.Append(" \"body\":\"请填写android内容\","); jsonData.Append(" \"click_type\":\"startapp\" "); //jsonData.Append(" , \"url\":\"https://xxx\" "); jsonData.Append(" }"); jsonData.Append(" }"); jsonData.Append(" },"); jsonData.Append(" \"ios\":{"); jsonData.Append(" \"type\":\"notify\","); //jsonData.Append(" \"payload\":\"自定义消息\","); jsonData.Append(" \"aps\":{"); jsonData.Append(" \"alert\":{"); jsonData.Append(" \"title\":\"dot365请填写ios标题\","); jsonData.Append(" \"body\":\"请填写ios内容\""); jsonData.Append(" },"); jsonData.Append(" \"content-available\":0 ,"); jsonData.Append(" \"sound\":\"com.gexin.ios.silence\" ");//声音 jsonData.Append(" },"); jsonData.Append("\"auto_badge\":\"+1\""); jsonData.Append(" } "); jsonData.Append(" } "); jsonData.Append(" } "); string strjson = jsonData.ToString().Trim(); responseFromServer = baseRequest.getReturnData(strjson, url, uniToken.getToken());
if (!string.IsNullOrEmpty(responseFromServer) && responseFromServer != "") { i += 1; } }
if (i == list.Count()) { flag = true; } } catch (Exception ex) { flag = false;
} return flag; } }}

4.传入CID验证Program.cs

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using uniPush_v2.uniPush;
namespace uniPush_v2{ class Program { static void Main(string[] args)        { //获取公钥Tok            //string tok = uniToken.getToken(); //个推发送            string[] cidArry = new string[] { "你要发送的手机CID"};            bool tok = uniToSingle.toSingle("每日科技名次|龙卷""每日辟谣!到底哪只眼“跳财”?想多了!眼皮跳可能是因为..", cidArry); Console.Read(); } }}

5.查看效果

更多技术请关注

uni-app 推送功能unipush的.net详细用法讲解》来自互联网,仅为收藏学习,如侵权请联系删除。本文URL:http://www.hashtobe.com/680.html