2019-05-28 19:03:41 +03:00
|
|
|
|
using Bit.App.Abstractions;
|
|
|
|
|
using Foundation;
|
2019-05-28 19:01:55 +03:00
|
|
|
|
using Newtonsoft.Json.Linq;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
using Xamarin.Forms;
|
|
|
|
|
|
|
|
|
|
namespace Bit.iOS.Services
|
|
|
|
|
{
|
|
|
|
|
public class iOSPushNotificationHandler
|
|
|
|
|
{
|
|
|
|
|
private const string TokenSetting = "token";
|
|
|
|
|
private const string DomainName = "iOSPushNotificationService";
|
|
|
|
|
|
2019-05-28 19:03:41 +03:00
|
|
|
|
private readonly IPushNotificationListenerService _pushNotificationListenerService;
|
2019-05-28 19:01:55 +03:00
|
|
|
|
|
|
|
|
|
public iOSPushNotificationHandler(
|
2019-05-28 19:03:41 +03:00
|
|
|
|
IPushNotificationListenerService pushNotificationListenerService)
|
2019-05-28 19:01:55 +03:00
|
|
|
|
{
|
2019-05-28 19:03:41 +03:00
|
|
|
|
_pushNotificationListenerService = pushNotificationListenerService;
|
2019-05-28 19:01:55 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void OnMessageReceived(NSDictionary userInfo)
|
|
|
|
|
{
|
|
|
|
|
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());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-05-28 19:03:41 +03:00
|
|
|
|
_pushNotificationListenerService.OnMessageAsync(values, Device.iOS);
|
2019-05-28 19:01:55 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void OnErrorReceived(NSError error)
|
|
|
|
|
{
|
|
|
|
|
Debug.WriteLine("{0} - Registration Failed.", DomainName);
|
2019-05-28 19:03:41 +03:00
|
|
|
|
_pushNotificationListenerService.OnError(error.LocalizedDescription, Device.iOS);
|
2019-05-28 19:01:55 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void OnRegisteredSuccess(NSData token)
|
|
|
|
|
{
|
|
|
|
|
Debug.WriteLine("{0} - Successfully Registered.", DomainName);
|
|
|
|
|
var trimmedDeviceToken = token.Description;
|
|
|
|
|
if(!string.IsNullOrWhiteSpace(trimmedDeviceToken))
|
|
|
|
|
{
|
|
|
|
|
trimmedDeviceToken = trimmedDeviceToken.Trim('<').Trim('>').Trim().Replace(" ", string.Empty);
|
|
|
|
|
}
|
|
|
|
|
Console.WriteLine("{0} - Token: {1}", DomainName, trimmedDeviceToken);
|
2019-05-28 19:03:41 +03:00
|
|
|
|
_pushNotificationListenerService.OnRegisteredAsync(trimmedDeviceToken, Device.iOS);
|
2019-05-28 19:01:55 +03:00
|
|
|
|
NSUserDefaults.StandardUserDefaults.SetString(trimmedDeviceToken, 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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|