diff --git a/Forms.Mobile/Forms.Mobile.Android/AndroidNotificationManager.cs b/Forms.Mobile/Forms.Mobile.Android/AndroidNotificationManager.cs new file mode 100644 index 0000000..36f6fd4 --- /dev/null +++ b/Forms.Mobile/Forms.Mobile.Android/AndroidNotificationManager.cs @@ -0,0 +1,87 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using AndroidApp = Android.App.Application; +using Android.App; +using Android.Content; +using Android.OS; +using Android.Runtime; +using Android.Views; +using Android.Widget; +using Forms.Mobile.Middleware; +using Xamarin.Forms; +using Android.Support.V4.App; +using Android.Graphics; + +[assembly: Dependency(typeof(Forms.Mobile.Droid.AndroidNotificationManager))] +namespace Forms.Mobile.Droid +{ + public class AndroidNotificationManager : INotificationManager + { + private const string defaultChannelId = "LocalNotification"; + private const string defaultChannelName = "Ganeral notification"; + private const string defaultChannelDescription = "This is the ganeral notification for application."; + private const int pendingIntendId = 0; + private NotificationManager notificationManager; + private bool channelInitialized; + private int messageId; + + public void Initialize() + { + CreateNotificationChannel(); + } + + public void ReceiveNotification(string title, string message) + { + throw new NotImplementedException(); + } + + public void ScheduleNotification(string title, string message) + { + if (!channelInitialized) + { + CreateNotificationChannel(); + } + + Intent intent = new Intent(AndroidApp.Context, typeof(MainActivity)); + //Intent deleteIntent = new Intent(AndroidApp.Context, typeof()); + intent.PutExtra("title", title); + intent.PutExtra("message", message); + messageId++; + + PendingIntent pendingIntent = PendingIntent.GetActivity(AndroidApp.Context, pendingIntendId, intent, PendingIntentFlags.OneShot); + NotificationCompat.Builder builder = new NotificationCompat.Builder(AndroidApp.Context, defaultChannelId) + .SetContentIntent(pendingIntent) + .SetDefaults(NotificationCompat.DefaultAll) + .SetContentTitle(title) + .SetContentText(message) + .SetLargeIcon(BitmapFactory.DecodeResource(AndroidApp.Context.Resources, Resource.Mipmap.ic_launcher)) + .SetSmallIcon(Resource.Mipmap.ic_launcher) + .SetAutoCancel(true); + + Notification notification = builder.Build(); + notificationManager.Notify(messageId, notification); + } + + private void CreateNotificationChannel() + { + notificationManager = (NotificationManager)AndroidApp.Context.GetSystemService(AndroidApp.NotificationService); + + // If API >= 26 then we need to create a channel for notification. + if (Build.VERSION.SdkInt >= BuildVersionCodes.O) + { + var channelNameJava = new Java.Lang.String(defaultChannelName); + var channel = new NotificationChannel(defaultChannelId, channelNameJava, NotificationImportance.High) + { + Description = defaultChannelDescription + }; + + notificationManager.CreateNotificationChannel(channel); + } + + channelInitialized = true; + + } + } +} \ No newline at end of file diff --git a/Forms.Mobile/Forms.Mobile.Android/Forms.Mobile.Android.csproj b/Forms.Mobile/Forms.Mobile.Android/Forms.Mobile.Android.csproj index fb8ae84..1f03f6d 100644 --- a/Forms.Mobile/Forms.Mobile.Android/Forms.Mobile.Android.csproj +++ b/Forms.Mobile/Forms.Mobile.Android/Forms.Mobile.Android.csproj @@ -65,7 +65,9 @@ + + diff --git a/Forms.Mobile/Forms.Mobile.Android/Receivers/NotificationReceiver.cs b/Forms.Mobile/Forms.Mobile.Android/Receivers/NotificationReceiver.cs new file mode 100644 index 0000000..eba4670 --- /dev/null +++ b/Forms.Mobile/Forms.Mobile.Android/Receivers/NotificationReceiver.cs @@ -0,0 +1,22 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +using Android.App; +using Android.Content; +using Android.OS; +using Android.Runtime; +using Android.Views; +using Android.Widget; + +namespace Forms.Mobile.Droid.Receivers +{ + public class NotificationReceiver : BroadcastReceiver + { + public override void OnReceive(Context context, Intent intent) + { + + } + } +} \ No newline at end of file diff --git a/Forms.Mobile/Forms.Mobile/App.xaml.cs b/Forms.Mobile/Forms.Mobile/App.xaml.cs index b6947bf..8f403a0 100644 --- a/Forms.Mobile/Forms.Mobile/App.xaml.cs +++ b/Forms.Mobile/Forms.Mobile/App.xaml.cs @@ -6,6 +6,7 @@ using Xamarin.Forms.Xaml; using Prism.Unity; using Forms.Mobile.Services; +using Forms.Mobile.Services.Interfaces; [assembly: XamlCompilation(XamlCompilationOptions.Compile)] namespace Forms.Mobile @@ -34,6 +35,7 @@ protected override void RegisterTypes(IContainerRegistry containerRegistry) containerRegistry.RegisterForNavigation(); containerRegistry.RegisterForNavigation(); containerRegistry.RegisterSingleton(); + containerRegistry.RegisterSingleton(); } } } diff --git a/Forms.Mobile/Forms.Mobile/Middleware/INotificationManager.cs b/Forms.Mobile/Forms.Mobile/Middleware/INotificationManager.cs new file mode 100644 index 0000000..277a816 --- /dev/null +++ b/Forms.Mobile/Forms.Mobile/Middleware/INotificationManager.cs @@ -0,0 +1,9 @@ +namespace Forms.Mobile.Middleware +{ + public interface INotificationManager + { + void Initialize(); + void ScheduleNotification(string title, string message); + void ReceiveNotification(string title, string message); + } +} diff --git a/Forms.Mobile/Forms.Mobile/Services/IAuthenticationService.cs b/Forms.Mobile/Forms.Mobile/Services/Interfaces/IAuthenticationService.cs similarity index 100% rename from Forms.Mobile/Forms.Mobile/Services/IAuthenticationService.cs rename to Forms.Mobile/Forms.Mobile/Services/Interfaces/IAuthenticationService.cs diff --git a/Forms.Mobile/Forms.Mobile/Services/Interfaces/INotificationsService.cs b/Forms.Mobile/Forms.Mobile/Services/Interfaces/INotificationsService.cs new file mode 100644 index 0000000..b3ab658 --- /dev/null +++ b/Forms.Mobile/Forms.Mobile/Services/Interfaces/INotificationsService.cs @@ -0,0 +1,11 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Forms.Mobile.Services.Interfaces +{ + public interface INotificationsService + { + void ShowNotification(string title, string message); + } +} diff --git a/Forms.Mobile/Forms.Mobile/Services/NotificationService.cs b/Forms.Mobile/Forms.Mobile/Services/NotificationService.cs new file mode 100644 index 0000000..0d350df --- /dev/null +++ b/Forms.Mobile/Forms.Mobile/Services/NotificationService.cs @@ -0,0 +1,20 @@ +using Forms.Mobile.Middleware; +using Forms.Mobile.Services.Interfaces; +using Xamarin.Forms; + +namespace Forms.Mobile.Services +{ + public class NotificationsService : INotificationsService + { + private readonly INotificationManager notificationManager; + public NotificationsService() + { + notificationManager = DependencyService.Get(); + } + + public void ShowNotification(string title, string message) + { + notificationManager.ScheduleNotification(title, message); + } + } +} diff --git a/Forms.Mobile/Forms.Mobile/ViewModels/LoginPageViewModel.cs b/Forms.Mobile/Forms.Mobile/ViewModels/LoginPageViewModel.cs index 8651e3b..405621b 100644 --- a/Forms.Mobile/Forms.Mobile/ViewModels/LoginPageViewModel.cs +++ b/Forms.Mobile/Forms.Mobile/ViewModels/LoginPageViewModel.cs @@ -91,18 +91,19 @@ private void ValidatePwd() private async void SignIp() { - // Call Service for login. - var user = await authenticationService.Login(new LoginModel { Email = Login, Password = Pwd }); - if (user != null) - { - - await NavigationService.NavigateAsync("MainPage"); - } - else - { - // Show login error; - IsLoginErrorVisible = true; - } + await NavigationService.NavigateAsync("MainPage"); + //// Call Service for login. + //var user = await authenticationService.Login(new LoginModel { Email = Login, Password = Pwd }); + //if (user != null) + //{ + + // await NavigationService.NavigateAsync("MainPage"); + //} + //else + //{ + // // Show login error; + // IsLoginErrorVisible = true; + //} } } } diff --git a/Forms.Mobile/Forms.Mobile/ViewModels/MainPageViewModel.cs b/Forms.Mobile/Forms.Mobile/ViewModels/MainPageViewModel.cs index b2c4d0e..0780e9a 100644 --- a/Forms.Mobile/Forms.Mobile/ViewModels/MainPageViewModel.cs +++ b/Forms.Mobile/Forms.Mobile/ViewModels/MainPageViewModel.cs @@ -1,19 +1,27 @@ -using Prism.Commands; -using Prism.Mvvm; +using Forms.Mobile.Services.Interfaces; +using Prism.Commands; using Prism.Navigation; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; namespace Forms.Mobile.ViewModels { public class MainPageViewModel : ViewModelBase { - public MainPageViewModel(INavigationService navigationService) + private readonly INotificationsService notificationsService; + + public DelegateCommand LocalNotificationCommand { get; private set; } + + public MainPageViewModel(INavigationService navigationService, INotificationsService notificationsService) : base(navigationService) { Title = "Main Page"; + + LocalNotificationCommand = new DelegateCommand(ShowNotification); + this.notificationsService = notificationsService; + } + + private void ShowNotification() + { + notificationsService.ShowNotification("New Message", "This is a local notification!"); } } } diff --git a/Forms.Mobile/Forms.Mobile/Views/MainPage.xaml b/Forms.Mobile/Forms.Mobile/Views/MainPage.xaml index 1079282..a6755ee 100644 --- a/Forms.Mobile/Forms.Mobile/Views/MainPage.xaml +++ b/Forms.Mobile/Forms.Mobile/Views/MainPage.xaml @@ -5,7 +5,14 @@ NavigationPage.HasNavigationBar="False"> -