public async Task <IActionResult> SendPushNotification(PushNotificationModel model) { var user = await _userManager.FindByIdAsync(model.UserId); if (user == null) { return(Json(new { result = false })); } NotificationCreateResult pushResult; try { pushResult = PushNotificationHelper.SendPushOneSignal(user.Email.ToLowerInvariant(), model.Message); // .SendAPNSNotification(model.DeviceToken, model.Message); _context.Files.Add(new FileStorage { Content = $"{model.Message}", Type = FileStorageType.PushNotification, CreatedAt = DateTime.UtcNow, MimeType = "text/plain", Title = "Push Message", Owner = user, Viewed = true }); await _context.SaveChangesAsync(); } catch (Exception e) { return(Json(new { exception = e.Message, result = false })); } return(Json(new { result = pushResult.Recipients > 0 })); }
public override void OnMessageReceived(RemoteMessage remoteMessage) { var setup = MvxAndroidSetupSingleton.EnsureSingletonAvailable(Application.Context); setup.EnsureInitialized(); if (remoteMessage?.Data != null && PushNotificationHelper.TryParse(remoteMessage.Data, out NotificationType type, out string payload) && Mvx.IoCProvider.Resolve <IAppService>() is IAppService appService) { new Thread(() => appService.RaiseNotificationReceived(type, payload)).Start(); ShowForegroundNotification(type, payload, remoteMessage.Data); } }
private static async System.Threading.Tasks.Task RegisterForPush(bool registerAgain = true) { PushNotificationHelper.RegisterPushNotificationChannel(); var helper = new TaskHelper(); var pushNotificationTask = await helper.RegisterTask(nameof(PushNotificationTask), typeof(PushNotificationTask).FullName, new PushNotificationTrigger(), registerAgain); if (pushNotificationTask == null) { Debug.WriteLine("Push notification background task is not started"); } }
/// <summary> /// Initializes new tasks and runs tasks scheduling it /// for execution to the current TaskScheduler. /// </summary> private void ManageTasks() { lock (tasks) { List <Task> tasksToStart = tasks.FindAll(t => t.TaskData.TaskState == TaskState.InQueue); if (tasksToStart.Count != 0) { foreach (Task taskToStart in tasksToStart) { lock (taskToStart) { Task task = tasks.Single(t => t.TaskData.Id == taskToStart.TaskData.Id); task.TaskData.TaskState = TaskState.InProgress; var cancellationTokenSource = new CancellationTokenSource(); CancellationToken token = cancellationTokenSource.Token; task.CancellationTokenSource = cancellationTokenSource; var systemTask = new SystemTask(() => { using (cancellationTokenSource.Token.Register(Thread.CurrentThread.Abort)) { ExecuteTaskAction(task); } }, token); SystemTask notificationTask = systemTask.ContinueWith((SystemTask t) => { cancellationTokenSource.Dispose(); var data = new Dictionary <string, string> { { "title", $"LibiadaWeb: Task completed" }, { "body", $"Task type: { task.TaskData.TaskType.GetDisplayValue() } \nExecution time: { task.TaskData.ExecutionTime }" }, { "icon", "/Content/DNA.png" }, { "tag", $"/{ task.TaskData.TaskType }/Result/{ task.TaskData.Id }" } }; PushNotificationHelper.Send(task.TaskData.UserId, data); }); task.SystemTask = systemTask; systemTask.Start(); } } } } }
private static async Task RegisterForPush(TaskHelper helper, bool registerAgain = true) { try { PushNotificationHelper.RegisterPushNotificationChannel(); var pushNotificationTask = await helper.RegisterTask(nameof(PushNotificationTask), typeof(PushNotificationTask).FullName, new PushNotificationTrigger(), registerAgain).AsTask(); if (pushNotificationTask == null) { Debug.WriteLine("Push notification background task is not started"); } } catch (Exception e) { Debug.WriteLine($"Failed to register for push notification. Error: {e.Message}"); } }
public override async void RegisteredForRemoteNotifications(UIApplication application, NSData deviceToken) { Hub = new SBNotificationHub(PushNotificationConstants.ListenConnectionString, PushNotificationConstants.NotificationHubName); await PushNotificationHelper.UpdateCurrentCountryTag(); // update registration with Azure Notification Hub Hub.UnregisterAll(deviceToken, (error) => { if (error != null) { Debug.WriteLine($"Unable to call unregister {error}"); return; } var tags = new NSSet(PushNotificationConstants.SubscriptionTags); Hub.RegisterNative(deviceToken, tags, (errorCallback) => { if (errorCallback != null) { Debug.WriteLine($"RegisterNativeAsync error: {errorCallback}"); UserDialogs.Instance.Toast("Error Registering to Push Notifications"); } }); var templateExpiration = DateTime.Now.AddDays(120) .ToString(System.Globalization.CultureInfo.CreateSpecificCulture("en-US")); Hub.RegisterTemplate(deviceToken, "defaultTemplate", PushNotificationConstants.APNTemplateBody, templateExpiration, tags, (errorCallback) => { if (errorCallback != null) { if (errorCallback != null) { Debug.WriteLine($"RegisterTemplateAsync error: {errorCallback}"); UserDialogs.Instance.Toast("Error Registering to Push Notifications template"); } } }); }); }
private async Task DelayInit() { await Task.Yield(); //wait full appearing RefreshMap(); FilterCategorySwitched += SwitchFilter_Toggled; DataRefreshing += Filter_DataRefreshing; ViewPage.Appearing += ViewPage_Appearing; MainPage.IsShowRightPanel = App.PushNotificationData == null; AppMobileService.Locaion.PositionUpdated += MapLocation_Position; AppMobileService.Locaion.HeadingUpdated += MapLocation_Heading; AppMobileService.Locaion.StartListening(); PushNotificationHelper.EnablePushNotifications(PushNotificationHelper.IsEnabled); await App.ProcessPushNotification(); }
private async Task SendRegistrationToServer(string token) { try { NotificationHub hub = new NotificationHub(PushNotificationConstants.NotificationHubName, PushNotificationConstants.ListenConnectionString, this); // Get countryName for subscription tags await PushNotificationHelper.UpdateCurrentCountryTag(); // register device with Azure Notification Hub using the token from FCM Registration registration = hub.Register(token, PushNotificationConstants.SubscriptionTags); // subscribe to the SubscriptionTags list with a simple template. string pnsHandle = registration.PNSHandle; TemplateRegistration templateReg = hub.RegisterTemplate(pnsHandle, "defaultTemplate", PushNotificationConstants.FCMTemplateBody, PushNotificationConstants.SubscriptionTags); } catch (Exception e) { Log.Error(PushNotificationConstants.DebugTag, $"Error registering device: {e.Message}"); } }