From 6da0d3e88daca1c69be3bcb2965611845b72d609 Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Mon, 9 Oct 2017 23:53:06 -0400 Subject: [PATCH] push for uwp --- .../PushNotificationImplementation.cs | 124 ++++++++++++++++++ src/UWP/UWP.csproj | 1 + 2 files changed, 125 insertions(+) create mode 100644 src/UWP/Services/PushNotificationImplementation.cs diff --git a/src/UWP/Services/PushNotificationImplementation.cs b/src/UWP/Services/PushNotificationImplementation.cs new file mode 100644 index 000000000..beb442089 --- /dev/null +++ b/src/UWP/Services/PushNotificationImplementation.cs @@ -0,0 +1,124 @@ +using Bit.App.Abstractions; +using Newtonsoft.Json; +using Newtonsoft.Json.Linq; +using System; +using System.Diagnostics; +using System.Threading; +using Windows.Networking.PushNotifications; +using Xamarin.Forms; + +namespace Bit.UWP.Services +{ + public class PushNotificationImplementation : IPushNotification + { + private PushNotificationChannel _channel; + private JsonSerializer serializer = new JsonSerializer() + { + ReferenceLoopHandling = ReferenceLoopHandling.Ignore + }; + + public string Token => _channel?.Uri.ToString(); + + public void Register() + { + Debug.WriteLine("Creating Push Notification Channel For Application"); + var channelTask = PushNotificationChannelManager.CreatePushNotificationChannelForApplicationAsync().AsTask(); + channelTask.Wait(); + + Debug.WriteLine("Creating Push Notification Channel For Application - Done"); + _channel = channelTask.Result; + + Debug.WriteLine("Registering call back for Push Notification Channel"); + _channel.PushNotificationReceived += Channel_PushNotificationReceived; + + CrossPushNotification.PushNotificationListener.OnRegistered(Token, Device.Windows); + } + + private void Channel_PushNotificationReceived(PushNotificationChannel sender, PushNotificationReceivedEventArgs args) + { + Debug.WriteLine("Push Notification Received " + args.NotificationType); + JObject jobject = null; + + switch(args.NotificationType) + { + case PushNotificationType.Badge: + jobject = JObject.FromObject(args.BadgeNotification, serializer); + break; + + case PushNotificationType.Raw: + jobject = JObject.FromObject(args.RawNotification, serializer); + break; + + case PushNotificationType.Tile: + jobject = JObject.FromObject(args.TileNotification, serializer); + break; + case PushNotificationType.TileFlyout: + jobject = JObject.FromObject(args.TileNotification, serializer); + break; + case PushNotificationType.Toast: + jobject = JObject.FromObject(args.ToastNotification, serializer); + break; + } + + Debug.WriteLine("Sending JObject to PushNotificationListener " + args.NotificationType); + CrossPushNotification.PushNotificationListener.OnMessage(jobject, Device.Windows); + } + + public void Unregister() + { + if(_channel != null) + { + _channel.PushNotificationReceived -= Channel_PushNotificationReceived; + _channel = null; + } + + CrossPushNotification.PushNotificationListener.OnUnregistered(Device.Windows); + } + } + + internal class CrossPushNotification + { + private static Lazy Implementation = new Lazy( + () => new PushNotificationImplementation(), + LazyThreadSafetyMode.PublicationOnly); + public static bool IsInitialized => PushNotificationListener != null; + public static IPushNotificationListener PushNotificationListener { get; private set; } + + public static void Initialize(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() where T : IPushNotificationListener, new() + { + Initialize(new T()); + } + + public static IPushNotification Current + { + get + { + if(!IsInitialized) + { + throw new Exception("Not initialized."); + } + + var ret = Implementation.Value; + if(ret == null) + { + throw new Exception("Not in PCL"); + } + + return ret; + } + } + } +} diff --git a/src/UWP/UWP.csproj b/src/UWP/UWP.csproj index 3b0678f48..6a12499ce 100644 --- a/src/UWP/UWP.csproj +++ b/src/UWP/UWP.csproj @@ -105,6 +105,7 @@ +