/// <summary>
/// 发送通知(指定RegistrationId)
/// </summary>
/// <param name="registrationId"></param>
/// <param name="title"></param>
/// <param name="alert"></param>
/// <param name="customizedValues"></param>
public static bool SendNoticeByRegistrationId(string registrationId, string title, string alert, Dictionary<string, string> customizedValues)
{
try
{
JPushClientV3 client = new JPushClientV3(appKey, masterSecret);
Audience audience = new Audience();
// In JPush V3, tag can be multiple added with different values.
// In following code, it is to send push to those who are in ((Tag1 AND Tag2) AND (Tag3 OR Tag4))
// If you want to send to all, please use: audience.Add(PushTypeV3.Broadcast, null);
// audience.Add(PushTypeV3.ByTagWithinAnd, new List<string>(new string[] { "Tag1", "Tag2" }));
// audience.Add(PushTypeV3.ByTagWithinOr, new List<string>(new string[] { "Tag3", "Tag4" }));
audience.Add(PushTypeV3.ByRegistrationId, new List<string> { registrationId });
Notification notification = new Notification
{
AndroidNotification = new AndroidNotificationParameters
{
BuilderId = 1,
Title = title,
Alert = alert,
CustomizedValues = customizedValues
}
};
var response = client.SendPushMessage(new PushMessageRequestV3
{
Audience = audience,
Platform = PushPlatform.Android,
IsTestEnvironment = false,
Notification = notification
});
var message = response.ResponseMessage;
List<string> idToCheck = new List<string>();
idToCheck.Add(response.MessageId);
var statusList = client.QueryPushMessageStatus(idToCheck);
if (statusList != null)
{
if (statusList[0].AndroidDeliveredCount > 0)
{
return true;
}
}
}
catch (Exception ex)
{
return false;
}
return false;
/// 发送通知(指定RegistrationId)
/// </summary>
/// <param name="registrationId"></param>
/// <param name="title"></param>
/// <param name="alert"></param>
/// <param name="customizedValues"></param>
public static bool SendNoticeByRegistrationId(string registrationId, string title, string alert, Dictionary<string, string> customizedValues)
{
try
{
JPushClientV3 client = new JPushClientV3(appKey, masterSecret);
Audience audience = new Audience();
// In JPush V3, tag can be multiple added with different values.
// In following code, it is to send push to those who are in ((Tag1 AND Tag2) AND (Tag3 OR Tag4))
// If you want to send to all, please use: audience.Add(PushTypeV3.Broadcast, null);
// audience.Add(PushTypeV3.ByTagWithinAnd, new List<string>(new string[] { "Tag1", "Tag2" }));
// audience.Add(PushTypeV3.ByTagWithinOr, new List<string>(new string[] { "Tag3", "Tag4" }));
audience.Add(PushTypeV3.ByRegistrationId, new List<string> { registrationId });
Notification notification = new Notification
{
AndroidNotification = new AndroidNotificationParameters
{
BuilderId = 1,
Title = title,
Alert = alert,
CustomizedValues = customizedValues
}
};
var response = client.SendPushMessage(new PushMessageRequestV3
{
Audience = audience,
Platform = PushPlatform.Android,
IsTestEnvironment = false,
Notification = notification
});
var message = response.ResponseMessage;
List<string> idToCheck = new List<string>();
idToCheck.Add(response.MessageId);
var statusList = client.QueryPushMessageStatus(idToCheck);
if (statusList != null)
{
if (statusList[0].AndroidDeliveredCount > 0)
{
return true;
}
}
}
catch (Exception ex)
{
return false;
}
return false;
}
上述是极光推送官网给的Demo,在调试的时候,走到var statusList = client.QueryPushMessageStatus(idToCheck);的时候总会报异常,异常的内容是:Failed to QueryPushMessageStatus ,百度了半天也没有查到这个异常消息是什么意思。后来经过监控,发现有一部分是可以通过的,有一部分会报异常,再查了一下,是因为response.MessageId在大于int.MaxValue的时候,会出现异常。于是在 上边加了一句
if (Convert.ToInt32(response.MessageId) > int.MaxValue)
{
return true;
}
问题就解决了。