128 lines
5.4 KiB
C#
128 lines
5.4 KiB
C#
using System;
|
|
using System.Globalization;
|
|
using Foundation;
|
|
using UIKit;
|
|
using UserNotifications;
|
|
|
|
|
|
namespace gehGassi.LocalNotifications.Platforms
|
|
{
|
|
public class LocalNotificationDelegate : UNUserNotificationCenterDelegate
|
|
{
|
|
|
|
|
|
public override void DidReceiveNotificationResponse(UNUserNotificationCenter center, UNNotificationResponse response, Action completionHandler)
|
|
{
|
|
Console.WriteLine("**-----**-----**-----**----** LocalNotifications DidReceiveNotificationResponse **-----**-----**-----**----**");
|
|
try
|
|
{
|
|
if (response is null)
|
|
return;
|
|
|
|
if (!response.IsDefaultAction)
|
|
return;
|
|
|
|
if (response.Notification.Request.Content.Badge != null)
|
|
{
|
|
UIApplication.SharedApplication.InvokeOnMainThread(() =>
|
|
{
|
|
nint appBadges = UIApplication.SharedApplication.ApplicationIconBadgeNumber - response.Notification.Request.Content.Badge.Int32Value;
|
|
UIApplication.SharedApplication.ApplicationIconBadgeNumber = appBadges;
|
|
});
|
|
}
|
|
|
|
NSDictionary dictionary = response.Notification.Request.Content.UserInfo;
|
|
IosNotification iosNotification = GetiOSNotification(dictionary);
|
|
|
|
if (iosNotification != null)
|
|
NotificationService.OnPlatformNotificationTapped(iosNotification.Payload, iosNotification.NotificationId, iosNotification.IsLocal);
|
|
|
|
completionHandler?.Invoke();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine("Error in DidReceiveNotificationResponse : " + ex.Message);
|
|
}
|
|
}
|
|
|
|
public override void WillPresentNotification(UNUserNotificationCenter center, UNNotification notification, Action<UNNotificationPresentationOptions> completionHandler)
|
|
{
|
|
Console.WriteLine("**-----**-----**-----**----** LocalNotifications WillPresentNotification **-----**-----**-----**----**");
|
|
try
|
|
{
|
|
//default value
|
|
UNNotificationPresentationOptions presentationOptions = UNNotificationPresentationOptions.None;
|
|
UNNotificationContent notificationContent = notification?.Request.Content;
|
|
|
|
if (notificationContent != null)
|
|
{
|
|
NSDictionary dictionary = notificationContent.UserInfo;
|
|
|
|
IosNotification iosNotification = GetiOSNotification(dictionary);
|
|
NotificationService.OnPlatformNotificationReceived(iosNotification.Payload, iosNotification.NotificationId, iosNotification.IsLocal);
|
|
|
|
NSNumber _presentAlert = (NSNumber)notification.Request.Content.UserInfo[NotificationService.PRESENT_ALERT];
|
|
NSNumber _playSound = (NSNumber)notification.Request.Content.UserInfo[NotificationService.PLAY_SOUND];
|
|
NSNumber _showBadge = (NSNumber)notification.Request.Content.UserInfo[NotificationService.SHOW_BADGE];
|
|
|
|
if (_presentAlert != null && _presentAlert.BoolValue)
|
|
presentationOptions |= UNNotificationPresentationOptions.Alert;
|
|
if (_playSound != null && _playSound.BoolValue)
|
|
presentationOptions |= UNNotificationPresentationOptions.Sound;
|
|
if (_showBadge != null && _showBadge.BoolValue)
|
|
presentationOptions |= UNNotificationPresentationOptions.Badge;
|
|
}
|
|
|
|
completionHandler?.Invoke(presentationOptions);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine("Error in WillPresentNotification : " + ex.Message);
|
|
}
|
|
}
|
|
|
|
private IosNotification GetiOSNotification(NSDictionary dictionary)
|
|
{
|
|
string payload = dictionary[NotificationService.PAYLOAD]?.ToString();
|
|
int? notificationId = (dictionary[NotificationService.NOTIFICATION_ID] as NSNumber)?.Int32Value;
|
|
bool isFromFirebase = false;
|
|
int? isLocalNumber = (dictionary[NotificationService.NOTIFICATION_ISLOCAL] as NSNumber)?.Int32Value;
|
|
bool isLocal = isLocalNumber.HasValue && isLocalNumber.Value == 1;
|
|
|
|
if (string.IsNullOrEmpty(payload) && !notificationId.HasValue)
|
|
{
|
|
var error = new NSError();
|
|
var jsonData = NSJsonSerialization.Serialize(dictionary, NSJsonWritingOptions.PrettyPrinted, out error);
|
|
var jsonText = new NSString(jsonData, NSStringEncoding.UTF8);
|
|
|
|
// Re-Get Payload & NotificatioinId from Firebase
|
|
payload = jsonText;
|
|
|
|
string notifId = dictionary[NotificationService.NOTIFICATION_ID_FIREBASE]?.ToString();
|
|
if (!string.IsNullOrEmpty(notifId))
|
|
{
|
|
notificationId = new NSNumber(int.Parse(notifId)).Int32Value;
|
|
isFromFirebase = true;
|
|
}
|
|
}
|
|
|
|
|
|
return new IosNotification(notificationId, payload, isLocal);
|
|
}
|
|
|
|
private class IosNotification
|
|
{
|
|
public int? NotificationId { get; }
|
|
public string Payload { get; }
|
|
public bool IsLocal { get; }
|
|
|
|
public IosNotification(int? notificationId, string payload, bool isLocal)
|
|
{
|
|
NotificationId = notificationId;
|
|
Payload = payload;
|
|
IsLocal = isLocal;
|
|
}
|
|
}
|
|
}
|
|
}
|