APNS将通知从应用程序推送到Iphone

时间:2019-07-12 06:27:28

标签: c#

在APNS C#推送中发送通知时收到一条错误消息。错误是连接电压低并且不发送通知。

Apple通知失败:ID = 1,代码= ConnectionError

从asp.net C#向iphone发送推送通知。我正在使用Push Sharpe库。首先创建一个Web应用程序并添加一个Web表单。打开包管理器控制台并编写代码Install-Package PushSharp -Version 4.0.10。 Web表单代码如下所示

设计阶段

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="iosp12.aspx.cs" Inherits="p12ios.iosp12" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
     <table class="auto-style1">
            <tr>
                <td class="auto-style2">Device token</td>
                <td>
                    <asp:TextBox ID="txtDeviceToken" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td class="auto-style3">message</td>
                <td class="auto-style4">
                    <asp:TextBox ID="txtMessage" runat="server" Height="44px"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td class="auto-style2">&nbsp;</td>
                <td>
                    <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" Width="142px" />
                </td>
            </tr>
            <tr>
                <td class="auto-style2">&nbsp;</td>
                <td>
                    <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
                </td>
            </tr>
            <tr>
                <td class="auto-style2">&nbsp;</td>
                <td>&nbsp;</td>
            </tr>
        </table>
    </div>
    </form>
</body>
</html>

代码阶段

  using System;
    using System.Web;
    using Newtonsoft.Json.Linq;
    using PushSharp.Apple;
    using System.Collections.Generic;

    namespace p12ios
    {
        public partial class iosp12 : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {

            }

            protected void Button1_Click(object sender, EventArgs e)
            {
                SendPushNotification(txtDeviceToken.Text, txtMessage.Text);
            }

            private void SendPushNotification(string deviceToken, string message)
            {
                try
                {

                    //Get Certificate
                    var appleCert =   System.IO.File.ReadAllBytes(Server.MapPath("~/IOS/P12 certificate"));

                    // Configuration (NOTE: .pfx can also be used here)
                    var config = new ApnsConfiguration(ApnsConfiguration.ApnsServerEnvironment.Sandbox, appleCert, "Password");

                    // 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;
                                string desc = $"Apple Notification Failed: ID={apnsNotification.Identifier}, Code={statusCode}";
                                Console.WriteLine(desc);
                                Label1.Text = desc;
                            }
                            else
                            {
                                string desc = $"Apple Notification Failed for some unknown reason : {ex.InnerException}";
                                // Inner exception might hold more useful information like an ApnsConnectionException           
                                Console.WriteLine(desc);
                                Label1.Text = desc;
                            }

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

                    apnsBroker.OnNotificationSucceeded += (notification) =>
                    {
                        Label1.Text = "Apple Notification Sent successfully!";
                    };




                    var fbs = new FeedbackService(config);
                    fbs.FeedbackReceived += (string devicToken, DateTime timestamp) =>
                    {
                        // Remove the deviceToken from your database
                        // timestamp is the time the token was reported as expired
                    };

                    // Start Proccess 
                    apnsBroker.Start();


                    if (deviceToken != "")
                    {
                        apnsBroker.QueueNotification(new ApnsNotification
                        {
                            DeviceToken = deviceToken,
                            Payload = JObject.Parse ("{ \"aps\" : { \"alert\" : \(message\" } }"))
                        });
                    }

                    apnsBroker.Stop();

                }
                catch (Exception)
                {

                    throw;
                }
            }
        }

    }

0 个答案:

没有答案