在推送服务中推送尖锐不发送通知和无回调

Posted

技术标签:

【中文标题】在推送服务中推送尖锐不发送通知和无回调【英文标题】:Push sharp not sending notification and No Call backs in Push service 【发布时间】:2015-04-02 23:12:12 【问题描述】:

我正在使用 pushsharp 为我的设备创建远程通知。我做了所有证书的事情。我的问题是,当我尝试使用 push sharp 发送通知时,这里没有任何反应,这是我从 Here 采用的代码。 我至少希望调用一些回调,例如 NotificationFailed、ChanelCreated...等

这是我的代码

公共类 PushNotificationService 私有静态 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");
    

【问题讨论】:

【参考方案1】:

这解决了我的问题:

在创建生产 SSL 证书时,请勿更改名称“aps_production.cer”。

在创建与开发相关的证书之前,首先创建仅用于生产的证书(SSL、provisioning、p12)并尝试。

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

【讨论】:

【参考方案2】:

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

pushBroker.StopAllServices();

【讨论】:

以上是关于在推送服务中推送尖锐不发送通知和无回调的主要内容,如果未能解决你的问题,请参考以下文章

解析服务器不向大量受众发送推送通知

不使用服务器的 iOS 推送通知

使用 OneSignal 推送通知服务向特定测试设备发送推送通知

Xamarin - 如何在不使用其他服务的情况下发送推送通知

我可以使用 Apple 的推送通知服务发送图像吗? [关闭]

向 FCM 服务器发送推送通知不起作用