1
0
mirror of https://github.com/bitwarden/mobile synced 2025-12-13 14:53:18 +00:00

cleanup and simplify ios push reg/handling

This commit is contained in:
Kyle Spearrin
2017-12-21 23:26:46 -05:00
parent fb76ecf198
commit bcf49ab396
5 changed files with 96 additions and 228 deletions

View File

@@ -1,20 +1,23 @@
using Bit.App.Abstractions;
using Bit.App.Utilities;
using Foundation;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading;
using UIKit;
using UserNotifications;
using Xamarin.Forms;
namespace Bit.iOS.Services
{
public class iOSPushNotificationService : IPushNotificationService, IPushNotificationHandler
public class iOSPushNotificationService : IPushNotificationService
{
public string Token => NSUserDefaults.StandardUserDefaults.StringForKey(PushNotificationContants.Token);
private const string TokenSetting = "token";
private readonly IPushNotificationListener _pushNotificationListener;
public iOSPushNotificationService(
IPushNotificationListener pushNotificationListener)
{
_pushNotificationListener = pushNotificationListener;
}
public string Token => NSUserDefaults.StandardUserDefaults.StringForKey(TokenSetting);
public void Register()
{
@@ -27,127 +30,9 @@ namespace Bit.iOS.Services
public void Unregister()
{
UIApplication.SharedApplication.UnregisterForRemoteNotifications();
OnUnregisteredSuccess();
}
private static string DictionaryToJson(NSDictionary dictionary)
{
NSError error;
var json = NSJsonSerialization.Serialize(dictionary, NSJsonWritingOptions.PrettyPrinted, out error);
return json.ToString(NSStringEncoding.UTF8);
}
public void OnMessageReceived(NSDictionary userInfo)
{
var parameters = new Dictionary<string, object>();
var json = DictionaryToJson(userInfo);
var values = JObject.Parse(json);
var keyAps = new NSString("aps");
if(userInfo.ContainsKey(keyAps))
{
var aps = userInfo.ValueForKey(keyAps) as NSDictionary;
if(aps != null)
{
foreach(var apsKey in aps)
{
parameters.Add(apsKey.Key.ToString(), apsKey.Value);
JToken temp;
if(!values.TryGetValue(apsKey.Key.ToString(), out temp))
{
values.Add(apsKey.Key.ToString(), apsKey.Value.ToString());
}
}
}
}
CrossPushNotification.PushNotificationListener.OnMessage(values, Device.iOS);
}
public void OnErrorReceived(NSError error)
{
Debug.WriteLine("{0} - Registration Failed.", PushNotificationContants.DomainName);
CrossPushNotification.PushNotificationListener.OnError(error.LocalizedDescription, Device.iOS);
}
public void OnRegisteredSuccess(NSData token)
{
Debug.WriteLine("{0} - Succesfully Registered.", PushNotificationContants.DomainName);
var trimmedDeviceToken = token.Description;
if(!string.IsNullOrWhiteSpace(trimmedDeviceToken))
{
trimmedDeviceToken = trimmedDeviceToken.Trim('<');
trimmedDeviceToken = trimmedDeviceToken.Trim('>');
trimmedDeviceToken = trimmedDeviceToken.Trim();
trimmedDeviceToken = trimmedDeviceToken.Replace(" ", "");
}
Console.WriteLine("{0} - Token: {1}", PushNotificationContants.DomainName, trimmedDeviceToken);
CrossPushNotification.PushNotificationListener.OnRegistered(trimmedDeviceToken, Device.iOS);
NSUserDefaults.StandardUserDefaults.SetString(trimmedDeviceToken, PushNotificationContants.Token);
_pushNotificationListener.OnUnregistered(Device.iOS);
NSUserDefaults.StandardUserDefaults.SetString(string.Empty, TokenSetting);
NSUserDefaults.StandardUserDefaults.Synchronize();
}
public void OnUnregisteredSuccess()
{
CrossPushNotification.PushNotificationListener.OnUnregistered(Device.iOS);
NSUserDefaults.StandardUserDefaults.SetString(string.Empty, PushNotificationContants.Token);
NSUserDefaults.StandardUserDefaults.Synchronize();
}
}
public interface IPushNotificationHandler
{
void OnMessageReceived(NSDictionary parameters);
void OnErrorReceived(NSError error);
void OnRegisteredSuccess(NSData token);
void OnUnregisteredSuccess();
}
internal class CrossPushNotification
{
private static Lazy<IPushNotificationService> Implementation = new Lazy<IPushNotificationService>(
() => new iOSPushNotificationService(),
LazyThreadSafetyMode.PublicationOnly);
public static bool IsInitialized => PushNotificationListener != null;
public static IPushNotificationListener PushNotificationListener { get; private set; }
public static void Initialize<T>(T listener) where T : IPushNotificationListener
{
if(PushNotificationListener == null)
{
PushNotificationListener = listener;
Debug.WriteLine("PushNotification plugin initialized.");
}
else
{
Debug.WriteLine("PushNotification plugin already initialized.");
}
}
public static void Initialize<T>() where T : IPushNotificationListener, new()
{
Initialize(new T());
}
public static IPushNotificationService Current
{
get
{
if(!IsInitialized)
{
throw new Exception("Not initialized.");
}
var ret = Implementation.Value;
if(ret == null)
{
throw new Exception("Not in PCL");
}
return ret;
}
}
}
}