/// <summary> /// Create a TemplateRegistration /// </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 TemplateRegistration(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, Resources.Push_ConflictWithReservedName, Registration.NativeRegistrationName)); } if (templateName.Contains(":") || templateName.Contains(";")) { throw new ArgumentException(Resources.Push_InvalidTemplateName); } 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(Resources.Push_BodyTemplateMustBeXml, "bodyTemplate", e); } var payloadType = TemplateRegistration.DetectBodyType(xmlDocument); this.WnsHeaders.Add(WnsTypeName, payloadType); } this.WnsHeaders = new ReadOnlyDictionary <string, string>(this.WnsHeaders); }
/// <summary> /// Register a particular channelUri with a template /// </summary> /// <param name="channelUri">The channelUri to register</param> /// <param name="xmlTemplate">The XmlDocument defining the template</param> /// <param name="templateName">The template name</param> /// <param name="tags">The tags to register to receive notifcations 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 TemplateRegistration(channelUri, xmlTemplate, templateName, tags, null); return(this.RegistrationManager.RegisterAsync(registration)); }
private static ZumoTest CreateRegisterChannelTest(bool registerTemplate = false, string templateType = null) { return new ZumoTest("Register push channel", async delegate(ZumoTest test) { string channelName = "MyPushChannel"; var pushChannel = HttpNotificationChannel.Find(channelName); if (pushChannel == null) { pushChannel = new HttpNotificationChannel(channelName); test.AddLog("Created new channel"); } else { test.AddLog("Reusing existing channel"); } ZumoWP8PushTests.pushChannel = pushChannel; if (pushChannel.ConnectionStatus == ChannelConnectionStatus.Disconnected || pushChannel.ChannelUri == null) { pushChannel.Open(); test.AddLog("Opened the push channel"); } else { test.AddLog("Channel already opened"); } if (pushChannel.IsShellToastBound) { test.AddLog("Channel is already bound to shell toast"); } else { var uris = new System.Collections.ObjectModel.Collection<Uri>(); uris.Add(new Uri(ImageUrlDomain)); pushChannel.BindToShellTile(uris); pushChannel.BindToShellToast(); test.AddLog("Bound the push channel to shell toast / tile"); } TimeSpan maxWait = TimeSpan.FromSeconds(30); await WaitForChannelUriAssignment(test, pushChannel, maxWait); if (ZumoTestGlobals.Instance.IsNHPushEnabled) { var zumoPush = ZumoTestGlobals.Instance.Client.GetPush(); TemplateRegistration reg = null; if (registerTemplate) { switch (templateType) { case "toast": reg = new TemplateRegistration(pushChannel.ChannelUri.ToString(), ZumoPushTestGlobals.NHWp8ToastTemplate, "wp8" + ZumoPushTestGlobals.NHToastTemplateName, "World English".Split()); break; case "tile": reg = new TemplateRegistration(pushChannel.ChannelUri.ToString(), ZumoPushTestGlobals.NHWp8TileTemplate, "wp8" + ZumoPushTestGlobals.NHTileTemplateName, "World Mandarin".Split()); break; case "raw": IDictionary<string, string> wp8Headers = new Dictionary<string, string>(); wp8Headers.Add("X-NotificationClass", "3"); reg = new TemplateRegistration(pushChannel.ChannelUri.ToString(), ZumoPushTestGlobals.NHWp8RawTemplate, "wp8" + ZumoPushTestGlobals.NHRawTemplateName, "World French".Split(), wp8Headers); break; } await zumoPush.RegisterAsync(reg); pushChannel.ChannelUriUpdated += new EventHandler<NotificationChannelUriEventArgs>(async (o, args) => { await zumoPush.RegisterAsync(reg); }); test.AddLog("Registered to Notification hub"); } else { await zumoPush.RegisterNativeAsync(pushChannel.ChannelUri.ToString(), "tag1 tag2".Split()); pushChannel.ChannelUriUpdated += new EventHandler<NotificationChannelUriEventArgs>(async (o, args) => { await zumoPush.RegisterNativeAsync(args.ChannelUri.ToString(), "tag1 tag2".Split()); }); test.AddLog("Registered with NH"); } } pushChannel.HttpNotificationReceived += pushChannel_HttpNotificationReceived; pushChannel.ShellToastNotificationReceived += pushChannel_ShellToastNotificationReceived; test.AddLog("Registered to raw / shell toast events"); if (pushChannel.ConnectionStatus != ChannelConnectionStatus.Connected || pushChannel.ChannelUri == null) { test.AddLog("Error, push channel isn't connected or channel URI is null"); return false; } else { return true; } }, registerTemplate ? ZumoTestGlobals.RuntimeFeatureNames.NH_PUSH_ENABLED : null); }
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(); TemplateRegistration reg = null; switch (nhNotificationType.ToLower()) { case "toast": var toastTemplate = BuildXmlToastPayload("sendToastText01", "$(News_English)"); reg = new TemplateRegistration(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 TemplateRegistration(ZumoPushTests.pushChannel.Uri, rawTemplate, ZumoPushTestGlobals.NHRawTemplateName, "World Mandarin".Split(), wnsHeaders); break; case "badge": var badgeTemplate = BuildBadgeXmlPayload("$(News_Badge)"); reg = new TemplateRegistration(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 TemplateRegistration(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); }
/// <summary> /// Register a particular channelUri with a template /// </summary> /// <param name="channelUri">The channelUri to register</param> /// <param name="xmlTemplate">The XmlDocument defining the template</param> /// <param name="templateName">The template name</param> /// <param name="tags">The tags to register to receive notifcations 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 TemplateRegistration(channelUri, xmlTemplate, templateName, tags, null); return this.RegistrationManager.RegisterAsync(registration); }