Skip to content

Commit

Permalink
Send newsfeeds as notifications (#138)
Browse files Browse the repository at this point in the history
* Send newsfeeds as notifications

* Send Wi-Fi newsfeeds
  • Loading branch information
OfirMatasas authored Sep 15, 2023
1 parent 12a5258 commit e0a5225
Show file tree
Hide file tree
Showing 11 changed files with 153 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,8 @@ private static void createDocumentForEachUser(JToken json, string type, ref List
extraElement
}
},
{ "user", user }
{ "user", user },
{ "shouldBeNotified", Convert.ToString(json["shouldBeNotified"] ?? user) }
};

if (type.ToLower().Equals(Constants.NOTIFICATION_TYPE_LOCATION_LOWER))
Expand Down
11 changes: 7 additions & 4 deletions Notify/Notify/Notify.Android/MainActivity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -142,13 +142,16 @@ private void createNotificationFromIntent(Intent intent)
string message = intent.GetStringExtra(AndroidNotificationManager.messageKey);
string data = intent.GetStringExtra(AndroidNotificationManager.dataKey);
Notification notification = Newtonsoft.Json.JsonConvert.DeserializeObject<Notification>(data);

NotificationsPageViewModel viewModel = NotificationsPageViewModel.Instance;
viewModel.ExpandedNotificationId = notification.ID;
viewModel.SelectedFilter = Constants.FILTER_TYPE_ALL;

DependencyService.Get<INotificationManager>().ReceiveNotification(title, message, notification);
App.Current.MainPage.Navigation.PushAsync(new NotificationsPage(viewModel));

if (notification != null)
{
viewModel.ExpandedNotificationId = notification.ID;
viewModel.SelectedFilter = Constants.FILTER_TYPE_ALL;
App.Current.MainPage.Navigation.PushAsync(new NotificationsPage(viewModel));
}
}
}
}
Expand Down
24 changes: 20 additions & 4 deletions Notify/Notify/Notify.Android/Managers/AndroidWiFiManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,16 @@ private static void sendNotificationsForArrivalDestinations(List<Notification> n
{
if (isArrivalNotification)
{
r_Logger.LogInformation($"Sending notification for arrival notification: {notification.Name}");
DependencyService.Get<INotificationManager>().SendNotification(notification);
if (notification.ShouldBeNotified.Equals(notification.Creator))
{
r_Logger.LogInformation($"Sending newsfeed to creator {notification.Creator} for arrival notification: {notification.Name}");
AzureHttpClient.Instance.SendNewsfeed(new Newsfeed(notification.Creator, $"{notification.Target} Arrived {notification.TypeInfo}", $"{notification.Target} has arrived to {notification.TypeInfo}"));
}
else
{
r_Logger.LogInformation($"Sending notification for arrival notification: {notification.Name}");
DependencyService.Get<INotificationManager>().SendNotification(notification);
}

if (notification.IsPermanent)
{
Expand Down Expand Up @@ -191,8 +199,16 @@ private static void sendNotificationsForLeaveDestinations(List<Notification> not
{
if (isLeaveNotification)
{
r_Logger.LogInformation($"Sending notification for leave notification: {notification.Name}");
DependencyService.Get<INotificationManager>().SendNotification(notification);
if(notification.ShouldBeNotified.Equals(notification.Creator))
{
r_Logger.LogInformation($"Sending newsfeed to creator {notification.Creator} for leave notification: {notification.Name}");
AzureHttpClient.Instance.SendNewsfeed(new Newsfeed(notification.Creator, $"{notification.Target} Left {notification.TypeInfo}", $"{notification.Target} has left {notification.TypeInfo}"));
}
else
{
r_Logger.LogInformation($"Sending notification for leave notification: {notification.Name}");
DependencyService.Get<INotificationManager>().SendNotification(notification);
}

if(notification.IsPermanent)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
using System;
using Android.App;
using Android.Content;
using Android.Graphics;
using Android.OS;
using AndroidX.Core.App;
using Newtonsoft.Json;
using Notify.Core;
using Notify.Helpers;
using Notify.Notifications;
using Notify.Services;
using Xamarin.Forms;
using AndroidApp = Android.App.Application;
using Notification = Android.App.Notification;

[assembly: Dependency(typeof(Notify.Droid.Notifications.AndroidNotificationManager))]
namespace Notify.Droid.Notifications
Expand Down Expand Up @@ -159,5 +160,20 @@ long GetNotifyTime(DateTime notifyTime)

return utcAlarmTime; // milliseconds
}

public void SendNewsfeed(Newsfeed newsfeed)
{
r_Logger.LogDebug($"Sending newsfeed with title: {newsfeed.Title} and content: {newsfeed.Content}");

try
{
DependencyService.Get<INotificationManager>()
.SendNotification(newsfeed.Title, newsfeed.Content, null);
}
catch (Exception ex)
{
r_Logger.LogError($"Error sending notification: {ex.Message}");
}
}
}
}
28 changes: 24 additions & 4 deletions Notify/Notify/Notify/AppShell.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public partial class AppShell
private static readonly object m_InitializeLock = new object();
private static bool m_IsInitialized;
private BluetoothManager m_BluetoothManager;
private DateTime m_LastTimeCheckedForNewsfeeds = DateTime.MinValue;

public AppShell()
{
Expand All @@ -45,15 +46,15 @@ private void initializeAppShell()
Connectivity.ConnectivityChanged += internetConnectivityChanged;
m_BluetoothManager = BluetoothManager.Instance;
m_BluetoothManager.StartBluetoothScanning();
setNoficicationManagerNotificationReceived();
setNotificationManagerNotificationReceived();
setMessagingCenterSubscriptions();

if (Preferences.Get(Constants.START_LOCATION_SERVICE, false))
{
startService();
}

retriveDestinations();
retrieveDestinations();
}
}
}
Expand All @@ -72,7 +73,7 @@ private void setMessagingCenterSubscriptions()
setMessagingCenterLocationArrivedMessageSubscription();
}

private async void retriveDestinations()
private async void retrieveDestinations()
{
await Task.Run(() =>
{
Expand Down Expand Up @@ -128,9 +129,28 @@ private void setMessagingCenterLocationSubscription()

checkIfDynamicLocationNotificationShouldBeUpdated(location);
sendAllRelevantLocationNotifications(location);
getNewsfeeds();
});
}

private async void getNewsfeeds()
{
if (DateTime.Now - m_LastTimeCheckedForNewsfeeds > TimeSpan.FromMinutes(1))
{
LoggerService.Instance.LogInformation("Getting newsfeeds");
List<Newsfeed> newsfeeds = await AzureHttpClient.Instance.GetNewsfeeds();
LoggerService.Instance.LogInformation($"Got {newsfeeds.Count} newsfeeds");

foreach (Newsfeed newsfeed in newsfeeds)
{
LoggerService.Instance.LogDebug($"Sending newsfeed: {newsfeed.Title}, {newsfeed.Content}");
DependencyService.Get<INotificationManager>().SendNewsfeed(newsfeed);
}

m_LastTimeCheckedForNewsfeeds = DateTime.Now;
}
}

private async void checkIfDynamicLocationNotificationShouldBeUpdated(Location location)
{
string destinationsJson = Preferences.Get(Constants.PREFERENCES_DESTINATIONS, string.Empty);
Expand Down Expand Up @@ -368,7 +388,7 @@ private void updateStatusOfNotifications(string newStatus, List<Notification> se
AzureHttpClient.Instance.UpdateNotificationsStatus(sentNotifications, newStatus);
}

private void setNoficicationManagerNotificationReceived()
private void setNotificationManagerNotificationReceived()
{
notificationManager.NotificationReceived += (sender, eventArgs) =>
{
Expand Down
51 changes: 51 additions & 0 deletions Notify/Notify/Notify/Azure/HttpClient/AzureHttpClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using Newtonsoft.Json.Linq;
using Notify.Core;
using Notify.Helpers;
using Notify.Notifications;
using Notify.Services;
using Notify.WiFi;
using Xamarin.Essentials;
Expand Down Expand Up @@ -281,6 +282,8 @@ public async Task<List<Notification>> GetNotifications()
createNewDynamicDestinations(notifications);
DependencyService.Get<IWiFiManager>().SendNotifications(null, null);

GetNewsfeedsAndSendThem();

return notifications;
}

Expand Down Expand Up @@ -457,6 +460,7 @@ private async Task<bool> updateNotification(string ID, string name, string descr

public async Task<List<User>> GetFriends()
{
GetNewsfeedsAndSendThem();
await GetFriendsPermissions();

return await GetData(
Expand Down Expand Up @@ -1081,5 +1085,52 @@ private async Task<HttpResponseMessage> deleteAsync(string requestUri, StringCon

return await m_HttpClient.SendAsync(request);
}

public async Task<List<Newsfeed>> GetNewsfeeds()
{
return await GetData(
endpoint: Constants.AZURE_FUNCTIONS_PATTERN_NEWSFEED,
preferencesKey: Constants.PREFERENCES_NEWSFEED,
converter: Converter.ToNewsfeed);
}

public async void GetNewsfeedsAndSendThem()
{
List<Newsfeed> newsfeeds = await GetNewsfeeds();

foreach (Newsfeed newsfeed in newsfeeds)
{
DependencyService.Get<INotificationManager>().SendNewsfeed(newsfeed);
}
}

public void SendNewsfeed(Newsfeed newsfeed)
{
HttpResponseMessage response;
string json;
dynamic data = new JObject
{
{ "username", newsfeed.Username },
{ "title", newsfeed.Title },
{ "content", newsfeed.Content },
};

json = JsonConvert.SerializeObject(data);
r_Logger.LogInformation($"request:{Environment.NewLine}{json}");

try
{
response = postAsync(
requestUri: Constants.AZURE_FUNCTIONS_PATTERN_NEWSFEED,
content: createJsonStringContent(json)).Result;

response.EnsureSuccessStatusCode();
r_Logger.LogInformation($"Successful status code from Azure Function from SendNewsfeeds");
}
catch (Exception ex)
{
r_Logger.LogError($"Error occurred on SendNewsfeeds: {ex.Message}");
}
}
}
}
16 changes: 16 additions & 0 deletions Notify/Notify/Notify/Core/Newsfeed.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
namespace Notify.Core
{
public class Newsfeed
{
public string Username { get; set; }
public string Title { get; set; }
public string Content { get; set; }

public Newsfeed(string username, string title, string content)
{
Username = username;
Title = title;
Content = content;
}
}
}
4 changes: 3 additions & 1 deletion Notify/Notify/Notify/Core/Notification.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public class Notification
public string Activation { get; set; }
public bool IsPermanent { get; set; }
public string Target { get; set; }
public string ShouldBeNotified { get; set; }

public bool IsPending => Status == Constants.NOTIFICATION_STATUS_PENDING;
public bool IsRenewable => Status == Constants.NOTIFICATION_STATUS_EXPIRED && Type != NotificationType.Time;
Expand All @@ -37,7 +38,7 @@ public Notification()

}

public Notification(string id, string name, string description, DateTime creationDateTime, string status, string creator, NotificationType type, object typeInfo, string target, string activation, bool permanent)
public Notification(string id, string name, string description, DateTime creationDateTime, string status, string creator, NotificationType type, object typeInfo, string target, string activation, bool permanent, string shouldBeNotified)
{
ID = id;
Name = name;
Expand All @@ -50,6 +51,7 @@ public Notification(string id, string name, string description, DateTime creatio
Activation = activation;
IsPermanent = permanent;
Target = target;
ShouldBeNotified = shouldBeNotified;
}

public Notification(string id)
Expand Down
2 changes: 2 additions & 0 deletions Notify/Notify/Notify/Helpers/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ public static List<string> GetRaceTypesList()
public static readonly string AZURE_FUNCTIONS_PATTERN_NOTIFICATION_UPDATE_DYNAMIC = $"{AZURE_FUNCTIONS_PATTERN_NOTIFICATION_UPDATE}/dynamic";
public static readonly string AZURE_FUNCTIONS_PATTERN_NOTIFICATION_UPDATE_STATUS = $"{AZURE_FUNCTIONS_PATTERN_NOTIFICATION}/status";
public static readonly string AZURE_FUNCTIONS_PATTERN_NOTIFICATION_RENEW = $"{AZURE_FUNCTIONS_PATTERN_NOTIFICATION}/renew";
public static readonly string AZURE_FUNCTIONS_PATTERN_NEWSFEED = "newsfeed";
public static readonly string AZURE_FUNCTIONS_PATTERN_DESTINATION = "destination";
public static readonly string AZURE_FUNCTIONS_PATTERN_DESTINATION_UPDATE = AZURE_FUNCTIONS_PATTERN_DESTINATION + "/update";
public static readonly string AZURE_FUNCTIONS_PATTERN_DESTINATION_SUGGESTIONS = AZURE_FUNCTIONS_PATTERN_DESTINATION + "/suggestions";
Expand Down Expand Up @@ -198,6 +199,7 @@ public static List<string> GetRaceTypesList()
#region Prefrences_Keys

public static readonly string PREFERENCES_NOTIFICATIONS = "Notifications";
public static readonly string PREFERENCES_NEWSFEED = "Newsfeed";
public static readonly string PREFERENCES_DESTINATIONS = "Destinations";
public static readonly string PREFERENCES_FRIENDS = "Friends";
public static readonly string PREFERENCES_PENDING_FRIEND_REQUESTS = "PendingFriendRequests";
Expand Down
11 changes: 10 additions & 1 deletion Notify/Notify/Notify/Helpers/Converter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,8 @@ public static Notification ToNotification(dynamic notification)
typeInfo: notificationTypeValue,
activation: activation,
permanent: notification.notification.permanent != null && (bool)notification.notification.permanent,
target: (string)notification.user);
target: (string)notification.user,
shouldBeNotified: (string)notification.shouldBeNotified ?? (string)notification.user);
}

public static User ToFriend(dynamic friend)
Expand Down Expand Up @@ -282,5 +283,13 @@ public static Permission ToPermission(dynamic permission)
timeNotificationPermission: (string)permission.time,
dynamicNotificationPermission: (string)permission.dynamic);
}

public static Newsfeed ToNewsfeed(dynamic newsfeed)
{
return new Newsfeed(
username: (string)newsfeed.username,
title: (string)newsfeed.title,
content: (string)newsfeed.content);
}
}
}
1 change: 1 addition & 0 deletions Notify/Notify/Notify/Notifications/INotificationManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ public interface INotificationManager
event EventHandler NotificationReceived;
void Initialize();
void SendNotification(Notification notification);
void SendNewsfeed(Newsfeed newsfeed);
void SendNotification(string title, string message, Notification notification, DateTime? notifyTime = null);
void ReceiveNotification(string title, string message, Notification notification);
}
Expand Down

0 comments on commit e0a5225

Please sign in to comment.