Xamarin表示WinPhone 8.1 Silverlight WNS推送通知

时间:2016-02-23 13:39:28

标签: push-notification xamarin.forms azure-notificationhub wns win-phone-silverlight-8.1

我尝试将我的应用配置为使用WNS而不是MPNS(我使用的是Xamarin Forms,并且后端有一个带有Azure Notification中心的Win Phone 8.1 Silverlight项目),为此我更新了我的代码使用移动服务注册手机以获取推送通知,并将WMAppManifest.xml中的Notification Service更改为WNS。在我通过azure检查手机注册后实施这些更改后,它说它的MPNS。下面是我的配置的一些屏幕截图以及如何注册应用程序的代码片段。

WMAppManifest.xml

WNS

Push Notifications enabled

Package.appxmanifest

Toast capable

NotificationManager代码

public class PushNotificationManager : IPushNotificationManager
{
    private PushNotificationChannel channel;

    public PushNotificationManager() { }

    public static MobileServiceClient MobileService = new MobileServiceClient(Utilities.Constants.ApplicationURL, Utilities.Constants.ApplicationKey);

    public async Task RegisterDevice()
    {
        try
        {
            channel = await PushNotificationChannelManager.CreatePushNotificationChannelForApplicationAsync();

            channel.PushNotificationReceived += Channel_PushNotificationReceived;

            await this.RegisterWinDevice(channel.Uri);

            NotificationTask.UnregisterBackgroundTask();
            NotificationTask.RegisterBackgroundTask();
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

    protected void Channel_PushNotificationReceived(PushNotificationChannel sender, PushNotificationReceivedEventArgs args)
    {
        try
        {
            //Create notification
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

    public async Task UnregisterDevice()
    {
        if(channel != null)
        {
            channel.Close();
        }

        await MobileService.GetPush().UnregisterNativeAsync();
    }

    private async Task RegisterWinDevice(string channelUri)
    {
        try
        {
            var tags = new List<string>() { };
            User user = LocalStorage.GetUserInfo();
            tags.Add(user.Id.ToString());

            await MobileService.GetPush().RegisterNativeAsync(channelUri, tags.ToArray());
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

    private void CreateNotification(string title, string message)
    {
        //Show Toast
    }
}

在azure中,我设置了Windows程序包SID和客户端密钥。我也启用了未经身份验证的推送通知(虽然从我的理解这是针对MPNS)。

最后,这是一个如何使用以下代码注册的屏幕截图:

Phone registration

如果有人知道如何让它正确注册到WNS我非常感谢帮助。谢谢!

1 个答案:

答案 0 :(得分:0)

只是更新我如何解决我的问题以防任何人遇到这个问题。我不得不将我的winphone项目切换到非Silverlight应用程序(我猜这个版本不支持它)。一旦我这样做,一切都开始正常工作。