From 538f3dc0247e5297f899a41491f8ad3f4e8c381d Mon Sep 17 00:00:00 2001 From: Adam Meaney Date: Mon, 23 Apr 2018 11:15:14 -0500 Subject: [PATCH 1/3] Add caching to the NotificationOpened on iOS to match the Android implementation. --- .../FirebasePushNotificationManager.cs | 51 ++++++++++++++----- 1 file changed, 38 insertions(+), 13 deletions(-) diff --git a/src/Plugin.FirebasePushNotification.iOS/FirebasePushNotificationManager.cs b/src/Plugin.FirebasePushNotification.iOS/FirebasePushNotificationManager.cs index d43fbab..e568d42 100644 --- a/src/Plugin.FirebasePushNotification.iOS/FirebasePushNotificationManager.cs +++ b/src/Plugin.FirebasePushNotification.iOS/FirebasePushNotificationManager.cs @@ -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> pendingTopics = new Queue>(); static bool connected = false; @@ -75,7 +77,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 { @@ -120,33 +130,39 @@ 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"))) + + if (options?.Keys != null && options.Keys.Count() != 0 && options.ContainsKey(new NSString("UIApplicationLaunchOptionsRemoteNotificationKey"))) { NSDictionary data = options.ObjectForKey(new NSString("UIApplicationLaunchOptionsRemoteNotificationKey")) as NSDictionary; + var response = new NotificationResponse(GetParameters(data)); - // CrossFirebasePushNotification.Current.OnNotificationOpened(GetParameters(data)); + if (enableDelayedResponse && _onNotificationOpened == null) + { + delayedNotificationResponse = response; + } + + _onNotificationOpened?.Invoke(CrossFirebasePushNotification.Current, new FirebasePushNotificationResponseEventArgs(response.Data, response.Identifier, response.Type)); + } - }*/ - } - 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); @@ -335,6 +351,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); @@ -515,10 +532,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(); From 1c5f36d033f56bb5f38875d27c2de18be03c1d4c Mon Sep 17 00:00:00 2001 From: Adam Meaney Date: Mon, 23 Apr 2018 11:28:59 -0500 Subject: [PATCH 2/3] Add caching to token refresh. --- .../FirebasePushNotificationManager.cs | 10 ++++++++++ .../FirebasePushNotificationManager.cs | 10 ++++++++++ 2 files changed, 20 insertions(+) diff --git a/src/Plugin.FirebasePushNotification.Android/FirebasePushNotificationManager.cs b/src/Plugin.FirebasePushNotification.Android/FirebasePushNotificationManager.cs index 748c207..22ea9d7 100644 --- a/src/Plugin.FirebasePushNotification.Android/FirebasePushNotificationManager.cs +++ b/src/Plugin.FirebasePushNotification.Android/FirebasePushNotificationManager.cs @@ -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 { diff --git a/src/Plugin.FirebasePushNotification.iOS/FirebasePushNotificationManager.cs b/src/Plugin.FirebasePushNotification.iOS/FirebasePushNotificationManager.cs index e568d42..f2d1cc9 100644 --- a/src/Plugin.FirebasePushNotification.iOS/FirebasePushNotificationManager.cs +++ b/src/Plugin.FirebasePushNotification.iOS/FirebasePushNotificationManager.cs @@ -38,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 { From 95b82444161aa3e032460c250ff3d20b2548b66f Mon Sep 17 00:00:00 2001 From: Adam Meaney Date: Mon, 23 Apr 2018 12:17:20 -0500 Subject: [PATCH 3/3] Removed unnecessary code added for my own testing. --- .../FirebasePushNotificationManager.cs | 16 +--------------- 1 file changed, 1 insertion(+), 15 deletions(-) diff --git a/src/Plugin.FirebasePushNotification.iOS/FirebasePushNotificationManager.cs b/src/Plugin.FirebasePushNotification.iOS/FirebasePushNotificationManager.cs index f2d1cc9..70cc6b6 100644 --- a/src/Plugin.FirebasePushNotification.iOS/FirebasePushNotificationManager.cs +++ b/src/Plugin.FirebasePushNotification.iOS/FirebasePushNotificationManager.cs @@ -142,7 +142,7 @@ public string[] SubscribedTopics{ public static async Task Initialize(NSDictionary options, bool autoRegistration = true, bool enableDelayedResponse = true) { - enableDelayedResponse = enableDelayedResponse; + EnableDelayedResponse = enableDelayedResponse; App.Configure(); CrossFirebasePushNotification.Current.NotificationHandler = CrossFirebasePushNotification.Current.NotificationHandler ?? new DefaultPushNotificationHandler(); @@ -152,20 +152,6 @@ public static async Task Initialize(NSDictionary options, bool autoRegistration await CrossFirebasePushNotification.Current.RegisterForPushNotifications(); } - - if (options?.Keys != null && options.Keys.Count() != 0 && options.ContainsKey(new NSString("UIApplicationLaunchOptionsRemoteNotificationKey"))) - { - NSDictionary data = options.ObjectForKey(new NSString("UIApplicationLaunchOptionsRemoteNotificationKey")) as NSDictionary; - var response = new NotificationResponse(GetParameters(data)); - - if (enableDelayedResponse && _onNotificationOpened == null) - { - delayedNotificationResponse = response; - } - - _onNotificationOpened?.Invoke(CrossFirebasePushNotification.Current, new FirebasePushNotificationResponseEventArgs(response.Data, response.Identifier, response.Type)); - } - } public static async void Initialize(NSDictionary options, IPushNotificationHandler pushNotificationHandler, bool autoRegistration = true, bool enableDelayedResponse = true) {