From 851e2d5ad420816bbcbc8a3e239921a7eb150447 Mon Sep 17 00:00:00 2001 From: a-maurice Date: Tue, 3 Sep 2024 15:02:20 -0700 Subject: [PATCH 1/3] Add null checks, and improve swig error handling --- messaging/src/FirebaseNotification.cs | 4 ++++ messaging/src/swig/messaging.i | 6 +++++- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/messaging/src/FirebaseNotification.cs b/messaging/src/FirebaseNotification.cs index 647ab6740..ca0ab1640 100644 --- a/messaging/src/FirebaseNotification.cs +++ b/messaging/src/FirebaseNotification.cs @@ -22,6 +22,8 @@ namespace Firebase.Messaging { /// implementation. public sealed class AndroidNotificationParams { internal static AndroidNotificationParams FromInternal(AndroidNotificationParamsInternal other) { + if (other == null) return null; + AndroidNotificationParams android = new AndroidNotificationParams(); android.ChannelId = other.channel_id; return android; @@ -45,6 +47,8 @@ public void Dispose(bool disposing) { } /// library. public sealed class FirebaseNotification { internal static FirebaseNotification FromInternal(FirebaseNotificationInternal other) { + if (other == null) return null; + FirebaseNotification notification = new FirebaseNotification(); notification.Android = AndroidNotificationParams.FromInternal(other.android); notification.Badge = other.badge; diff --git a/messaging/src/swig/messaging.i b/messaging/src/swig/messaging.i index 1bf5a488b..75ff2e48d 100644 --- a/messaging/src/swig/messaging.i +++ b/messaging/src/swig/messaging.i @@ -316,18 +316,22 @@ void SendPendingEvents() { // messageReceivedDelegate. [MonoPInvokeCallback(typeof(MessageReceivedDelegate))] private static int MessageReceivedDelegateMethod(System.IntPtr message) { + int tookOwnership = 0; return ExceptionAggregator.Wrap(() => { // Use a local copy so another thread cannot unset this before we use it. var handler = FirebaseMessagingInternal.MessageReceivedInternal; if (handler != null) { + // Take ownership, and track it so that the caller of this knows, even + // if an exception is thrown, since the C# object will still delete it. FirebaseMessageInternal messageInternal = new FirebaseMessageInternal(message, true); + tookOwnership = 1; handler(null, new Firebase.Messaging.MessageReceivedEventArgs( FirebaseMessage.FromInternal(messageInternal))); messageInternal.Dispose(); return 1; } return 0; - }, 0); + }, tookOwnership); } // Called from ListenerImpl::TokenReceived() via the From 900a0ffb4a40733f6aa66ef07475f5821c1e1403 Mon Sep 17 00:00:00 2001 From: a-maurice Date: Tue, 3 Sep 2024 16:26:25 -0700 Subject: [PATCH 2/3] Update readme.md --- docs/readme.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/docs/readme.md b/docs/readme.md index 0f3d17361..d306c5656 100644 --- a/docs/readme.md +++ b/docs/readme.md @@ -71,6 +71,11 @@ Support Release Notes ------------- +### Upcoming +- Changes + - Messaging: Fixed a crash when opening a push notification. + ([#1091](https://github.com/firebase/firebase-unity-sdk/issues/1091)). + ### 12.2.0 - Changes - General: Update to Firebase C++ SDK version 12.2.0. From 4ab2565d6448128c384ccecc3cca975a3590824c Mon Sep 17 00:00:00 2001 From: a-maurice Date: Tue, 3 Sep 2024 17:00:48 -0700 Subject: [PATCH 3/3] Use a bool instead of an int for ownership --- messaging/src/swig/messaging.i | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/messaging/src/swig/messaging.i b/messaging/src/swig/messaging.i index 75ff2e48d..c84853c2c 100644 --- a/messaging/src/swig/messaging.i +++ b/messaging/src/swig/messaging.i @@ -69,7 +69,7 @@ public: // OnMessage() callback in this class. SWIGSTDCALL is used here as C# // delegates *must* be called using the stdcall calling convention rather // than whatever the compiler defines. - typedef int (SWIGSTDCALL *MessageReceivedCallback)(void* message); + typedef bool (SWIGSTDCALL *MessageReceivedCallback)(void* message); // Function which is used to reference a C# delegate which is called from // OnTokenReceived() callback in this class. SWIGSTDCALL is used here as C# // delegates *must* be called using the stdcall calling convention rather @@ -250,7 +250,7 @@ void SendPendingEvents() { // Listener.TokenReceivedDelegateMethod respectively. internal class Listener : System.IDisposable { // Delegate called from ListenerImpl::MessageReceivedCallback(). - internal delegate int MessageReceivedDelegate(System.IntPtr message); + internal delegate bool MessageReceivedDelegate(System.IntPtr message); // Delegate called from ListenerImpl::TokenReceivedCallback(). internal delegate void TokenReceivedDelegate(string token); @@ -315,8 +315,8 @@ void SendPendingEvents() { // Called from ListenerImpl::MessageReceived() via the // messageReceivedDelegate. [MonoPInvokeCallback(typeof(MessageReceivedDelegate))] - private static int MessageReceivedDelegateMethod(System.IntPtr message) { - int tookOwnership = 0; + private static bool MessageReceivedDelegateMethod(System.IntPtr message) { + bool tookOwnership = false; return ExceptionAggregator.Wrap(() => { // Use a local copy so another thread cannot unset this before we use it. var handler = FirebaseMessagingInternal.MessageReceivedInternal; @@ -324,13 +324,13 @@ void SendPendingEvents() { // Take ownership, and track it so that the caller of this knows, even // if an exception is thrown, since the C# object will still delete it. FirebaseMessageInternal messageInternal = new FirebaseMessageInternal(message, true); - tookOwnership = 1; + tookOwnership = true; handler(null, new Firebase.Messaging.MessageReceivedEventArgs( FirebaseMessage.FromInternal(messageInternal))); messageInternal.Dispose(); - return 1; + return true; } - return 0; + return false; }, tookOwnership); }