/// <summary>
        /// Create a <see cref="WnsTemplateRegistration"/>
        /// </summary>
        /// <param name="channelUri">The channel uri</param>
        /// <param name="bodyTemplate">The template xml in string format</param>
        /// <param name="templateName">The template name</param>
        /// <param name="tags">The tags that restrict which notifications this registration will receive</param>
        /// <param name="additionalHeaders">Additional headers</param>
        public WnsTemplateRegistration(string channelUri, string bodyTemplate, string templateName, IEnumerable <string> tags, IEnumerable <KeyValuePair <string, string> > additionalHeaders)
            : base(channelUri, tags)
        {
            if (string.IsNullOrWhiteSpace(templateName))
            {
                throw new ArgumentNullException("templateName");
            }

            if (templateName.Equals(Registration.NativeRegistrationName))
            {
                throw new ArgumentException(string.Format(CultureInfo.InvariantCulture, "Template name conflicts with reserved name '{0}'.", Registration.NativeRegistrationName));
            }

            if (templateName.Contains(":") || templateName.Contains(";"))
            {
                throw new ArgumentException("Template name cannot contain ';' or ':'.");
            }

            if (string.IsNullOrWhiteSpace(bodyTemplate))
            {
                throw new ArgumentNullException("bodyTemplate");
            }

            this.TemplateName = templateName;
            this.WnsHeaders   = new Dictionary <string, string>(StringComparer.OrdinalIgnoreCase);
            if (additionalHeaders != null)
            {
                foreach (var item in additionalHeaders)
                {
                    this.WnsHeaders.Add(item.Key, item.Value);
                }
            }

            this.BodyTemplate = bodyTemplate;

            if (!this.WnsHeaders.ContainsKey(WnsTypeName))
            {
                // Because there are no headers, it is not raw
                // This means it must be XML
                XmlDocument xmlDocument = new XmlDocument();
                try
                {
                    xmlDocument.LoadXml(bodyTemplate);
                }
                catch (Exception e)
                {
                    throw new ArgumentException("Valid XML is required for any template without a raw header.", "bodyTemplate", e);
                }

                var payloadType = WnsTemplateRegistration.DetectBodyType(xmlDocument);
                this.WnsHeaders.Add(WnsTypeName, payloadType);
            }

            this.WnsHeaders = new ReadOnlyDictionary <string, string>(this.WnsHeaders);
        }
Exemple #2
0
        /// <summary>
        /// Register a particular channelUri with a template
        /// </summary>
        /// <param name="channelUri">The channelUri to register</param>
        /// <param name="xmlTemplate">The string defining the template</param>
        /// <param name="templateName">The template name</param>
        /// <param name="tags">The tags to register to receive notifications from</param>
        /// <returns>Task that completes when registration is complete</returns>
        public Task RegisterTemplateAsync(string channelUri, string xmlTemplate, string templateName, IEnumerable <string> tags)
        {
            if (string.IsNullOrWhiteSpace(channelUri))
            {
                throw new ArgumentNullException("channelUri");
            }

            if (string.IsNullOrWhiteSpace(xmlTemplate))
            {
                throw new ArgumentNullException("xmlTemplate");
            }

            if (string.IsNullOrWhiteSpace(templateName))
            {
                throw new ArgumentNullException("templateName");
            }

            var registration = new WnsTemplateRegistration(channelUri, xmlTemplate, templateName, tags);

            return(this.RegistrationManager.RegisterAsync(registration));
        }
        private static ZumoTest CreateRegisterTemplateChannelTest(string nhNotificationType)
        {
            return new ZumoTest("Register template " + nhNotificationType + " push channel", async delegate(ZumoTest test)
            {
                var client = ZumoTestGlobals.Instance.Client;
                var push = client.GetPush();
                WnsTemplateRegistration reg = null;
                switch (nhNotificationType.ToLower())
                {
                    case "toast":
                        var toastTemplate = BuildXmlToastPayload("sendToastText01", "$(News_English)");
                        reg = new WnsTemplateRegistration(ZumoPushTests.pushChannel.Uri, toastTemplate.ToString(), ZumoPushTestGlobals.NHToastTemplateName, "World English".Split());
                        break;
                    case "raw":
                        var rawTemplate = "<raw>$(News_French)</raw>";
                        IDictionary<string, string> wnsHeaders = new Dictionary<string, string>();
                        wnsHeaders.Add("X-WNS-Type", "wns/raw");
                        reg = new WnsTemplateRegistration(ZumoPushTests.pushChannel.Uri, rawTemplate, ZumoPushTestGlobals.NHRawTemplateName, "World Mandarin".Split(), wnsHeaders);
                        break;
                    case "badge":
                        var badgeTemplate = BuildBadgeXmlPayload("$(News_Badge)");
                        reg = new WnsTemplateRegistration(ZumoPushTests.pushChannel.Uri, badgeTemplate.ToString(), ZumoPushTestGlobals.NHBadgeTemplateName, "World Badge".Split());
                        break;
                    case "tile":
                        var tileTemplate = BuildXmlTilePayload("TileWideImageAndText02", new[] { "tl-wiat2-1", "$(News_Mandarin)" }, new[] { wideImageUrl }, new[] { "zumowide" });
                        reg = new WnsTemplateRegistration(ZumoPushTests.pushChannel.Uri, tileTemplate.ToString(), ZumoPushTestGlobals.NHTileTemplateName, "World Mandarin".Split());
                        break;
                    default:
                        throw new Exception("Template type" + nhNotificationType + "is not supported.");
                }

                await push.RegisterAsync(reg);
                pushChannelUri = null;
                test.AddLog("Registered " + nhNotificationType + " template with NH succeeded.");
                pushChannel.PushNotificationReceived += pushChannel_PushNotificationReceived;
                return true;
            }, ZumoTestGlobals.RuntimeFeatureNames.NH_PUSH_ENABLED);
        }