private void CheckUserNotifications(User user) { if (!AccountReceivesNotificationsNow(user.Context.Server.Accounts.Current)) { return; } var notifications = user.Context.Server.Notifications.GetList().OrderBy(x => x.Id); if (!notifications.Any()) { return; } // При первом запуске уведомления не рассылаем. if (user.Context.LastNotificationId != 0) { foreach (var notification in notifications) { if (notification.Id > user.Context.LastNotificationId) { string text = ""; switch (notification.Type) { case Lers.NotificationType.CriticalError: text += Emoji.StopSign; break; case Lers.NotificationType.Incident: case Lers.NotificationType.EquipmentCalibrationRequired: text += Emoji.Warning; break; default: text += Emoji.InformationSource; break; } text += " " + notification.Message; if (!string.IsNullOrEmpty(notification.Url)) { text += $"\r\n{notification.Url}"; } bot.SendText(user.ChatId, text); } } } // Сохраним дату самого нового сообщения Lers.Notification lastNotify = notifications.OrderBy(x => x.Id).Last(); user.Context.LastNotificationId = lastNotify.Id; user.Context.LastNotificationDate = lastNotify.DateTime; }
/// <summary> /// Отображает новое уведомление в строке состояния Android. /// </summary> /// <param name="context"></param> /// <param name="notification"></param> private void ShowNotification(Context context, Lers.Notification notification) { // https://forums.xamarin.com/discussion/69009/notification-click-to-run-activity // По взаимодействию с формами. // https://stackoverflow.com/questions/34754149/android-xamarin-make-push-notification-not-create-a-new-activity-but-use-the-cur var valuesForActivity = new Bundle(); valuesForActivity.PutInt("NotificationId", notification.Id); var resultIntent = new Intent(context, typeof(MainActivity)); // Устанавливаем параметр "Идентификатор" для уведомление resultIntent.PutExtras(valuesForActivity); var stackBuilder = TaskStackBuilder.Create(context); // Указываем стартовую активность - Главная форма stackBuilder.AddParentStack(Java.Lang.Class.FromType(typeof(MainActivity))); stackBuilder.AddNextIntent(resultIntent); var pendingIntent = PendingIntent.GetActivity(context, 0, resultIntent, PendingIntentFlags.UpdateCurrent); int iconImportance = ResourceUtils.GetImageByImportance(notification.Importance); var notificationBuilder = new Notification.Builder(context) .SetAutoCancel(true) .SetSmallIcon(iconImportance) .SetContentTitle(notification.Type.GetDescription()) .SetContentText(notification.Message) .SetStyle(new Notification.BigTextStyle()) .SetContentIntent(pendingIntent); if (Build.VERSION.SdkInt >= BuildVersionCodes.O) { // Каналы поддерживаются только на OREO и выше. notificationBuilder.SetChannelId(Channels.GeneralChannelId); } var notificationManager = (NotificationManager)context.GetSystemService(Context.NotificationService); notificationManager.Notify(notification.Id, notificationBuilder.Build()); }