準備條件:在NuGet 下載極光的SDK 搜索:
cn.jpush.api
直接上代碼,創建一個方法類,寫入下面方法:
/// <summary>
/// 推送信息
/// </summary>
/// <param name="DeviceType">-1-全部 3-Android 4-iOS</param>
/// <param name="IsPushAllRegID">是否發送全部設備ID 1-是 0-否</param>
/// <param name="RegIDAry">設備註冊ID數組集合 All爲全部</param>
/// <param name="Title">標題</param>
/// <param name="Message">內容</param>
/// <returns></returns>
public static int PushMessage(int DeviceType, int IsPushAllRegID, string[] RegIDAry, string Title, string Message)
{
int ret = 0;
string appKey = ConfigurationManager.AppSettings["AppKey"].ToString();
string master_Secret = ConfigurationManager.AppSettings["Master_Secret"].ToString();
JPushClient client = new JPushClient(appKey, master_Secret);
PushPayload payload = Get_AndroidPushPayload(DeviceType, IsPushAllRegID, RegIDAry, Title, Message);
try
{
var result = client.SendPush(payload);
var apiResult = client.getReceivedApi(result.msg_id.ToString());
var apiResultv3 = client.getReceivedApi_v3(result.msg_id.ToString());
var queryResultWithV2 = client.getReceivedApi("1739302794");
var querResultWithV3 = client.getReceivedApi_v3("1739302794");
}
catch (APIRequestException e)
{
throw e;
}
catch (APIConnectionException e)
{
return 1;
}
ret = 1;
return ret;
}
/// <summary>
/// 返回指定類型的推送對象
/// </summary>
/// <param name="DeviceType">-1-全部 3-Android 4-iOS</param>
/// <param name="RegID">設備註冊ID數組集合 All爲全部</param>
/// <param name="Title">標題</param>
/// <param name="Message">內容</param>
/// <returns></returns>
private static PushPayload Get_AndroidPushPayload(int DeviceType, int IsPushAllRegID, string[] RegIDAry, string Title, string Message)
{
PushPayload pushPayload = new PushPayload();
//根據設備類型,選擇對應的推送目標
switch (DeviceType)
{
case -1:
pushPayload.platform = Platform.all();
break;
case 3:
pushPayload.platform = Platform.android();
break;
case 4:
pushPayload.platform = Platform.ios();
break;
default:
pushPayload.platform = Platform.all();
break;
}
//判斷是否發送全部用戶
if (IsPushAllRegID == 1)
{
pushPayload.audience = Audience.all();
}
else
{
pushPayload.audience = Audience.s_registrationId(RegIDAry);
}
Notification nt = new Notification();
//設置安卓推送對象
if (DeviceType == -1 && DeviceType == 3)
{
AndroidNotification anf = new AndroidNotification().setTitle(Title)
.setAlert(Message)
.setBuilderID(3)
.setPriority(2)
.setStyle(2);
nt.AndroidNotification = anf;
}
//設置蘋果推送對象
if (DeviceType == -1 && DeviceType == 4)
{
IosNotification inf = new IosNotification().setAlert(Message);
nt.IosNotification = inf;
}
pushPayload.notification = nt;
return pushPayload;
}