1
0
mirror of https://github.com/bitwarden/mobile synced 2026-01-08 11:33:31 +00:00

[SG-702] Tapping Push Notification does not open the account the request is for (#2112)

* [SG-702] Tap notification now switches accounts if it is a passwordless notification.

* [SG-702] Fix compilation errors

* [SG-702] Fixed iOS notification tap fix

* [SG-702] Notification data model

* [SG-702] Change method signature with object containing properties. PR fixes.
This commit is contained in:
André Bispo
2022-10-07 12:06:57 +01:00
committed by GitHub
parent 1e5eab0574
commit abada481b7
11 changed files with 128 additions and 12 deletions

View File

@@ -1,4 +1,5 @@
using System.Threading.Tasks;
using Bit.App.Models;
using Newtonsoft.Json.Linq;
namespace Bit.App.Abstractions
@@ -9,6 +10,7 @@ namespace Bit.App.Abstractions
Task OnRegisteredAsync(string token, string device);
void OnUnregistered(string device);
void OnError(string message, string device);
Task OnNotificationTapped(BaseNotificationData data);
bool ShouldShowNotification();
}
}

View File

@@ -1,5 +1,6 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using Bit.App.Models;
namespace Bit.App.Abstractions
{
@@ -10,7 +11,7 @@ namespace Bit.App.Abstractions
Task<string> GetTokenAsync();
Task RegisterAsync();
Task UnregisterAsync();
void SendLocalNotification(string title, string message, string notificationId);
void SendLocalNotification(string title, string message, BaseNotificationData data);
void DismissLocalNotification(string notificationId);
}
}

View File

@@ -0,0 +1,23 @@
using System;
namespace Bit.App.Models
{
public abstract class BaseNotificationData
{
public abstract string Type { get; }
public string Id { get; set; }
}
public class PasswordlessNotificationData : BaseNotificationData
{
public const string TYPE = "passwordlessNotificationData";
public override string Type => TYPE;
public int TimeoutInMinutes { get; set; }
public string UserEmail { get; set; }
}
}

View File

@@ -1,5 +1,6 @@
using System.Threading.Tasks;
using Bit.App.Abstractions;
using Bit.App.Models;
using Newtonsoft.Json.Linq;
namespace Bit.App.Services
@@ -28,5 +29,10 @@ namespace Bit.App.Services
{
return false;
}
public Task OnNotificationTapped(BaseNotificationData data)
{
return Task.FromResult(0);
}
}
}

View File

@@ -1,6 +1,7 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using Bit.App.Abstractions;
using Bit.App.Models;
namespace Bit.App.Services
{
@@ -30,6 +31,6 @@ namespace Bit.App.Services
public void DismissLocalNotification(string notificationId) { }
public void SendLocalNotification(string title, string message, string notificationId) { }
public void SendLocalNotification(string title, string message, BaseNotificationData data) { }
}
}

View File

@@ -1,9 +1,11 @@
#if !FDROID
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Bit.App.Abstractions;
using Bit.App.Models;
using Bit.App.Pages;
using Bit.App.Resources;
using Bit.Core;
@@ -11,6 +13,7 @@ using Bit.Core.Abstractions;
using Bit.Core.Enums;
using Bit.Core.Exceptions;
using Bit.Core.Models.Response;
using Bit.Core.Services;
using Bit.Core.Utilities;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
@@ -30,6 +33,7 @@ namespace Bit.App.Services
private IApiService _apiService;
private IMessagingService _messagingService;
private IPushNotificationService _pushNotificationService;
private ILogger _logger;
public async Task OnMessageAsync(JObject value, string deviceType)
{
@@ -147,7 +151,14 @@ namespace Bit.App.Services
await _stateService.SetPasswordlessLoginNotificationAsync(passwordlessLoginMessage, passwordlessLoginMessage?.UserId);
var userEmail = await _stateService.GetEmailAsync(passwordlessLoginMessage?.UserId);
_pushNotificationService.SendLocalNotification(AppResources.LogInRequested, String.Format(AppResources.ConfimLogInAttempForX, userEmail), Constants.PasswordlessNotificationId);
var notificationData = new PasswordlessNotificationData()
{
Id = Constants.PasswordlessNotificationId,
TimeoutInMinutes = Constants.PasswordlessNotificationTimeoutInMinutes,
UserEmail = userEmail,
};
_pushNotificationService.SendLocalNotification(AppResources.LogInRequested, String.Format(AppResources.ConfimLogInAttempForX, userEmail), notificationData);
_messagingService.Send("passwordlessLoginRequest", passwordlessLoginMessage);
break;
default:
@@ -213,6 +224,27 @@ namespace Bit.App.Services
Debug.WriteLine($"{TAG} error - {message}");
}
public async Task OnNotificationTapped(BaseNotificationData data)
{
Resolve();
try
{
if (data is PasswordlessNotificationData passwordlessNotificationData)
{
var notificationUserId = await _stateService.GetUserIdAsync(passwordlessNotificationData.UserEmail);
if (notificationUserId != null)
{
await _stateService.SetActiveUserAsync(notificationUserId);
_messagingService.Send(AccountsManagerMessageCommands.SWITCHED_ACCOUNT);
}
}
}
catch (Exception ex)
{
_logger.Exception(ex);
}
}
public bool ShouldShowNotification()
{
return _showNotification;
@@ -230,6 +262,7 @@ namespace Bit.App.Services
_apiService = ServiceContainer.Resolve<IApiService>("apiService");
_messagingService = ServiceContainer.Resolve<IMessagingService>("messagingService");
_pushNotificationService = ServiceContainer.Resolve<IPushNotificationService>();
_logger = ServiceContainer.Resolve<ILogger>();
_resolved = true;
}
}