From 5a34d4cd6d5e79eb2f77ac323b555a3624cd8bf6 Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Fri, 1 Jul 2016 19:16:47 -0400 Subject: [PATCH] Check push registration once per day --- src/App/Constants.cs | 2 +- src/App/Pages/VaultListSitesPage.cs | 16 ++++++++++++---- src/App/Services/PushNotificationListener.cs | 11 +++++++++-- 3 files changed, 22 insertions(+), 7 deletions(-) diff --git a/src/App/Constants.cs b/src/App/Constants.cs index c23d7750b..a30ee4dcd 100644 --- a/src/App/Constants.cs +++ b/src/App/Constants.cs @@ -7,7 +7,7 @@ public const string SettingLockSeconds = "lockSeconds"; public const string SettingLastBackgroundedDate = "lastBackgroundedDate"; - public const string PushPromptShown = "initialPushPromptShown"; + public const string PushLastRegistration = "lastPushRegistration"; } } diff --git a/src/App/Pages/VaultListSitesPage.cs b/src/App/Pages/VaultListSitesPage.cs index 5c1c51c30..64304f8df 100644 --- a/src/App/Pages/VaultListSitesPage.cs +++ b/src/App/Pages/VaultListSitesPage.cs @@ -82,13 +82,21 @@ namespace Bit.App.Pages if(_connectivity.IsConnected && Device.OS == TargetPlatform.iOS && !_favorites) { - if(!_settings.GetValueOrDefault(Constants.PushPromptShown)) + var pushPromptShow = _settings.GetValueOrDefault(Constants.PushPromptShown); + if(!pushPromptShow) { _settings.AddOrUpdateValue(Constants.PushPromptShown, true); - await _userDialogs.AlertAsync("bitwarden keeps your vault automatically synced by using push notifications. For the best possible experience, please select \"Ok\" on the following prompt when asked to enable push notifications.", "Enable Automatic Syncing", "Ok, got it!"); + await _userDialogs.AlertAsync(@"bitwarden keeps your vault automatically synced by using push notifications. + For the best possible experience, please select ""Ok"" on the following prompt when asked to enable push notifications.", + "Enable Automatic Syncing", "Ok, got it!"); } - _pushNotification.Register(); + // Check push registration once per day + var lastPushRegistration = _settings.GetValueOrDefault(Constants.PushLastRegistration); + if(!pushPromptShow || !lastPushRegistration.HasValue || (DateTime.UtcNow - lastPushRegistration) > TimeSpan.FromDays(1)) + { + _pushNotification.Register(); + } } } @@ -193,7 +201,7 @@ namespace Bit.App.Pages { private VaultListSitesPage _page; - public static readonly BindableProperty SiteParameterProperty = BindableProperty.Create(nameof(SiteParameter), + public static readonly BindableProperty SiteParameterProperty = BindableProperty.Create(nameof(SiteParameter), typeof(VaultListPageModel.Site), typeof(VaultListViewCell), null); public VaultListViewCell(VaultListSitesPage page) diff --git a/src/App/Services/PushNotificationListener.cs b/src/App/Services/PushNotificationListener.cs index 940fdd11b..bf6ea13c3 100644 --- a/src/App/Services/PushNotificationListener.cs +++ b/src/App/Services/PushNotificationListener.cs @@ -5,6 +5,8 @@ using Newtonsoft.Json; using Newtonsoft.Json.Linq; using Bit.App.Abstractions; using Bit.App.Models; +using Plugin.Settings.Abstractions; +using System; namespace Bit.App.Services { @@ -15,17 +17,20 @@ namespace Bit.App.Services private readonly IDeviceApiRepository _deviceApiRepository; private readonly IAuthService _authService; private readonly IAppIdService _appIdService; + private readonly ISettings _settings; public PushNotificationListener( ISyncService syncService, IDeviceApiRepository deviceApiRepository, IAuthService authService, - IAppIdService appIdService) + IAppIdService appIdService, + ISettings settings) { _syncService = syncService; _deviceApiRepository = deviceApiRepository; _authService = authService; _appIdService = appIdService; + _settings = settings; } public void OnMessage(JObject values, DeviceType deviceType) @@ -33,7 +38,7 @@ namespace Bit.App.Services _showNotification = false; Debug.WriteLine("Message Arrived: {0}", JsonConvert.SerializeObject(values)); - var type = (Enums.PushType)values.GetValue("type", System.StringComparison.OrdinalIgnoreCase).ToObject(); + var type = (Enums.PushType)values.GetValue("type", StringComparison.OrdinalIgnoreCase).ToObject(); switch(type) { case Enums.PushType.SyncCipherUpdate: @@ -71,6 +76,7 @@ namespace Bit.App.Services if(response.Succeeded) { Debug.WriteLine("Registered device with server."); + _settings.AddOrUpdateValue(Constants.PushLastRegistration, DateTime.UtcNow); } else { @@ -81,6 +87,7 @@ namespace Bit.App.Services public void OnUnregistered(DeviceType deviceType) { Debug.WriteLine("Push Notification - Device Unnregistered"); + _settings.Remove(Constants.PushLastRegistration); } public void OnError(string message, DeviceType deviceType)