1
0
mirror of https://github.com/bitwarden/mobile.git synced 2024-06-22 09:55:16 +02:00
bitwarden-mobile/src/iOS/Services/iOSPushNotificationHandler.cs
Federico Maccaroni db7ca3b93e
BEEEP: Abstract and Centralize Logging (#1663)
* Abstracted App Center Logging into its own component, so that we can have it centralized in one place and we avoid checking for FDroid on all the places we want to use it

* Implemented the new logger where Crashes.TrackError was being used except on some specific cases

* Improved logging, added a debug logger and removed AppCenter to be used on DEBUG
2022-03-02 14:15:16 -03:00

106 lines
4.1 KiB
C#

using System;
using System.Diagnostics;
using Bit.App.Abstractions;
using Bit.Core.Services;
using Foundation;
using Newtonsoft.Json.Linq;
using UserNotifications;
using Xamarin.Forms;
namespace Bit.iOS.Services
{
public class iOSPushNotificationHandler : NSObject, IUNUserNotificationCenterDelegate
{
private const string TokenSetting = "token";
const string TAG = "##PUSH NOTIFICATIONS";
private readonly IPushNotificationListenerService _pushNotificationListenerService;
public iOSPushNotificationHandler(
IPushNotificationListenerService pushNotificationListenerService)
{
_pushNotificationListenerService = pushNotificationListenerService;
}
public void OnMessageReceived(NSDictionary userInfo)
{
try
{
Debug.WriteLine($"{TAG} - OnMessageReceived.");
var json = DictionaryToJson(userInfo);
var values = JObject.Parse(json);
var keyAps = new NSString("aps");
if (userInfo.ContainsKey(keyAps) && userInfo.ValueForKey(keyAps) is NSDictionary aps)
{
foreach (var apsKey in aps)
{
if (!values.TryGetValue(apsKey.Key.ToString(), out JToken temp))
{
values.Add(apsKey.Key.ToString(), apsKey.Value.ToString());
}
}
}
_pushNotificationListenerService.OnMessageAsync(values, Device.iOS);
}
catch (Exception ex)
{
Logger.Instance.Exception(ex);
}
}
public void OnErrorReceived(NSError error)
{
Debug.WriteLine($"{TAG} - Registration Failed.");
_pushNotificationListenerService.OnError(error.LocalizedDescription, Device.iOS);
}
public void OnRegisteredSuccess(NSData token)
{
Debug.WriteLine($"{TAG} - Successfully Registered.");
var hexDeviceToken = BitConverter.ToString(token.ToArray())
.Replace("-", string.Empty)
.ToLowerInvariant();
Debug.WriteLine($"{TAG} - Token: {hexDeviceToken}");
UNUserNotificationCenter.Current.Delegate = this;
_pushNotificationListenerService.OnRegisteredAsync(hexDeviceToken, Device.iOS);
NSUserDefaults.StandardUserDefaults.SetString(hexDeviceToken, TokenSetting);
NSUserDefaults.StandardUserDefaults.Synchronize();
}
private static string DictionaryToJson(NSDictionary dictionary)
{
var json = NSJsonSerialization.Serialize(dictionary, NSJsonWritingOptions.PrettyPrinted, out NSError error);
return json.ToString(NSStringEncoding.UTF8);
}
// To receive notifications in foreground on iOS 10 devices.
[Export("userNotificationCenter:willPresentNotification:withCompletionHandler:")]
public void WillPresentNotification(UNUserNotificationCenter center, UNNotification notification, Action<UNNotificationPresentationOptions> completionHandler)
{
Debug.WriteLine($"{TAG} WillPresentNotification {notification?.Request?.Content?.UserInfo}");
OnMessageReceived(notification?.Request?.Content?.UserInfo);
completionHandler(UNNotificationPresentationOptions.Alert);
}
[Export("userNotificationCenter:didReceiveNotificationResponse:withCompletionHandler:")]
public void DidReceiveNotificationResponse(UNUserNotificationCenter center, UNNotificationResponse response, Action completionHandler)
{
Debug.WriteLine($"{TAG} DidReceiveNotificationResponse {response?.Notification?.Request?.Content?.UserInfo}");
if (response.IsDefaultAction)
{
OnMessageReceived(response?.Notification?.Request?.Content?.UserInfo);
}
// Inform caller it has been handled
completionHandler();
}
}
}