mirror of
https://github.com/bitwarden/android.git
synced 2024-12-25 02:18:27 +03:00
simplify push in UWP as well
This commit is contained in:
parent
bcf49ab396
commit
b651becf66
2 changed files with 18 additions and 61 deletions
|
@ -140,12 +140,10 @@ namespace Bit.UWP
|
||||||
container.RegisterSingleton(Plugin.Settings.CrossSettings.Current);
|
container.RegisterSingleton(Plugin.Settings.CrossSettings.Current);
|
||||||
|
|
||||||
// Push
|
// Push
|
||||||
var pushListener = new PushNotificationListener();
|
container.RegisterSingleton<IPushNotificationListener, PushNotificationListener>();
|
||||||
container.RegisterSingleton<IPushNotificationListener>(pushListener);
|
container.RegisterSingleton<IPushNotificationService, UwpPushNotificationService>();
|
||||||
CrossPushNotification.Initialize(pushListener);
|
|
||||||
container.RegisterSingleton(CrossPushNotification.Current);
|
|
||||||
CachedImageRenderer.Init();
|
|
||||||
|
|
||||||
|
CachedImageRenderer.Init();
|
||||||
Resolver.SetResolver(new SimpleInjectorResolver(container));
|
Resolver.SetResolver(new SimpleInjectorResolver(container));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,7 +3,6 @@ using Newtonsoft.Json;
|
||||||
using Newtonsoft.Json.Linq;
|
using Newtonsoft.Json.Linq;
|
||||||
using System;
|
using System;
|
||||||
using System.Diagnostics;
|
using System.Diagnostics;
|
||||||
using System.Threading;
|
|
||||||
using Windows.Networking.PushNotifications;
|
using Windows.Networking.PushNotifications;
|
||||||
using Xamarin.Forms;
|
using Xamarin.Forms;
|
||||||
|
|
||||||
|
@ -12,10 +11,16 @@ namespace Bit.UWP.Services
|
||||||
public class UwpPushNotificationService : IPushNotificationService
|
public class UwpPushNotificationService : IPushNotificationService
|
||||||
{
|
{
|
||||||
private PushNotificationChannel _channel;
|
private PushNotificationChannel _channel;
|
||||||
private JsonSerializer serializer = new JsonSerializer()
|
private JsonSerializer _serializer = new JsonSerializer
|
||||||
{
|
{
|
||||||
ReferenceLoopHandling = ReferenceLoopHandling.Ignore
|
ReferenceLoopHandling = ReferenceLoopHandling.Ignore
|
||||||
};
|
};
|
||||||
|
private readonly IPushNotificationListener _pushNotificationListener;
|
||||||
|
|
||||||
|
public UwpPushNotificationService(IPushNotificationListener pushNotificationListener)
|
||||||
|
{
|
||||||
|
_pushNotificationListener = pushNotificationListener;
|
||||||
|
}
|
||||||
|
|
||||||
public string Token => _channel?.Uri.ToString();
|
public string Token => _channel?.Uri.ToString();
|
||||||
|
|
||||||
|
@ -31,7 +36,7 @@ namespace Bit.UWP.Services
|
||||||
Debug.WriteLine("Registering call back for Push Notification Channel");
|
Debug.WriteLine("Registering call back for Push Notification Channel");
|
||||||
_channel.PushNotificationReceived += Channel_PushNotificationReceived;
|
_channel.PushNotificationReceived += Channel_PushNotificationReceived;
|
||||||
|
|
||||||
CrossPushNotification.PushNotificationListener.OnRegistered(Token, Device.UWP);
|
_pushNotificationListener.OnRegistered(Token, Device.UWP);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void Channel_PushNotificationReceived(PushNotificationChannel sender, PushNotificationReceivedEventArgs args)
|
private void Channel_PushNotificationReceived(PushNotificationChannel sender, PushNotificationReceivedEventArgs args)
|
||||||
|
@ -42,24 +47,24 @@ namespace Bit.UWP.Services
|
||||||
switch(args.NotificationType)
|
switch(args.NotificationType)
|
||||||
{
|
{
|
||||||
case PushNotificationType.Badge:
|
case PushNotificationType.Badge:
|
||||||
jobject = JObject.FromObject(args.BadgeNotification, serializer);
|
jobject = JObject.FromObject(args.BadgeNotification, _serializer);
|
||||||
break;
|
break;
|
||||||
case PushNotificationType.Raw:
|
case PushNotificationType.Raw:
|
||||||
jobject = JObject.FromObject(args.RawNotification, serializer);
|
jobject = JObject.FromObject(args.RawNotification, _serializer);
|
||||||
break;
|
break;
|
||||||
case PushNotificationType.Tile:
|
case PushNotificationType.Tile:
|
||||||
jobject = JObject.FromObject(args.TileNotification, serializer);
|
jobject = JObject.FromObject(args.TileNotification, _serializer);
|
||||||
break;
|
break;
|
||||||
case PushNotificationType.TileFlyout:
|
case PushNotificationType.TileFlyout:
|
||||||
jobject = JObject.FromObject(args.TileNotification, serializer);
|
jobject = JObject.FromObject(args.TileNotification, _serializer);
|
||||||
break;
|
break;
|
||||||
case PushNotificationType.Toast:
|
case PushNotificationType.Toast:
|
||||||
jobject = JObject.FromObject(args.ToastNotification, serializer);
|
jobject = JObject.FromObject(args.ToastNotification, _serializer);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
Debug.WriteLine("Sending JObject to PushNotificationListener " + args.NotificationType);
|
Debug.WriteLine("Sending JObject to PushNotificationListener " + args.NotificationType);
|
||||||
CrossPushNotification.PushNotificationListener.OnMessage(jobject, Device.UWP);
|
_pushNotificationListener.OnMessage(jobject, Device.UWP);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Unregister()
|
public void Unregister()
|
||||||
|
@ -70,53 +75,7 @@ namespace Bit.UWP.Services
|
||||||
_channel = null;
|
_channel = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
CrossPushNotification.PushNotificationListener.OnUnregistered(Device.UWP);
|
_pushNotificationListener.OnUnregistered(Device.UWP);
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
internal class CrossPushNotification
|
|
||||||
{
|
|
||||||
private static Lazy<IPushNotificationService> Implementation = new Lazy<IPushNotificationService>(
|
|
||||||
() => new UwpPushNotificationService(),
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue