From 7b9c00cd77de8f51f027988a005344c49ac529b3 Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Sat, 20 Aug 2016 19:19:10 -0400 Subject: [PATCH] More accurate push registration for android --- src/Android/Android.csproj | 6 +---- src/Android/MainApplication.cs | 23 +++++++++++++++----- src/App/Pages/LoginPage.cs | 8 +++++++ src/App/Services/PushNotificationListener.cs | 3 ++- 4 files changed, 28 insertions(+), 12 deletions(-) diff --git a/src/Android/Android.csproj b/src/Android/Android.csproj index 98c2f782c..394e144db 100644 --- a/src/Android/Android.csproj +++ b/src/Android/Android.csproj @@ -57,20 +57,16 @@ 4 False SdkOnly - True False False - - - - Xamarin False False False False False + 1G diff --git a/src/Android/MainApplication.cs b/src/Android/MainApplication.cs index f86e6027a..c2cd1a614 100644 --- a/src/Android/MainApplication.cs +++ b/src/Android/MainApplication.cs @@ -41,8 +41,17 @@ namespace Bit.Android RegisterActivityLifecycleCallbacks(this); AppContext = ApplicationContext; StartPushService(); - Resolver.Resolve().Unregister(); - Resolver.Resolve().Register(); + + var pushNotification = Resolver.Resolve(); +#if DEBUG + // When running in debug mode you must unregister the previous instance first or else things wont work + // ref https://github.com/rdelrosario/xamarin-plugins/issues/65 + pushNotification.Unregister(); +#endif + if(Resolver.Resolve().IsAuthenticated) + { + pushNotification.Register(); + } } public override void OnTerminate() @@ -85,9 +94,10 @@ namespace Bit.Android public static void StartPushService() { AppContext.StartService(new Intent(AppContext, typeof(PushNotificationService))); - if(global::Android.OS.Build.VERSION.SdkInt >= global::Android.OS.BuildVersionCodes.Kitkat) + if(Build.VERSION.SdkInt >= BuildVersionCodes.Kitkat) { - PendingIntent pintent = PendingIntent.GetService(AppContext, 0, new Intent(AppContext, typeof(PushNotificationService)), 0); + PendingIntent pintent = PendingIntent.GetService(AppContext, 0, new Intent(AppContext, + typeof(PushNotificationService)), 0); AlarmManager alarm = (AlarmManager)AppContext.GetSystemService(AlarmService); alarm.Cancel(pintent); } @@ -96,9 +106,10 @@ namespace Bit.Android public static void StopPushService() { AppContext.StopService(new Intent(AppContext, typeof(PushNotificationService))); - if(global::Android.OS.Build.VERSION.SdkInt >= global::Android.OS.BuildVersionCodes.Kitkat) + if(Build.VERSION.SdkInt >= BuildVersionCodes.Kitkat) { - PendingIntent pintent = PendingIntent.GetService(AppContext, 0, new Intent(AppContext, typeof(PushNotificationService)), 0); + PendingIntent pintent = PendingIntent.GetService(AppContext, 0, new Intent(AppContext, + typeof(PushNotificationService)), 0); AlarmManager alarm = (AlarmManager)AppContext.GetSystemService(AlarmService); alarm.Cancel(pintent); } diff --git a/src/App/Pages/LoginPage.cs b/src/App/Pages/LoginPage.cs index fc76f43f8..b1dc26498 100644 --- a/src/App/Pages/LoginPage.cs +++ b/src/App/Pages/LoginPage.cs @@ -10,6 +10,7 @@ using XLabs.Ioc; using Acr.UserDialogs; using System.Threading.Tasks; using Plugin.Settings.Abstractions; +using PushNotification.Plugin.Abstractions; namespace Bit.App.Pages { @@ -23,6 +24,7 @@ namespace Bit.App.Pages private ISyncService _syncService; private ISettings _settings; private IGoogleAnalyticsService _googleAnalyticsService; + private IPushNotification _pushNotification; private readonly string _email; public LoginPage(string email = null) @@ -37,6 +39,7 @@ namespace Bit.App.Pages _syncService = Resolver.Resolve(); _settings = Resolver.Resolve(); _googleAnalyticsService = Resolver.Resolve(); + _pushNotification = Resolver.Resolve(); Init(); } @@ -201,6 +204,11 @@ namespace Bit.App.Pages _googleAnalyticsService.RefreshUserId(); _googleAnalyticsService.TrackAppEvent("LoggedIn"); + if(Device.OS == TargetPlatform.Android) + { + _pushNotification.Register(); + } + if(_authService.IsAuthenticatedTwoFactor) { await Navigation.PushAsync(new LoginTwoFactorPage()); diff --git a/src/App/Services/PushNotificationListener.cs b/src/App/Services/PushNotificationListener.cs index f6d4dbd84..8b34e5a9c 100644 --- a/src/App/Services/PushNotificationListener.cs +++ b/src/App/Services/PushNotificationListener.cs @@ -83,7 +83,8 @@ namespace Bit.App.Services return; } - var response = await _deviceApiRepository.PutTokenAsync(_appIdService.AppId, new Models.Api.DeviceTokenRequest(token)); + var response = await _deviceApiRepository.PutTokenAsync(_appIdService.AppId, + new Models.Api.DeviceTokenRequest(token)); if(response.Succeeded) { Debug.WriteLine("Registered device with server.");