1、引入cn.jpush.api
引入示例2、新建JPushTools类,命名自己定
3、引用下头文件
using cn.jpush.api;
using cn.jpush.api.common;
using cn.jpush.api.push.mode;
using cn.jpush.api.common.resp;
using cn.jpush.api.push.notification;
4、在极光推送后台(https://www.jiguang.cn/)申请及配置自己的应用信息,配置完成后,得到app_key和master_secret
5、在JPushTools里面定义
private static readonly string app_key = "";
private static readonly string master_secret = "";
6、(1)推送给所有人方法 # ps:下边用到的JPushModel是自定义推送的内容封装 #
public static ReJSON SendJPushByAll(JPushModel model)
{
JPushClient client = new JPushClient(app_key, master_secrets);
var pushPayload = new PushPayload()
{
//设置设备
platform = Platform.all(),
//群体
audience = Audience.all(),
//通知内容
notification = new Notification
{
//安卓通知
AndroidNotification = new AndroidNotification().setAlert(model.Content).setTitle(model.Title).AddExtra("Modular_Id", model.Modular_Id).AddExtra("News_Id", model.News_Id).AddExtra("LinkUrl", model.LinkUrl),
//iOS通知
//建议用incrBadge(+1),要iOS端和极光服务器同步设置个数
IosNotification = new IosNotification().incrBadge(+1).setAlert(model.Content).setSound("default").AddExtra("Modular_Id", model.Modular_Id).AddExtra("News_Id", model.News_Id).AddExtra("LinkUrl", model.LinkUrl)
},
options = new Options
{
time_to_live = 864000
}
};
try
{
cn.jpush.api.push.MessageResult response = client.SendPush(pushPayload);
bool bol = response.isResultOK();
if (bol == true)
{
reJson.code = 1;
reJson.message = response.msg_id.ToString();
return reJson;
}
else
{
reJson.code = 0;
reJson.message = "推送失败";
return reJson;
}
}
catch (APIRequestException ex)
{
//api请求异常
reJson.code = 0;
reJson.message = ex.ErrorMessage;
return reJson;
}
catch (APIConnectionException ex)
{
//api连接异常
reJson.code = 0;
reJson.message = ex.message;
return reJson;
}
}
6、(2)推送给指定registrationId的用户 # ps:registrationId需要前端提供,因此需要自行存储 #
public static ReJSON SendJPushByRegistrationId(JPushModel model)
{
JPushClient client = new JPushClient(app_key, master_secret);
var pushPayload = new PushPayload()
{
//设置设备
platform = Platform.all(),
//群体
audience = Audience.s_registrationId(model.Registration_Id),
//通知内容
notification = new Notification
{
//安卓通知
AndroidNotification = new AndroidNotification().setAlert(model.Content).setTitle(model.Title).AddExtra("Modular_Id", model.Modular_Id).AddExtra("News_Id", model.News_Id).AddExtra("LinkUrl", model.LinkUrl),
//iOS通知
// //建议用incrBadge(+1),要iOS端和极光服务器同步设置个数
IosNotification = new IosNotification().incrBadge(+1).setAlert(model.Content).setSound("default").AddExtra("Modular_Id", model.Modular_Id).AddExtra("News_Id", model.News_Id).AddExtra("LinkUrl", model.LinkUrl)
},
options = new Options
{
time_to_live = 864000
}
};
try
{
cn.jpush.api.push.MessageResult response = client.SendPush(pushPayload);
bool bol = response.isResultOK();
if (bol == true)
{
reJson.code = 1;
reJson.message = response.msg_id.ToString();
return reJson;
}
else
{
reJson.code = 0;
reJson.message = "推送失败";
return reJson;
}
}
catch (APIRequestException ex)
{
//api请求异常
reJson.code = 0;
reJson.message = ex.ErrorMessage;
return reJson;
}
catch (APIConnectionException ex)
{
//api连接异常
reJson.code = 0;
reJson.message = ex.message;
return reJson;
}
}
至此服务端操作基本完成,在需要下发通知的位置调用和设置下发内容即可!
# 星光不问赶路人,时光不负有别人 #
网友评论