我有Windows Phone 8.1 silverlight应用程序,我想在其中使用新框架WNS接收通知。

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

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



这导致了一种获取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");
    }

另外,我基于microsofts update info删除了ID_CAP_PushNotification我没有收到 channel ,但收到错误消息:



解决方案
搜索错误和found this link,可以通过访问package.appxmanifest并启用Internet(客户端和服务器),如下面答案中所述解决此问题。

错误2
然后,UploadChannel()函数应该起作用。但是,注册API调用await App.MobileService.GetPush().RegisterNativeAsync(channel.Uri);会在服务器上导致错误:



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

Ekstra
在服务器上,我可以订阅URI,并接收通知。但不是在客户端上。这应该是这样吗?

最佳答案

在客户端:
理想情况下,要使用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" />
注意: PhonePublisherId中的0是有意的。我不知道为什么,但是当我没有提供这些应用程序时,该应用程序将无法运行。
您正在正确执行 channel uri请求:
PushNotificationChannel channel = await PushNotificationChannelManager.CreatePushNotificationChannelForApplicationAsync();
string channelUri = channel.Uri;
您还应该在Package.appxmanifest中设置 Internet(客户端和服务器)功能,以进行检查。
要在客户端上接收通知,您应按以下说明拦截接收到的通知:
https://msdn.microsoft.com/en-us/library/windows/apps/xaml/jj709907.aspx
在服务器端:
由于您使用MPNS方法来处理Azure服务器中的URI,因此发生错误“不支持的 channel URI”。
有关使用WNS的正确方法,请参见此处:http://azure.microsoft.com/en-us/documentation/articles/mobile-services-dotnet-backend-windows-universal-dotnet-get-started-push/

关于c# - 在Windows Phone Silverlight 8.1上接收WNS推送通知,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/30278608/

10-11 22:55
查看更多