推送锐利不在Push服务中发送通知和无回拨

时间:2015-04-02 23:12:12

标签: push-notification apple-push-notifications pushsharp

我正在使用pushsharp为我的设备创建远程通知。我做了所有的证明。我的问题是,当我尝试使用推送发送通知时,这里没有发生任何事情,这是我从Here采用的代码。 我至少期待一些回调被调用,如NotificationFailed,ChanelCreated ......等等。

这是我的代码

公共类PushNotificationService     {         private static PushNotificationApple _pushNotificationApple;

    public static void Main()
    {
        PushNotificationService ser = new PushNotificationService();
       string token = "0eaaf46d379bc5f5529f0b3357e0973ccd4655b163d789d875e3ad5fe64210d9";
        ser.SendPushNotification(token, "Hello my push");
        System.Console.WriteLine("Test");
    }


    public PushNotificationService()
    {
        if (_pushNotificationApple == null)
        {
            _pushNotificationApple = new PushNotificationApple();
        }
    }

    /// <summary>
    /// Send the push notification to the device
    /// </summary>
    /// <param name="deviceToken">The device token</param>
    /// <param name="message">The message</param>
    /// <returns>True if the notification is sent</returns>
    public bool SendPushNotification(string deviceToken, string message)
    {
        if (_pushNotificationApple != null)
        {
            _pushNotificationApple.SendNotification(deviceToken, message);
        }
        return true;
    }
}

另一个班级

/// <summary>
/// Class for
/// </summary>
public class PushNotificationApple
{
    private static PushBroker pushBroker;

    /// <summary>
    /// The push notification apple
    /// </summary>
    public PushNotificationApple()
    {
        if (pushBroker == null)
        {
            //Create our push services broker
            pushBroker = new PushBroker();

            //Wire up the events for all the services that the broker registers
            pushBroker.OnNotificationSent += NotificationSent;
            pushBroker.OnChannelException += ChannelException;
            pushBroker.OnServiceException += ServiceException;
            pushBroker.OnNotificationFailed += NotificationFailed;
            pushBroker.OnNotificationRequeue += NotificationRequeue;
            pushBroker.OnDeviceSubscriptionExpired += DeviceSubscriptionExpired;
            pushBroker.OnDeviceSubscriptionChanged += DeviceSubscriptionChanged;
            pushBroker.OnChannelCreated += ChannelCreated;
            pushBroker.OnChannelDestroyed += ChannelDestroyed;

            //-------------------------
            // APPLE NOTIFICATIONS
            //-------------------------
            //Configure and start Apple APNS
            // IMPORTANT: Make sure you use the right Push certificate.  Apple allows you to generate one for connecting to Sandbox,
            //   and one for connecting to Production.  You must use the right one, to match the provisioning profile you build your
            //   app with!
            // Make sure you provide the correct path to the certificate, in my case this is how I did it in a WCF service under Azure,
            // but in your case this might be different. Putting the .p12 certificate in the main directory of your service 
            // (in case you have a webservice) is never a good idea, people can download it from there..
            //System.Web.Hosting.HostingEnvironment.MapPath("~/folder/file");


             var appleCert = File.ReadAllBytes(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "../../../Resources/PushNotification.p12"));

            //var appleCert = File.ReadAllBytes("/Resources/PushNotification.p12");

            // TODD revise
            // var appleCert = File.ReadAllBytes(System.Web.Hosting.HostingEnvironment.MapPath("~/PushSharp.PushCert.Development.p12"));

            //IMPORTANT: If you are using a Development provisioning Profile, you must use the Sandbox push notification server 
            //  (so you would leave the first arg in the ctor of ApplePushChannelSettings as 'false')
            //  If you are using an AdHoc or AppStore provisioning profile, you must use the Production push notification server
            //  (so you would change the first arg in the ctor of ApplePushChannelSettings to 'true')
            pushBroker.RegisterAppleService(new ApplePushChannelSettings(false, appleCert, "")); //Extension method
        }
    }

    private void NotificationRequeue(object sender, PushSharp.Core.NotificationRequeueEventArgs e)
    {
        Console.WriteLine("Chanel Notification Requeue");
    }

    public void SendNotification(string deviceToken, string message)
    {
        //Fluent construction of an iOS notification
        //IMPORTANT: For iOS you MUST MUST MUST use your own DeviceToken here that gets generated within your iOS app itself when the Application Delegate
        //  for registered for remote notifications is called, and the device token is passed back to you
        if (pushBroker != null)
        {
            pushBroker.QueueNotification(new AppleNotification()
                                       .ForDeviceToken(deviceToken)
                                       .WithAlert(message)
                                       .WithBadge(10)
                                       .WithSound("sound.caf"));
        }
    }

    private void ChannelDestroyed(object sender)
    {
        Console.WriteLine("Chanel Destroyed");
    }

    private void ChannelCreated(object sender, PushSharp.Core.IPushChannel pushChannel)
    {
        Console.WriteLine("Chanel created");
    }

    private void DeviceSubscriptionChanged(object sender, string oldSubscriptionId, string newSubscriptionId, PushSharp.Core.INotification notification)
    {
        Console.WriteLine("Device Subscription Changed");
    }

    private void DeviceSubscriptionExpired(object sender, string expiredSubscriptionId, DateTime expirationDateUtc, PushSharp.Core.INotification notification)
    {
        Console.WriteLine("Device Subscription Expired");
    }

    private void NotificationFailed(object sender, PushSharp.Core.INotification notification, Exception error)
    {
        Console.WriteLine("Notification Failed");
    }

    private void ServiceException(object sender, Exception error)
    {
        Console.WriteLine("Service Exception");
    }
    private void ChannelException(object sender, PushSharp.Core.IPushChannel pushChannel, Exception error)
    {
        Console.WriteLine("Channel Exception");
    }

    private void NotificationSent(object sender, PushSharp.Core.INotification notification)
    {
        Console.WriteLine("Notification Sent");
    }
}

2 个答案:

答案 0 :(得分:0)

这解决了我的问题:

创建生产SSL证书时,请勿更改名称&#34; aps_production.cer&#34;。

在创建与开发相关的证书之前,首先只为生产创建证书(SSL,配置,p12)并尝试。

尝试不同的方法后,它真的对我有用。试试看。

答案 1 :(得分:0)

在方法SendNotification()的末尾添加此行解决了我的问题。

pushBroker.StopAllServices();