当前位置: 首页 > 知识库问答 >
问题:

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

颜宸
2023-03-14

我阅读了Microsofts关于在Windows Phone8.0上发送和接收推送通知的文章:

共有1个答案

谭卜鹰
2023-03-14

下面是我的类,用于接收推送通知和处理ChannelURI。只需调用updateChannelURI方法。如果需要,将更新channelUri,ChannelUriUpdated事件将被激发,并将其保存到应用程序数据设置中。

如果您的app正在运行,并且您收到通知,那么将执行四个带有通知内容的方法中的一个,由通知类型决定。

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;
    }
}
 类似资料:
  • http://Socket.io允许你触发或响应自定义的事件,除了connect,message,disconnect这些事件的名字不能使用之外,你可以触发任何自定义的事件名称。 服务器端 // 注意,io(<端口号>) 将为你创建一个http服务。 var io = require('socket.io')(80); io.on('connection', function (socket)

  • 我认为通过使用,我可以捕获请求正文,但事实是它什么也没有捕获。如果我理解正确,我应该收到一个结构在这里详细说明的变更资源。是我做错了什么,还是我理解错了?我很感激任何可以给予的洞察力和提前感谢!

  • 问题内容: 我想使用XMPP,以便我的应用程序将更新发送到Android手机(1.5及更高版本)。我非常想使用XMPP将推送通知发送到电话。 我将如何实现这一目标。目前,我的Web应用程序正在使用许多servlet在apache tomact上运行,因此android手机可以访问信息,但是我发现很难理解如何实现XMPP,以便可以将信息从服务器推送到客户端( android手机)。 我已经通过Goo

  • 如何通过Azure从我的UWP-App向不同设备上的应用程序的其他实例发送推送通知? 以下是注册设备以接收推送的说明。(这是可行的)第二部分是关于如何在控制台应用程序上发送推送(这也是可行的)https://azure.microsoft.com/en-us/documentation/articles/notification-hubs-windows-store-dotnet-get-star

  • 我有一个React原生应用程序(在iOS上测试),正在尝试合并推送通知。我正在使用以下模块:https://www.npmjs.com/package/react-native-firebase-push-notifications. 我尝试运行示例应用程序代码,并能够获得(1)消息令牌和(2)从我的设备成功获得权限。 我正在尝试从Firebase发送测试通知,并且正在使用我设备的令牌。但是,触发

  • 根据新的android策略,我们已经在我的cordova应用程序中设置目标SDK为26(API级别26)。在此之后,android oreo设备不会接收到任何使用GCM从AWS SNS发送的推送通知。