【问题标题】:Send Push Notification in C#在 C# 中发送推送通知
【发布时间】:2017-07-11 03:55:36
【问题描述】:

我正在尝试在 VS C# Web 项目中使用以下编码向 iOS 设备发送推送通知。

实际上下面的编码没有任何错误,但我最终没有在我的设备上收到任何通知,有人知道吗?谢谢。

static void Main(string[] args)
    {
        var config = new ApnsConfiguration(ApnsConfiguration.ApnsServerEnvironment.Sandbox, @"D:\Share\Certificates_Prod.p12", "");

        // 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();

        apnsBroker.QueueNotification(new ApnsNotification
        {
            DeviceToken = "58f0f386003a4b7be..................................",
            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();
    }

【问题讨论】:

  • 你有什么问题?是否没有响应阅读以确认您的消息已发送?还有int[] HexValue = new int[] {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0A, 0x0B, 0....是什么0x00为什么这么多?
  • 现在我没有收到任何通知,您对 HexStringToByteArray 函数有什么建议吗?
  • 是的,我在我的回答中发布了它
  • 谢谢~~我试过了还是不行。
  • 您创建了一个开发者帐户并注册了您的应用程序正确吗?还有你使用旧格式的原因吗?

标签: c# push-notification certificate apple-push-notifications pushsharp


【解决方案1】:

您使用的是旧版 API。如果您希望继续使用它,那么 here 已经完成了 5 年的 C# 测试。

Apple 现在支持通过 http/2 的 APN。与其编写自己的代码,不如查看一些现有的库,例如 PushSharp,它们将为您处理低级 API 和错误处理。

// 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 ($"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\":{\"alert\":\"" + "Hi,, This Is a Sample Push Notification For IPhone.." + "\",\"badge\":1,\"sound\":\"default\"}}")
    });
}

// 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 ();

【讨论】:

  • 最后我按照您的建议更新了我对 PushSharp 的编码参考,但仍然没有发生任何事情,请您看看好吗?谢谢。
【解决方案2】:

You Convert HexStringToBytes 函数错误。它在错误的地方有几个 0x00 值:

int[] HexValue = new int[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F };

应该是:

int[] HexValue = new int[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F };

我还会推荐一些更清洁的东西。试试这样的:

public static byte[] HexStringToByteArray(string Hex)
{  
    if(1 == (Hex.length&1))  throw new Exception("Hex string cannot have an odd number of characters");
    return Enumerable.Range(0, hex.Length <<1 ) 
        .Select(x => Convert.ToByte(hex.Substring(x << 1, 2), 16)) 
        .ToArray();
}

【讨论】:

  • 谢谢~,你的建议我试过了,还是不行,还是没有收到通知
【解决方案3】:

问题已修复,就像 APNS 不允许空消息一样,我将有效负载 JSON 更新为:

JObject.Parse("{\"aps\": {\"alert\": \"joetheman\",\"sound\": \"default\"},\"message\": \"Some custom message for your app\",\"id\": 1234}")

它对我有用。

【讨论】:

    猜你喜欢
    • 2018-02-17
    • 2020-06-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-18
    • 2018-11-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多