Question

I am trying to use PushSharp to send notification to various devices. My Server-side app registers notifications to send to a table in MSSQL, so that another app (Bot) will process those notifications and send them to Apple servers.

I am using the following code:

static DataEntities Entities;

    static void Main(string[] args)
    {

        Entities = new DataEntities();

        var appleCert = File.ReadAllBytes(Path.GetFullPath("My_Push_Notifications.p12"));
        PushBroker broker = new PushBroker();
        broker.RegisterAppleService(new PushSharp.Apple.ApplePushChannelSettings(false, appleCert, "XXXXX"));

        broker.OnChannelCreated += broker_OnChannelCreated;
        broker.OnChannelDestroyed += broker_OnChannelDestroyed;
        broker.OnChannelException += broker_OnChannelException;
        broker.OnNotificationRequeue += broker_OnNotificationRequeue;
        broker.OnServiceException += broker_OnServiceException;
        broker.OnNotificationSent += broker_OnNotificationSent;
        broker.OnNotificationFailed += broker_OnNotificationFailed;

        while (true)
        {
            var pendingNotifications = Entities.Notifications.Include("UserDevice").Where(n => n.Status == (byte)Constants.Notifications.Status.Pending);

            if (pendingNotifications.ToList().Count > 0)
            {
                Console.WriteLine("Pending notifications: {0}", pendingNotifications.Count());
            }
            else
                Console.WriteLine("No pending notifications");

            foreach (var notification in pendingNotifications)
            {
                broker.QueueNotification<AppleNotification>(new AppleNotification()
                    .ForDeviceToken(notification.UserDevice.DeviceID)
                    .WithAlert(notification.Text)
                    .WithTag(notification.NotificationID));

                notification.Status = (byte)Constants.Notifications.Status.Sending;
            }

            Entities.SaveChanges();

            Thread.Sleep(2000);
        }
    }

As you can see I queue notifications to the PushBroker but no event ever gets called, and the iOS device is receiving nothing. I also tried to use "StopAllServices" before the end of the loop but nothing changes.

How can that be possible?

Thank you.

Was it helpful?

Solution

I solved this. PushSharp was not raising events because you have to add Event Handlers BEFORE you register apple service on the broker. So the correct code is:

    PushBroker broker = new PushBroker();
    broker.OnChannelCreated += broker_OnChannelCreated;
    broker.OnChannelDestroyed += broker_OnChannelDestroyed;
    broker.OnChannelException += broker_OnChannelException;
    broker.OnNotificationRequeue += broker_OnNotificationRequeue;
    broker.OnServiceException += broker_OnServiceException;
    broker.OnNotificationSent += broker_OnNotificationSent;
    broker.OnNotificationFailed += broker_OnNotificationFailed;
    // Now you can register the service.
    broker.RegisterAppleService(new PushSharp.Apple.ApplePushChannelSettings(false, appleCert, "XXXXX"));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top