【问题标题】:How to use the library PushSharp to send push notifications to iOS?如何使用库 PushSharp 向 iOS 发送推送通知?
【发布时间】:2016-05-26 14:57:02
【问题描述】:

我正在使用最新的PushSharp 版本通过 APN 发送推送通知。我正在使用他们的 Git wiki 页面中给出的以下代码来发送通知:

// Configuration (NOTE: .pfx can also be used here)
var config = new ApnsConfiguration (ApnsConfiguration.ApnsServerEnvironment.Sandbox, 
    "push-cert.p12", "push-cert-pwd");

// Create a new broker
var apnsBroker = new ApnsServiceBroker (config);

// Wire up events
apnsBroker.OnNotificationFailed += (notification, aggregateEx) => {

    aggregateEx.Handle (ex => {

        // See what kind of exception it was to further diagnose
        if (ex is ApnsNotificationException) {
            var notificationException = (ApnsNotificationException)ex;

            // Deal with the failed notification
            var apnsNotification = notificationException.Notification;
            var statusCode = notificationException.ErrorStatusCode;

            Console.WriteLine ($"Apple Notification Failed: ID={apnsNotification.Identifier}, Code={statusCode}");

        } else {
            // Inner exception might hold more useful information like an ApnsConnectionException           
            Console.WriteLine ($"Apple Notification Failed for some unknown reason : {ex.InnerException}");
        }

        // Mark it as handled
        return true;
    });
};

apnsBroker.OnNotificationSucceeded += (notification) => {
    Console.WriteLine ("Apple Notification Sent!");
};

// Start the broker
apnsBroker.Start ();

foreach (var deviceToken in MY_DEVICE_TOKENS) {
    // Queue a notification to send
    apnsBroker.QueueNotification (new ApnsNotification {
        DeviceToken = deviceToken,
        Payload = JObject.Parse ("{\"aps\":{\"badge\":7}}")
    });
}

// Stop the broker, wait for it to finish   
// This isn't done after every message, but after you're
// done with the broker
apnsBroker.Stop ();

困惑 -

  1. 我不知道 apnsBroker.QueueNotification 方法是根本发送推送,还是只是排队。

  2. 我不知道是否需要以某种方式在我的 Windows 机器上安装苹果证书。

最新版本的 PushSharp 在线没有合适的示例代码。

【问题讨论】:

  • Apple infra 有时真的很烦人.. Google infra 在苹果面前就像微风一样

标签: apple-push-notifications pushsharp


【解决方案1】:
  1. 只需在控制台应用程序中触发上述代码,Pushsharp 就会发送通知。

  2. Apple 允许一次推送通知使用一个推送令牌。

【讨论】:

    【解决方案2】:

    代码按原样工作。但正如你所说,有一些不确定的地方。

    第一个通知将在您排队时立即发送,它只是一种异步机制,不在那里等待代码。因此,如果出现任何问题(或正确),您可以通过经纪人的事件进行处理。

    第二部分有点复杂。首先,您已经为 macOS 机器上的推送通知创建了一个证书。比你必须将它上传到你的开发者帐户等。你可以通过谷歌找到视频。在这里描述它很长。比您必须将您的“Apple Push Services”证书从 macOS 机器导出到 p12 文件。并获取该 .p12 文件并将其放入您的 .net 服务文件夹,例如“App_Data”文件夹并加载它(我假设您正在编写 Web 服务):

     var config = new ApnsConfiguration(ApnsConfiguration.ApnsServerEnvironment.Sandbox,
                    Path.Combine(HostingEnvironment.ApplicationPhysicalPath, "App_Data", "yourfileName.p12"),"yourFilePassword");
    

    希望对你有帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多