如何发送 iOS - 带有 P12 证书的 APN 通知 - C# 示例
Posted
技术标签:
【中文标题】如何发送 iOS - 带有 P12 证书的 APN 通知 - C# 示例【英文标题】:How to send iOS - APN Notification With P12 Certificate - C# Samples 【发布时间】:2021-05-24 02:23:40 【问题描述】:我是 ios APN - 通知生成服务的新手,请帮助我们如何从 C# Windows 服务 - .Net Framework 4.5 / 4.7 发送 APN?
厌倦了 Moon-APN 、 Pushsharp 、 DotAPN 但没有结果。如果有人有逐步过程的示例代码,请分享。
在此先感谢 :-)
【问题讨论】:
你找到解决办法了吗?? 【参考方案1】:@Sumesh 您可以使用.NET
开源库PushSharp
将消息推送到Apple 的APNS 服务。
step1:下载PushSharp
开源项目编译https://github.com/Redth/PushSharp!
step2:编译成功后APNS推送需要用到Newtonsoft.Json.dll
、PushSharp.Apple.dll
、PushSharp.Core.dll
三个汇编库文件
step3:然后ios客户端需要提供.p12
证书文件和证书文件的加密密码
step4:准备好这些后,新建一个console程序引用上面的库文件,将证书复制到根目录,修改属性,输出到复制目录,和往常一样复制
控制台程序代码如下:
class Program
static ApnsConfiguration config;
static ApnsServiceBroker apnsBroker;
static void Main(string[] args)
config = new ApnsConfiguration(ApnsConfiguration.ApnsServerEnvironment.Sandbox, "certificate.p12", "certificate's password");
apnsBroker = new ApnsServiceBroker(config);
//post catch error
apnsBroker.OnNotificationFailed += (notification, aggregateEx) =>
aggregateEx.Handle(ex =>
if (ex is ApnsNotificationException)
var notificationException = (ApnsNotificationException)ex;
//handle failed APN msg
var apnsNotification = notificationException.Notification;
var statusCode = notificationException.ErrorStatusCode;
Console.WriteLine("Apple Notification Failed: ID=apnsNotification.Identifier, Code=statusCode" + notification.DeviceToken);
else
//internal catch error
Console.WriteLine("Apple Notification Failed for some unknown reason : ex.InnerException" + notification.DeviceToken);
// flag handle
return true;
);
;
//successed
apnsBroker.OnNotificationSucceeded += (notification) =>
Console.WriteLine("Apple Notification Sent ! "+notification.DeviceToken);
;
//engined start
apnsBroker.Start();
/// <summary>
/// apn message
/// </summary>
public static void SendMsg()
List<string> MY_DEVICE_TOKENS = new List<string>()
"1f6f37acad29348c6a5957529c9fa61ad69766ec9c7367948745899cbccdfd51",
"1f6f37acad29348c6a5957529c9fa61ad69766ec9c7367948745899cbccdfd51"
;
foreach (var deviceToken in MY_DEVICE_TOKENS)
// queue triger a notification message to iOS client
apnsBroker.QueueNotification(new ApnsNotification
DeviceToken = deviceToken, // this one deviceToken from iOS client,it have to
Payload = JObject.Parse("\"aps\":\"sound\":\"default\",\"badge\":\"1\",\"alert\":\"This is a test of a mass advertising message push message\"")
);
//engined stop
apnsBroker.Stop();
Console.Read();
希望能帮到你
【讨论】:
以上是关于如何发送 iOS - 带有 P12 证书的 APN 通知 - C# 示例的主要内容,如果未能解决你的问题,请参考以下文章
ios企业证书导出的P12和对应的描述文件,可以签名ipa吗?