c# – 如何发送和接收Windows Phone 8.1的推送通知

我关注微软在
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中没有一些类.

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

最佳答案 这是我用来接收推送通知和处理ChannelUri的课程.只需调用UpdateChannelUri方法即可.如果需要,将更新channelUri,并且将触发ChannelUriUpdated事件,并将相同的内容保存到应用程序数据设置中.

如果您的应用程序正在运行并且您收到通知,则将执行包含通知内容的四种方法之一,由通知类型确定.

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;
    }
}
点赞