mirror of
https://github.com/bitwarden/mobile
synced 2026-01-06 02:23:57 +00:00
local push notification implementation from lib
This commit is contained in:
@@ -1,8 +1,5 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using XLabs.Ioc;
|
||||
|
||||
using Foundation;
|
||||
using UIKit;
|
||||
using Bit.App.Abstractions;
|
||||
@@ -16,7 +13,6 @@ using Plugin.Settings.Abstractions;
|
||||
using System.Diagnostics;
|
||||
using Xamarin.Forms;
|
||||
using Bit.iOS.Core.Services;
|
||||
using PushNotification.Plugin;
|
||||
using Plugin.Connectivity.Abstractions;
|
||||
using Bit.App.Pages;
|
||||
using HockeyApp.iOS;
|
||||
@@ -81,14 +77,16 @@ namespace Bit.iOS
|
||||
UINavigationBar.Appearance.ShadowImage = new UIImage();
|
||||
UINavigationBar.Appearance.SetBackgroundImage(new UIImage(), UIBarMetrics.Default);
|
||||
UIBarButtonItem.AppearanceWhenContainedIn(new Type[] { typeof(UISearchBar) }).TintColor = primaryColor;
|
||||
UIButton.AppearanceWhenContainedIn(new Type[] { typeof(UISearchBar) }).SetTitleColor(primaryColor, UIControlState.Normal);
|
||||
UIButton.AppearanceWhenContainedIn(new Type[] { typeof(UISearchBar) }).SetTitleColor(primaryColor,
|
||||
UIControlState.Normal);
|
||||
UIButton.AppearanceWhenContainedIn(new Type[] { typeof(UISearchBar) }).TintColor = primaryColor;
|
||||
UIStepper.Appearance.TintColor = grayLight;
|
||||
UISlider.Appearance.TintColor = primaryColor;
|
||||
|
||||
MessagingCenter.Subscribe<Xamarin.Forms.Application, ToolsExtensionPage>(Xamarin.Forms.Application.Current, "ShowAppExtension", (sender, page) =>
|
||||
MessagingCenter.Subscribe<Xamarin.Forms.Application, ToolsExtensionPage>(
|
||||
Xamarin.Forms.Application.Current, "ShowAppExtension", (sender, page) =>
|
||||
{
|
||||
var itemProvider = new NSItemProvider(new NSDictionary(), iOS.Core.Constants.UTTypeAppExtensionSetup);
|
||||
var itemProvider = new NSItemProvider(new NSDictionary(), Core.Constants.UTTypeAppExtensionSetup);
|
||||
var extensionItem = new NSExtensionItem();
|
||||
extensionItem.Attachments = new NSItemProvider[] { itemProvider };
|
||||
var activityViewController = new UIActivityViewController(new NSExtensionItem[] { extensionItem }, null);
|
||||
@@ -112,7 +110,8 @@ namespace Bit.iOS
|
||||
UIApplication.SharedApplication.StatusBarHidden = false;
|
||||
UIApplication.SharedApplication.StatusBarStyle = UIStatusBarStyle.LightContent;
|
||||
|
||||
MessagingCenter.Subscribe<Xamarin.Forms.Application, bool>(Xamarin.Forms.Application.Current, "ShowStatusBar", (sender, show) =>
|
||||
MessagingCenter.Subscribe<Xamarin.Forms.Application, bool>(Xamarin.Forms.Application.Current,
|
||||
"ShowStatusBar", (sender, show) =>
|
||||
{
|
||||
UIApplication.SharedApplication.SetStatusBarHidden(!show, false);
|
||||
});
|
||||
@@ -196,7 +195,8 @@ namespace Bit.iOS
|
||||
Debug.WriteLine("WillEnterForeground");
|
||||
}
|
||||
|
||||
public override bool OpenUrl(UIApplication application, NSUrl url, string sourceApplication, NSObject annotation)
|
||||
public override bool OpenUrl(UIApplication application, NSUrl url, string sourceApplication,
|
||||
NSObject annotation)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
@@ -217,12 +217,14 @@ namespace Bit.iOS
|
||||
}
|
||||
}
|
||||
|
||||
public override void DidRegisterUserNotificationSettings(UIApplication application, UIUserNotificationSettings notificationSettings)
|
||||
public override void DidRegisterUserNotificationSettings(UIApplication application,
|
||||
UIUserNotificationSettings notificationSettings)
|
||||
{
|
||||
application.RegisterForRemoteNotifications();
|
||||
}
|
||||
|
||||
public override void DidReceiveRemoteNotification(UIApplication application, NSDictionary userInfo, Action<UIBackgroundFetchResult> completionHandler)
|
||||
public override void DidReceiveRemoteNotification(UIApplication application, NSDictionary userInfo,
|
||||
Action<UIBackgroundFetchResult> completionHandler)
|
||||
{
|
||||
if(CrossPushNotification.Current is IPushNotificationHandler)
|
||||
{
|
||||
|
||||
152
src/iOS/Services/PushNotificationImplementation.cs
Normal file
152
src/iOS/Services/PushNotificationImplementation.cs
Normal file
@@ -0,0 +1,152 @@
|
||||
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 Xamarin.Forms;
|
||||
|
||||
namespace Bit.iOS.Services
|
||||
{
|
||||
public class PushNotificationImplementation : IPushNotification, IPushNotificationHandler
|
||||
{
|
||||
public string Token => NSUserDefaults.StandardUserDefaults.StringForKey(PushNotificationKey.Token);
|
||||
|
||||
public void Register()
|
||||
{
|
||||
var userNotificationTypes = UIUserNotificationType.Alert | UIUserNotificationType.Badge |
|
||||
UIUserNotificationType.Sound;
|
||||
var settings = UIUserNotificationSettings.GetSettingsForTypes(userNotificationTypes, null);
|
||||
UIApplication.SharedApplication.RegisterUserNotificationSettings(settings);
|
||||
}
|
||||
|
||||
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.", PushNotificationKey.DomainName);
|
||||
CrossPushNotification.PushNotificationListener.OnError(error.LocalizedDescription, Device.iOS);
|
||||
}
|
||||
|
||||
public void OnRegisteredSuccess(NSData token)
|
||||
{
|
||||
Debug.WriteLine("{0} - Succesfully Registered.", PushNotificationKey.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}", PushNotificationKey.DomainName, trimmedDeviceToken);
|
||||
CrossPushNotification.PushNotificationListener.OnRegistered(trimmedDeviceToken, Device.iOS);
|
||||
NSUserDefaults.StandardUserDefaults.SetString(trimmedDeviceToken, PushNotificationKey.Token);
|
||||
NSUserDefaults.StandardUserDefaults.Synchronize();
|
||||
}
|
||||
|
||||
public void OnUnregisteredSuccess()
|
||||
{
|
||||
CrossPushNotification.PushNotificationListener.OnUnregistered(Device.iOS);
|
||||
NSUserDefaults.StandardUserDefaults.SetString(string.Empty, PushNotificationKey.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<IPushNotification> Implementation = new Lazy<IPushNotification>(
|
||||
() => new PushNotificationImplementation(),
|
||||
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 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -238,6 +238,7 @@
|
||||
<Compile Include="Services\DeviceActionService.cs" />
|
||||
<Compile Include="Main.cs" />
|
||||
<Compile Include="AppDelegate.cs" />
|
||||
<Compile Include="Services\PushNotificationImplementation.cs" />
|
||||
<Compile Include="Services\ReflectionService.cs" />
|
||||
<None Include="app.config">
|
||||
<SubType>Designer</SubType>
|
||||
@@ -324,14 +325,6 @@
|
||||
<Reference Include="Plugin.Settings.Abstractions, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL">
|
||||
<HintPath>..\..\packages\Xam.Plugins.Settings.3.0.1\lib\Xamarin.iOS10\Plugin.Settings.Abstractions.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="PushNotification.Plugin, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL">
|
||||
<HintPath>..\..\packages\Xam.Plugin.PushNotification.1.2.4\lib\Xamarin.iOS10\PushNotification.Plugin.dll</HintPath>
|
||||
<Private>True</Private>
|
||||
</Reference>
|
||||
<Reference Include="PushNotification.Plugin.Abstractions, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL">
|
||||
<HintPath>..\..\packages\Xam.Plugin.PushNotification.1.2.4\lib\Xamarin.iOS10\PushNotification.Plugin.Abstractions.dll</HintPath>
|
||||
<Private>True</Private>
|
||||
</Reference>
|
||||
<Reference Include="SimpleInjector, Version=4.0.8.0, Culture=neutral, PublicKeyToken=984cb50dea722e99, processorArchitecture=MSIL">
|
||||
<HintPath>..\..\packages\SimpleInjector.4.0.8\lib\netstandard1.3\SimpleInjector.dll</HintPath>
|
||||
</Reference>
|
||||
|
||||
@@ -67,7 +67,6 @@
|
||||
<package id="Validation" version="2.3.7" targetFramework="xamarinios10" />
|
||||
<package id="WebP.Touch" version="1.0.3" targetFramework="xamarinios10" />
|
||||
<package id="Xam.Plugin.Connectivity" version="3.0.2" targetFramework="xamarinios10" />
|
||||
<package id="Xam.Plugin.PushNotification" version="1.2.4" targetFramework="xamarinios10" developmentDependency="true" />
|
||||
<package id="Xam.Plugins.Settings" version="3.0.1" targetFramework="xamarinios10" />
|
||||
<package id="Xamarin.Build.Download" version="0.4.7" targetFramework="xamarinios10" />
|
||||
<package id="Xamarin.FFImageLoading" version="2.2.9" targetFramework="xamarinios10" />
|
||||
|
||||
Reference in New Issue
Block a user