Namrata
Namrata

Reputation: 11

Windows push notification service using Pushsharp giving Notification Failure

var push = new PushBroker();
push.OnNotificationSent += NotificationSent;
push.OnChannelException += ChannelException;
push.OnServiceException += ServiceException;
push.OnNotificationFailed += NotificationFailed;
push.OnDeviceSubscriptionExpired += DeviceSubscriptionExpired;
push.OnDeviceSubscriptionChanged += DeviceSubscriptionChanged;
push.OnChannelCreated += ChannelCreated;
push.OnChannelDestroyed += ChannelDestroyed;
push.RegisterWindowsPhoneService();
push.QueueNotification(new WindowsPhoneToastNotification()
        .ForEndpointUri(new Uri(uri))
        .ForOSVersion(WindowsPhoneDeviceOSVersion.Eight)
        .WithBatchingInterval(BatchingInterval.Immediate)
        .WithNavigatePath("/LandingView.xaml")
        .WithText1("PushSharp")
        .WithText2("This is a Toast"));
 push.StopAllServices();

I am using pushsharp nuget package for push notifications and while passing uri to this c# backend code for windows, I am getting notification failure exception.

Upvotes: 1

Views: 1187

Answers (1)

user1573896
user1573896

Reputation: 35

I am using the latest version of PushSharp (version 3.0) in a project of mine to send toast notifications to Windows Phone Devices and it is working fine for me. I notice by the code you have above that you are using an older version of the PushSharp package, there is a new 3.0 version available from nuget.

You could use that latest package to send toast notification to windows phone devices. The latest version of PushSharp uses the WNS as opposed to the old MPNS.

If you go to that nuget get link i supplied above and download the solution you can see some examples on how to implement the push notifcations for windows phone using WNS. Look under the PushSharp.Test project (look for the WNSRealTest.cs file).

Below is an example of how you can send a toast notification to windows phone device:

var config = new WnsConfiguration(
                 "Your-WnsPackageNameProperty",
                 "Your-WnsPackageSid",
                 "Your-WnsClientSecret"
                  );

var broker = new WnsServiceBroker(config);
broker.OnNotificationFailed += (notification, exception) =>
{
   //you could do something here
};
broker.OnNotificationSucceeded += (notification) =>
{
   //you could do something here
};

 broker.Start();

 broker.QueueNotification(new WnsToastNotification
 {
                ChannelUri = "Your device Channel URI",
                Payload = XElement.Parse(string.Format(@"
                    <toast>
                        <visual>
                            <binding template=""ToastText02"">
                                <text id=""1"">{0}</text>
                                <text id=""2"">{1}</text>
                            </binding>  
                        </visual>
                    </toast>
                ","Your Header","Your Toast Message"))
  });

  broker.Stop();

As you may notice above the WnsConfiguration constructor requires a Package Name, Package SID, and a Client Secrete. To get these values your app must be registered with the Store Dashboard. This will provide you with credentials for your app that your cloud service will use in authenticating with WNS. You can check steps 1-3 on the following MSDN page for details on how to get this done. (note in the link above it states that you have to edit your appManifest.xml file with the identity of your app, I did not do this step, just make sure you have your windows phone app setup correctly to receive toast notification, this blog post will help with that.

Hope this helps.

Upvotes: 1

Related Questions