Reputation: 18349
I have no clue why this isn't working and it's not throwing any errors either. None of the subscribed events are ever triggered (including OnNotificationSent
and OnNotificationSendFailure
). The code is almost identical to PushSharp's sample code in GIT, the only difference is that it's running inside a thread:
public class MyNotificationService
{
ILog _log = LogManager.GetLogger("");
PushService _PushService;
public void Start()
{
PushService _PushService;
byte[] appleCert = File.ReadAllBytes("myCert.p12");
_PushService = new PushService();
_PushService.Events.OnChannelCreated += Events_OnChannelCreated;
_PushService.Events.OnChannelDestroyed += Events_OnChannelDestroyed;
_PushService.Events.OnChannelException += Events_OnChannelException;
_PushService.Events.OnDeviceSubscriptionExpired += Events_OnDeviceSubscriptionExpired;
_PushService.Events.OnDeviceSubscriptionIdChanged += Events_OnDeviceSubscriptionIdChanged;
_PushService.Events.OnNotificationSendFailure += Events_OnNotificationSendFailure;
_PushService.Events.OnNotificationSent += Events_OnNotificationSent;
_PushService.StartApplePushService(new ApplePushChannelSettings(false, appleCert, "myPass"));
_MainThread = new Thread(() =>
{
try
{
var nt = NotificationFactory.Apple()
.ForDeviceToken("60A378B0FF0628FB52461C6F9F2CEDAA29A05D52F97EF2E811")
.WithAlert("Test")
.WithSound("default")
.WithCustomItem("data", "some other data")
.WithBadge(7);
_PushService.QueueNotification(nt);
}
catch (Exception e)
{
_log.Error("In main thread", e);
}
}
});
_MainThread.Start();
}
static void Events_OnDeviceSubscriptionIdChanged(PlatformType platform, string oldDeviceInfo, string newDeviceInfo, Notification nt)
{
//Currently this event will only ever happen for Android GCM
_log.Debug("Device Registration Changed: Old-> " + oldDeviceInfo + " New-> " + newDeviceInfo);
}
static void Events_OnNotificationSent(Notification nt)
{
_log.Debug("Sent: " + nt.Platform.ToString() + " -> " + nt.ToString());
}
static void Events_OnNotificationSendFailure(Notification nt, Exception notificationFailureException)
{
_log.Error("Failure: " + nt.Platform.ToString() + " -> " + notificationFailureException.Message + " -> " + nt.ToString());
}
static void Events_OnChannelException(Exception exception, PlatformType platformType, Notification nt)
{
_log.Error("Channel Exception: " + platformType.ToString() + " -> " + exception.ToString());
}
static void Events_OnDeviceSubscriptionExpired(PlatformType platform, string deviceInfo, Notification nt)
{
_log.Debug("Device Subscription Expired: " + platform.ToString() + " -> " + deviceInfo);
}
static void Events_OnChannelDestroyed(PlatformType platformType, int newChannelCount)
{
_log.Debug("Channel Destroyed for: " + platformType.ToString() + " Channel Count: " + newChannelCount);
}
static void Events_OnChannelCreated(PlatformType platformType, int newChannelCount)
{
_log.Debug("Channel Created for: " + platformType.ToString() + " Channel Count: " + newChannelCount);
}
}
Again, none of the events are getting triggered (nothing logged, breakpoints not hit). Curiously, calling StopAllServices
hangs forever, apparently without doing anything. I have checked my provisioning profile and cert several times and can't find anything wrong with them. Any ideas?
EDIT:
Turns out the problem can only be reproduced when I run it from a Windows Service .Net project. From a console application, everything works fine. I thought it was a permissions issue that was blocking network access, but I couldn't make it work,.
Upvotes: 2
Views: 5006
Reputation: 1473
I had a similar problem. I've used PushSharp in my ASP.Net MVC application . Make sure that the version of Newtonsoft.Json in your application and in your version of PushSharp is the same.
Upvotes: 1
Reputation: 6375
I had the exact same problem! I was using push sharp in a dll that was referenced by my exe. I fixed the problem by adding the following to my app.config:
<configuration>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
Make sure the Newtonsoft.Json dll is in your bin folder!
Upvotes: 4
Reputation: 343
First
I have same problem. No events are triggered but notification are delivered. Still looking for answer. Maybe this link would help.
Second (about "StopAllServices hangs forever")
Maybe this will help. In short - no empty device token should be provided
Upvotes: 0
Reputation: 3
Does your service run as "LocalSystem" ? If no, try to install it as "LocalSystem".
Perhaps the service have no network access, so no connection to Internet or Database.
Btw, the device token seems really short but if it works as a console application, it should work as a service.
Upvotes: 0