Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Receive WNS push notfication on Windows phone silverlight 8.1

I have windows phone 8.1 silverlight application where I want to receive Notfications using the new framework, WNS.

I have in the package.appxmanifest: <identity name="4657xxxxxxx" publisher="CN=xxxxx" version="1.0.0.0"/> and added it to the Mobile Service Hub.

For this I have removed old references to MPNS usings, and added the following for WNS:

using Windows.UI.Notifications;

using Windows.Networking.PushNotifications;

using Windows.UI.StartScreen;

This resulted in a new way of getting the channelURI:

 public static PushNotificationChannel CurrentChannel { get; private set; }

    public async static Task<bool> UploadChannel()
    {
        bool newChannel = false;
        var channel = await Windows.Networking.PushNotifications.PushNotificationChannelManager.CreatePushNotificationChannelForApplicationAsync();
        var settings = Windows.Storage.ApplicationData.Current.LocalSettings.Values;
        object oldChannel;
        settings.TryGetValue("channelURI", out oldChannel);
        if ((oldChannel as PushNotificationChannel).Uri != CurrentChannel.Uri)
        {
            settings.Add("channelURI", CurrentChannel);
            newChannel = true;
        }
        try
        {
            await App.MobileService.GetPush().RegisterNativeAsync(channel.Uri);
        }
        catch (Exception exception)
        {
            CurrentChannel.Close();
            HandleRegisterException(exception);
        }

        CurrentChannel.PushNotificationReceived += CurrentChannel_PushNotificationReceived;
        return newChannel;
    }
    private static void HandleRegisterException(Exception exception)
    {
        MessageBox.Show("error - retry pushchannel");
    }

Additionally I removed the ID_CAP_PushNotification based on microsofts update info I do not get a channel I get an error:

The application does not have the cloud notification capability. (Exception from HRESULT: 0x803E0110)

solution Searched for the error and found this link, This can be solved as stated in the answer below by accessing package.appxmanifest and enable Internet (Client & Server).

ERROR 2 Then it the UploadChannel()function should work. However the Register API call await App.MobileService.GetPush().RegisterNativeAsync(channel.Uri); results in an error on the server:

Message='Could not register with the 'mpns' platform. Error received: 'Unsupported channel uri: 'https://db3.notify.windows.com . . . .

The error makes sense but I have no idea on how to solve it.

Ekstra On the server I can subscribe with the URI, and receive notifications. But not on the client. Is this how it should be or?

like image 984
JTIM Avatar asked May 16 '15 17:05

JTIM


1 Answers

On Client side:

Ideally, to use WNS, you should remove all references to MPNS from WMAppManifest.xml and add the info provided by Windows Store to your package.appxmanifest.

I understand that you are migrating from WP8 to WP8.1. So in your package.appxmanifest, edit the code so that it looks like this:

<Identity Name="4657xxxxxxx" Publisher="CN=xxxxx" Version="1.0.0.0" />
<mp:PhoneIdentity PhoneProductId="xxxx" PhonePublisherId="00000000-0000-0000-0000-000000000000" />

Note: The 0s in the PhonePublisherId are intentional. I have no idea why, but the app wouldn't work when I did not provide them as such.

You are doing the channel uri request right:

PushNotificationChannel channel = await PushNotificationChannelManager.CreatePushNotificationChannelForApplicationAsync();
string channelUri = channel.Uri;

You should also set the Internet (Client & Server) capability in Package.appxmanifest to be checked.

To receive notifications on the client, you should intercept the received notification as described here: https://msdn.microsoft.com/en-us/library/windows/apps/xaml/jj709907.aspx

On Server Side:

The error "Unsupported Channel URI" occurs because you are using the MPNS methods to process the URI in your Azure server.

Refer here for the proper way to do it using WNS: http://azure.microsoft.com/en-us/documentation/articles/mobile-services-dotnet-backend-windows-universal-dotnet-get-started-push/

like image 55
Rajshri Mohan K S Avatar answered Oct 18 '22 18:10

Rajshri Mohan K S