【问题标题】:How to send and receive push notifications for Windows Phone 8.1如何发送和接收 Windows Phone 8.1 的推送通知
【发布时间】:2015-08-19 13:29:24
【问题描述】:

我关注了微软关于在 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 实现这些。

【问题讨论】:

    标签: c# windows-8 windows-runtime windows-phone-8.1 windows-8.1


    【解决方案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;
        }
    }
    

    【讨论】:

    • @Kristian Sir 我们如何获得 LocalSettings 类
    • 先生,我尝试为 LocalSetting 添加,所以它看起来像 Windows.Storage.ApplicationData.Current.LocalSettings.Values["key"] = value;请帮忙
    • @Krishtian 我们使用相同的类但没有获得频道 URI
    • 您是否在应用程序清单中指定您的应用程序支持 toast?
    • 是的,我设置吐司能力是是的
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多