using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using com.igetui.api.openservice;
using com.igetui.api.openservice.igetui;
using com.igetui.api.openservice.igetui.template;
using com.igetui.api.openservice.payload;
using Newtonsoft.Json;
namespace Quanjiang.DigitalScholl.PushNotifications.GeTui
{
public class PushMessage
{
//参数设置
//http的域名
private static readonly string Host = ConfigManager.AppSettings["HOST"].Value;
//定义常量, appId、Appkey、Mastersecret
private static readonly string Appid = ConfigManager.AppSettings["APPID"].Value;
private static readonly string Appkey = ConfigManager.AppSettings["APPKEY"].Value;
private static readonly string Mastersecret = ConfigManager.AppSettings["MASTERSECRET"].Value;
///
/// 对单个用户推送消息
///
/// 个推业务层中的对外用户标识,用于标识客户端身份,由第三方客户端获取并保存到第三方服务端,是个推SDK的唯一识别号,简称CID。
/// 通知标题
/// 通知内容
/// 新闻ID
public static void PushMessageToSingle(string clientId, string notifyTitle, string notifyContent, string newsId)
{
var push = new IGtPush(Host, Appkey, Mastersecret);
//消息模版:TransmissionTemplate:透传模板
var template = GeTuiTransmissionTemplate(notifyTitle, notifyContent, newsId);
// 单推消息模型
var message = new SingleMessage();
message.IsOffline = true; // 用户当前不在线时,是否离线存储,可选
message.OfflineExpireTime = 1000 * 3600 * 12; // 离线有效时间,单位为毫秒,可选
message.Data = template;
Target target = new Target();
target.appId = Appid;
target.clientId = clientId;
try
{
push.pushMessageToSingle(message, target);
}
catch (RequestException e)
{
String requestId = e.RequestId;
//发送失败后的重发
push.pushMessageToSingle(message, target, requestId);
}
}
///
/// 对指定列表用户推送消息
///
/// 用户列表
/// 通知标题
/// 通知内容
/// 新闻ID
public static void PushMessageToList(List clientIdList, string notifyTitle, string notifyContent, string newsId)
{
// 推送主类(方式1,不可与方式2共存)
var push = new IGtPush(Host, Appkey, Mastersecret);
// 推送主类(方式2,不可与方式1共存)此方式可通过获取服务端地址列表判断最快域名后进行消息推送,每10分钟检查一次最快域名
//IGtPush push = new IGtPush("",Appkey,Mastersecret);
var message = new ListMessage();
var template = GeTuiTransmissionTemplate(notifyTitle, notifyContent, newsId);
// 用户当前不在线时,是否离线存储,可选
message.IsOffline = true;
// 离线有效时间,单位为毫秒,可选
message.OfflineExpireTime = 1000 * 3600 * 12;
message.Data = template;
message.PushNetWorkType = 0; //判断是否客户端是否wifi环境下推送,1为在WIFI环境下,0为不限制网络环境。
//设置接收者
var targetList = new List();
foreach (var cid in clientIdList)
{
var msgTarget = new Target
{
appId = Appid,
clientId = cid
};
targetList.Add(msgTarget);
}
var contentId = push.getContentId(message);
push.pushMessageToList(contentId, targetList);
}
/*
*
* 所有推送接口均支持四个消息模板,依次为透传模板,通知透传模板,通知链接模板,通知弹框下载模板
* 注:IOS离线推送需通过APN进行转发,需填写pushInfo字段,目前仅不支持通知弹框下载功能
*
*/
///
/// 透传模板
///
///
public static TransmissionTemplate GeTuiTransmissionTemplate(string notifyTitle, string notifyContent, string newsId)
{
var template = new TransmissionTemplate();
template.AppId = Appid;
template.AppKey = Appkey;
//应用启动类型,1:强制应用启动 2:等待应用启动
template.TransmissionType = "2";
template.TransmissionContent = JsonConvert.SerializeObject(new { title = notifyTitle, content = notifyContent, payload =new{ newsId,flag=true } });
//APN高级推送 IOS
APNPayload apnpayload = new APNPayload();
DictionaryAlertMsg alertMsg = new DictionaryAlertMsg();
alertMsg.Body = notifyContent;
alertMsg.ActionLocKey = "ActionLocKey";
alertMsg.LocKey = "LocKey";
alertMsg.addLocArg("LocArg");
alertMsg.LaunchImage = "LaunchImage";
//iOS8.2支持字段
alertMsg.Title = notifyTitle;
alertMsg.TitleLocKey = "TitleLocKey";
alertMsg.addTitleLocArg("TitleLocArg");
apnpayload.AlertMsg = alertMsg;
//应用icon上显示的数字
apnpayload.Badge = 1;
//包含此键值1以配置后台更新通知。当此键出现时,系统会在后台唤醒您的应用程序,并将通知发送给其应用程序代理。
apnpayload.ContentAvailable = 1;
//apnpayload.Category = "";
//当您希望系统播放声音时,请加入此键。此键的值是应用程序主包中或Library / Sounds应用程序数据容器文件夹中的声音文件的名称。如果找不到声音文件,或者如果您指定default了该值,系统将播放默认警报声音。
apnpayload.Sound = "test1.wav";
apnpayload.addCustomMsg("payload", new { newsId, flag = true });
template.setAPNInfo(apnpayload);
return template;
}
}
}