您使用的服务器端技术是什么?你必须从那里开始。基本上,您不能通过桌面浏览器进行跨域调用。您可以执行此操作的方法是,使用有效负载调用您的服务器端方法,然后让服务器端发送通知。这是我编写的 c# 示例代码。
public interface INotification
{
void Set(string deviceId, string alert, int? badge, string sound);
}
public class BaseNotification
{
public List<string> Aliases { get; set; }
public List<string> Tags { get; set; }
}
public class iOSNotification : BaseNotification, INotification
{
public List<string> Device_Tokens { get; set; }
public NotificationBody aps { get; set; }
public iOSNotification()
{
Device_Tokens = new List<string>();
}
public void Set(string deviceId, string alert, int? badge, string sound)
{
Device_Tokens.Add(deviceId);
aps = new NotificationBody
{
Alert = alert,
Badge = badge.HasValue ? badge.Value : 0,
Sound = sound
};
}
}
//in a static extensions
public static string ToJSONString<T>(this IEnumerable<T> items)
{
var jsonString = JsonConvert.SerializeObject(items, Formatting.Indented, new JsonSerializerSettings
{
ContractResolver = new LowerCaseContractResolver(),
NullValueHandling = NullValueHandling.Ignore
});
jsonString = jsonString.Replace("\r\n", string.Empty);
return jsonString;
}
protected internal void SendPushNotification(List<INotification> payLoad, string uriKey) {
var json = payLoad.ToJSONString();
var Uri = GetAppSettings(uriKey);
var encoding = new UTF8Encoding();
var contentLength = encoding.GetByteCount(json);
var request = (WebRequest)WebRequest.Create(Uri);
request.Method = "POST";
CredentialCache credentialCache = new CredentialCache();
credentialCache.Add(new Uri(Uri), "Basic", GetCredentials());
request.Credentials = credentialCache;
request.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(new ASCIIEncoding().GetBytes(_username + ":" + _password)));
request.ContentType = "application/json";
request.ContentLength = contentLength;
using (var stream = request.GetRequestStream()) {
stream.Write(encoding.GetBytes(json), 0, contentLength);
stream.Close();
var response = request.GetResponse();
response.Close();
}
}