public AppCenterPushNotificationService(ILogger logger, HttpClient httpClient, IAppCenterConfiguration appCenterConfiguration) { this.logger = logger; this.httpClient = httpClient; this.appCenterConfiguration = this.EnsureConfig(appCenterConfiguration); var apiToken = this.appCenterConfiguration.ApiToken; this.httpClient.DefaultRequestHeaders.TryAddWithoutValidation("X-API-Token", apiToken); this.jsonSerializerSettings = new JsonSerializerSettings { DateFormatHandling = DateFormatHandling.IsoDateFormat, DateParseHandling = DateParseHandling.DateTime, DateTimeZoneHandling = DateTimeZoneHandling.Utc }; }
private IAppCenterConfiguration EnsureConfig(IAppCenterConfiguration config) { if (string.IsNullOrEmpty(config.ApiToken)) { this.logger.Log(LogLevel.Error, $"Invalid ApiToken"); throw new ArgumentException($"Use ApiToken provided by AppCenter", nameof(config.ApiToken)); } if (string.IsNullOrEmpty(config.OrganizationName)) { this.logger.Log(LogLevel.Error, $"Invalid OrganizationName"); throw new ArgumentException($"Use OrganizationName provided by AppCenter", nameof(config.OrganizationName)); } if (config.AppNames == null || config.AppNames.Count == 0) { this.logger.Log(LogLevel.Error, $"Invalid AppNames"); throw new ArgumentException($"Use AppNames set-up in AppCenter", nameof(config.AppNames)); } return(config); }
public AppCenterPushNotificationService(IAppCenterConfiguration appCenterConfiguration) : this(Logger.Current, new HttpClient(), appCenterConfiguration) { }
private static async Task SendPushNotificationAsync(IAppCenterConfiguration appCenterConfiguration, List <string> userIds) { var dateTimeNow = DateTime.Now; var appCenterPushNotificationService = new AppCenterPushNotificationService(new ConsoleLogger(), new HttpClient(), appCenterConfiguration); var pushMessage = new AppCenterPushMessage { Content = new AppCenterPushContent { Name = $"AppCenterPushUserIdsTarget_{dateTimeNow:yyyyMMdd_HHmmss}", Title = "AppCenterPushUserIdsTarget", Body = $"This message has been sent at {dateTimeNow:G}", CustomData = new Dictionary <string, string> { { "key", "value" } } }, Target = new AppCenterPushUserIdsTarget { UserIds = userIds } }; var appCenterPushResponses = await appCenterPushNotificationService.SendPushNotificationAsync(pushMessage); if (appCenterPushResponses.Any()) { System.Console.WriteLine(""); System.Console.WriteLine("SendPushNotificationAsync responses:"); System.Console.WriteLine("------------------------------------"); foreach (var appCenterPushResponse in appCenterPushResponses) { if (appCenterPushResponse is AppCenterPushSuccess success) { System.Console.ForegroundColor = ConsoleColor.Green; System.Console.WriteLine($"--> {appCenterConfiguration.AppNames[success.RuntimePlatform]}: {success.NotificationId}"); System.Console.ForegroundColor = ConsoleColor.Gray; } else if (appCenterPushResponse is AppCenterPushError error) { System.Console.ForegroundColor = ConsoleColor.Red; System.Console.WriteLine($"--> {appCenterConfiguration.AppNames[error.RuntimePlatform]}: Code={error.ErrorCode}, Message={error.ErrorMessage}"); System.Console.ForegroundColor = ConsoleColor.Gray; } } } else { System.Console.WriteLine(""); System.Console.WriteLine("No responses from AppCenter."); } System.Console.WriteLine(""); System.Console.WriteLine("GetPushNotificationsAsync:"); System.Console.WriteLine("--------------------------"); System.Console.WriteLine(""); var notificationOverviewResults = await appCenterPushNotificationService.GetPushNotificationsAsync(); if (notificationOverviewResults.Any()) { System.Console.WriteLine($"NotificationId\t\t\t\t\t| State\t\t| Success\t| Failure"); foreach (var result in notificationOverviewResults) { System.Console.WriteLine($"{result.NotificationId}\t| {result.State}\t| {result.PnsSendSuccess}\t\t| {result.PnsSendFailure}"); } } else { System.Console.WriteLine(""); System.Console.WriteLine("No responses from AppCenter."); } System.Console.ReadKey(); }