Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cache events #133

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,17 @@ public event FirebasePushNotificationTokenEventHandler OnTokenRefresh
{
add
{
var previous = _onTokenRefresh;
_onTokenRefresh += value;

if (previous == null)
{
var token = Token;
if (!string.IsNullOrEmpty(token))
{
_onTokenRefresh?.Invoke(this, new FirebasePushNotificationTokenEventArgs(Token));
}
}
}
remove
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ namespace Plugin.FirebasePushNotification
public class FirebasePushNotificationManager : NSObject, IFirebasePushNotification, IUNUserNotificationCenterDelegate, IMessagingDelegate
{
public static UNNotificationPresentationOptions CurrentNotificationPresentationOption { get; set; } = UNNotificationPresentationOptions.None;
private static bool EnableDelayedResponse;
static NotificationResponse delayedNotificationResponse = null;
static NSObject messagingConnectionChangeNotificationToken;
static Queue<Tuple<string, bool>> pendingTopics = new Queue<Tuple<string, bool>>();
static bool connected = false;
Expand All @@ -36,7 +38,17 @@ public event FirebasePushNotificationTokenEventHandler OnTokenRefresh
{
add
{
var previous = _onTokenRefresh;
_onTokenRefresh += value;

if (EnableDelayedResponse && previous == null)
{
var token = Token;
if (!string.IsNullOrEmpty(token))
{
_onTokenRefresh?.Invoke(this, new FirebasePushNotificationTokenEventArgs(Token));
}
}
}
remove
{
Expand Down Expand Up @@ -75,7 +87,15 @@ public event FirebasePushNotificationResponseEventHandler OnNotificationOpened
{
add
{
var previousVal = _onNotificationOpened;
_onNotificationOpened += value;
if (delayedNotificationResponse != null && previousVal == null)
{
var tmpParams = delayedNotificationResponse;
_onNotificationOpened?.Invoke(CrossFirebasePushNotification.Current, new FirebasePushNotificationResponseEventArgs(tmpParams.Data, tmpParams.Identifier, tmpParams.Type));
delayedNotificationResponse = null;
}

}
remove
{
Expand Down Expand Up @@ -120,33 +140,25 @@ public string[] SubscribedTopics{

public IPushNotificationHandler NotificationHandler { get; set; }

public static async Task Initialize(NSDictionary options, bool autoRegistration = true)
public static async Task Initialize(NSDictionary options, bool autoRegistration = true, bool enableDelayedResponse = true)
{
EnableDelayedResponse = enableDelayedResponse;
App.Configure();

CrossFirebasePushNotification.Current.NotificationHandler = CrossFirebasePushNotification.Current.NotificationHandler ?? new DefaultPushNotificationHandler();

if(autoRegistration)
if (autoRegistration)
{
await CrossFirebasePushNotification.Current.RegisterForPushNotifications();
}


/*if (options != null && options.Keys != null && options.Keys.Count() != 0 && options.ContainsKey(new NSString("UIApplicationLaunchOptionsRemoteNotificationKey")))
{
NSDictionary data = options.ObjectForKey(new NSString("UIApplicationLaunchOptionsRemoteNotificationKey")) as NSDictionary;

// CrossFirebasePushNotification.Current.OnNotificationOpened(GetParameters(data));

}*/

}
public static async void Initialize(NSDictionary options, IPushNotificationHandler pushNotificationHandler, bool autoRegistration = true)
public static async void Initialize(NSDictionary options, IPushNotificationHandler pushNotificationHandler, bool autoRegistration = true, bool enableDelayedResponse = true)
{
CrossFirebasePushNotification.Current.NotificationHandler = pushNotificationHandler;
await Initialize(options, autoRegistration);
}
public static async void Initialize(NSDictionary options,NotificationUserCategory[] notificationUserCategories,bool autoRegistration = true)
public static async void Initialize(NSDictionary options,NotificationUserCategory[] notificationUserCategories,bool autoRegistration = true, bool enableDelayedResponse = true)
{

await Initialize(options, autoRegistration);
Expand Down Expand Up @@ -335,6 +347,7 @@ public void WillPresentNotification(UNUserNotificationCenter center, UNNotificat
System.Diagnostics.Debug.WriteLine("WillPresentNotification");
var parameters = GetParameters(notification.Request.Content.UserInfo);
_onNotificationReceived?.Invoke(CrossFirebasePushNotification.Current, new FirebasePushNotificationDataEventArgs(parameters));

CrossFirebasePushNotification.Current.NotificationHandler?.OnReceived(parameters);

completionHandler(CurrentNotificationPresentationOption);
Expand Down Expand Up @@ -515,10 +528,18 @@ public void DidReceiveNotificationResponse(UNUserNotificationCenter center, UNNo
else if (response.IsDismissAction)
catType = NotificationCategoryType.Dismiss;

var notificationResponse = new NotificationResponse(parameters, $"{response.ActionIdentifier}".Equals("com.apple.UNNotificationDefaultActionIdentifier",StringComparison.CurrentCultureIgnoreCase)?string.Empty:$"{response.ActionIdentifier}",catType);
_onNotificationOpened?.Invoke(this,new FirebasePushNotificationResponseEventArgs(notificationResponse.Data,notificationResponse.Identifier,notificationResponse.Type));

CrossFirebasePushNotification.Current.NotificationHandler?.OnOpened(notificationResponse);
var notificationResponse = new NotificationResponse(parameters, $"{response.ActionIdentifier}".Equals("com.apple.UNNotificationDefaultActionIdentifier", StringComparison.CurrentCultureIgnoreCase)?string.Empty:$"{response.ActionIdentifier}",catType);

if (EnableDelayedResponse && _onNotificationOpened == null)
{
delayedNotificationResponse = notificationResponse;
}
else
{
_onNotificationOpened?.Invoke(this, new FirebasePushNotificationResponseEventArgs(notificationResponse.Data, notificationResponse.Identifier, notificationResponse.Type));

CrossFirebasePushNotification.Current.NotificationHandler?.OnOpened(notificationResponse);
}

// Inform caller it has been handled
completionHandler();
Expand Down