在Windows phone Silverlight 8.1上接收WNS推送通知

JTI*_*TIM 6 c# silverlight azure azure-mobile-services windows-phone-8.1

我有windows phone 8.1 silverlight应用程序,我希望使用新框架WNS接收Notfications.

我在package.appxmanifest中:<identity name="4657xxxxxxx" publisher="CN=xxxxx" version="1.0.0.0"/>并将其添加到Mobile Service Hub.

为此,我删除了对MPNS使用的旧引用,并为WNS添加了以下内容:

使用Windows.UI.Notifications;

使用Windows.Networking.PushNotifications;

使用Windows.UI.StartScreen;

这导致了获得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");
    }
Run Code Online (Sandbox Code Playgroud)

另外我删除了ID_CAP_PushNotification基于微软的更新信息 我没有得到一个频道我得到一个错误:

该应用程序没有云通知功能.(HRESULT异常:0x803E0110)

解决方案 搜索错误并找到此链接,这可以通过访问package.appxmanifest并启用Internet(客户端和服务器)来解决,如下面的答案中所述.

错误2 然后它的UploadChannel()功能应该工作.但是,Register API调用会await App.MobileService.GetPush().RegisterNativeAsync(channel.Uri);导致服务器出错:

消息='无法注册'mpns'平台.收到错误:'不支持的频道uri:' https : //db3.notify.windows.com....

错误是有道理的,但我不知道如何解决它.

Ekstra 在服务器上我可以使用URI订阅,并接收通知.但不是客户端.这应该是怎么样的?

Raj*_*K S 2

在客户端:

理想情况下,要使用 WNS,您应该从 WMAppManifest.xml 中删除对 MPNS 的所有引用,并将 Windows 应用商店提供的信息添加到 package.appxmanifest 中。

我了解到您正在从 WP8 迁移到 WP8.1。因此,在您的 package.appxmanifest 中,编辑代码,使其如下所示:

<Identity Name="4657xxxxxxx" Publisher="CN=xxxxx" Version="1.0.0.0" />
<mp:PhoneIdentity PhoneProductId="xxxx" PhonePublisherId="00000000-0000-0000-0000-000000000000" />
Run Code Online (Sandbox Code Playgroud)

注意: PhonePublisherId 中的 0 是故意的。我不知道为什么,但当我不提供它们时,应用程序将无法工作。

您正在正确执行频道 uri 请求:

PushNotificationChannel channel = await PushNotificationChannelManager.CreatePushNotificationChannelForApplicationAsync();
string channelUri = channel.Uri;
Run Code Online (Sandbox Code Playgroud)

您还应该在 Package.appxmanifest 中设置要检查的Internet(客户端和服务器)功能。

要在客户端上接收通知,您应该拦截收到的通知,如下所述: https ://msdn.microsoft.com/en-us/library/windows/apps/xaml/jj709907.aspx

在服务器端:

出现“不支持的通道 URI”错误是因为您在 Azure 服务器中使用 MPNS 方法处理 URI。

请参阅此处,了解使用 WNS 执行此操作的正确方法:http://azure.microsoft.com/en-us/documentation/articles/mobile-services-dotnet-backend-windows-universal-dotnet-get-started-push/