如何发送和接收 Windows Phone 8.1 的推送通知 [英] How to send and receive push notifications for Windows Phone 8.1

查看:22
本文介绍了如何发送和接收 Windows Phone 8.1 的推送通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我关注了微软关于在 Windows Phone 8.0 上发送和接收推送通知的文章:

I followed Microsofts article about sending and receiving push notifications on Windows Phone 8.0:

https://msdn.microsoft.com/en-us/library/windows/apps/hh202967(v=vs.105).aspx

它工作正常,但现在我们正在创建一个新的 Windows Phone 8.1 应用程序并尝试重写相同的 8.0 代码,但某些类在 WP 8.1 中不可用.

It works fine, but now we are creating a new Windows Phone 8.1 app and try to rewrite the same 8.0 code, but some classes are not available in WP 8.1.

请帮助我如何为 Windows Phone 8.1 实现这些.

Please help me how we can implement these for Windows Phone 8.1.

推荐答案

这是我用来接收推送通知和处理 ChannelUri 的类.只需调用 UpdateChannelUri 方法.如果需要,channelUri 将被更新,并且 ChannelUriUpdated 事件将被触发,同样的事件将被保存到应用程序数据设置中.

Here is my class which I use for receiving push notifications and handling the ChannelUri. Just call the UpdateChannelUri method. The channelUri will be updated if need and the ChannelUriUpdated event will be fired and the same will be saved to application data settings.

如果您的应用正在运行并且您收到通知,则将执行包含通知内容的四种方法之一,具体取决于通知类型.

If your app is running and you receive a notification, one of the four methods with notification content will be executed, determined by the notification type.

public sealed class PushService
{
    private const string ChannelUriKey = "ChannelUri";
    private const string ChannelUriDefault = null;

    private PushNotificationChannel _channel;

    private string _channelUri;

    /// <summary>
    /// Initializes a new instance of the <see cref="Services.PushService"/> class.
    /// </summary>
    public PushService()
    {
        this._channelUri = LocalSettingsLoad(ApplicationData.Current.LocalSettings, ChannelUriKey, ChannelUriDefault);
    }

    /// <summary>
    /// Gets the push notification channel URI. If no channel URI was yet created
    /// then the value will be <c>null</c>.
    /// </summary>
    public string ChannelUri
    {
        get { return _channelUri; }
        private set
        {
            if (_channelUri != value)
            {
                this._channelUri = value;
                LocalSettingsStore(ApplicationData.Current.LocalSettings, ChannelUriKey, value);
            }
        }
    }

    /// <summary>
    /// Requests a new push channel URI.
    /// </summary>
    public async Task<string> UpdateChannelUri()
    {
        var retries = 3;
        var difference = 10; // In seconds

        var currentRetry = 0;

        do
        {
            try
            {
                _channel = await PushNotificationChannelManager.CreatePushNotificationChannelForApplicationAsync();
                _channel.PushNotificationReceived += OnPushNotificationReceived;
                if (!_channel.Uri.Equals(ChannelUri))
                {
                    ChannelUri = _channel.Uri;
                    // TODO send channel uri to your server to your server
                    this.RaiseChannelUriUpdated();
                    return _channel.Uri;
                }
            }
            catch
            {
                // Could not create a channel
            }

            await Task.Delay(TimeSpan.FromSeconds(difference));

        } while (currentRetry++ < retries);

        return null;
    }

    private void OnPushNotificationReceived(PushNotificationChannel sender, PushNotificationReceivedEventArgs args)
    {
        switch (args.NotificationType)
        {
            case PushNotificationType.Badge:
                this.OnBadgeNotificationReceived(args.BadgeNotification.Content.GetXml());
                break;

            case PushNotificationType.Tile:
                this.OnTileNotificationReceived(args.TileNotification.Content.GetXml());
                break;

            case PushNotificationType.Toast:
                this.OnToastNotificationReceived(args.ToastNotification.Content.GetXml());
                break;

            case PushNotificationType.Raw:
                this.OnRawNotificationReceived(args.RawNotification.Content);
                break;
        }

        args.Cancel = true;
    }

    private void OnBadgeNotificationReceived(string notificationContent)
    {
        // Code when a badge notification is received when app is running
    }

    private void OnTileNotificationReceived(string notificationContent)
    {
        // Code when a tile notification is received when app is running
    }

    private void OnToastNotificationReceived(string notificationContent)
    {
        // Code when a toast notification is received when app is running

        // Show a toast notification programatically

        var xmlDocument = new XmlDocument();
        xmlDocument.LoadXml(notificationContent);
        var toastNotification = new ToastNotification(xmlDocument);

        //toastNotification.SuppressPopup = true;
        ToastNotificationManager.CreateToastNotifier().Show(toastNotification);
    }

    private void OnRawNotificationReceived(string notificationContent)
    {
        // Code when a raw notification is received when app is running
    }

    public event EventHandler<EventArgs> ChannelUriUpdated;
    private void RaiseChannelUriUpdated()
    {
        if (ChannelUriUpdated != null)
        {
            ChannelUriUpdated(this, new EventArgs());
        }
    }

    public static T LocalSettingsLoad<T>(ApplicationDataContainer settings, string key, T defaultValue)
    {
        T value;

        if (settings.Values.ContainsKey(key))
        {
            value = (T)settings.Values[key];
        }
        else
        {
            // Otherwise use the default value.
            value = defaultValue;
        }

        return value;
    }

    public static bool LocalSettingsStore(ApplicationDataContainer settings, string key, object value)
    {
        bool valueChanged = false;

        if (settings.Values.ContainsKey(key))
        {
            // If the key exists
            if (settings.Values[key] != value)
            {
                // If the value has changed, store the new value
                settings.Values[key] = value;
                valueChanged = true;
            }
        }
        else
        {
            // Otherwise create the key
            settings.Values.Add(key, value);
            valueChanged = true;
        }

        return valueChanged;
    }
}

这篇关于如何发送和接收 Windows Phone 8.1 的推送通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆