Skip to content

Commit 512e201

Browse files
authored
Add null checks, and improve swig error handling (#1101)
* Add null checks, and improve swig error handling * Update readme.md * Use a bool instead of an int for ownership
1 parent c11a478 commit 512e201

File tree

3 files changed

+19
-6
lines changed

3 files changed

+19
-6
lines changed

docs/readme.md

+5
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,11 @@ Support
7171

7272
Release Notes
7373
-------------
74+
### Upcoming
75+
- Changes
76+
- Messaging: Fixed a crash when opening a push notification.
77+
([#1091](https://github.com/firebase/firebase-unity-sdk/issues/1091)).
78+
7479
### 12.2.0
7580
- Changes
7681
- General: Update to Firebase C++ SDK version 12.2.0.

messaging/src/FirebaseNotification.cs

+4
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ namespace Firebase.Messaging {
2222
/// implementation.
2323
public sealed class AndroidNotificationParams {
2424
internal static AndroidNotificationParams FromInternal(AndroidNotificationParamsInternal other) {
25+
if (other == null) return null;
26+
2527
AndroidNotificationParams android = new AndroidNotificationParams();
2628
android.ChannelId = other.channel_id;
2729
return android;
@@ -45,6 +47,8 @@ public void Dispose(bool disposing) { }
4547
/// library.
4648
public sealed class FirebaseNotification {
4749
internal static FirebaseNotification FromInternal(FirebaseNotificationInternal other) {
50+
if (other == null) return null;
51+
4852
FirebaseNotification notification = new FirebaseNotification();
4953
notification.Android = AndroidNotificationParams.FromInternal(other.android);
5054
notification.Badge = other.badge;

messaging/src/swig/messaging.i

+10-6
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public:
6969
// OnMessage() callback in this class. SWIGSTDCALL is used here as C#
7070
// delegates *must* be called using the stdcall calling convention rather
7171
// than whatever the compiler defines.
72-
typedef int (SWIGSTDCALL *MessageReceivedCallback)(void* message);
72+
typedef bool (SWIGSTDCALL *MessageReceivedCallback)(void* message);
7373
// Function which is used to reference a C# delegate which is called from
7474
// OnTokenReceived() callback in this class. SWIGSTDCALL is used here as C#
7575
// delegates *must* be called using the stdcall calling convention rather
@@ -250,7 +250,7 @@ void SendPendingEvents() {
250250
// Listener.TokenReceivedDelegateMethod respectively.
251251
internal class Listener : System.IDisposable {
252252
// Delegate called from ListenerImpl::MessageReceivedCallback().
253-
internal delegate int MessageReceivedDelegate(System.IntPtr message);
253+
internal delegate bool MessageReceivedDelegate(System.IntPtr message);
254254
// Delegate called from ListenerImpl::TokenReceivedCallback().
255255
internal delegate void TokenReceivedDelegate(string token);
256256

@@ -315,19 +315,23 @@ void SendPendingEvents() {
315315
// Called from ListenerImpl::MessageReceived() via the
316316
// messageReceivedDelegate.
317317
[MonoPInvokeCallback(typeof(MessageReceivedDelegate))]
318-
private static int MessageReceivedDelegateMethod(System.IntPtr message) {
318+
private static bool MessageReceivedDelegateMethod(System.IntPtr message) {
319+
bool tookOwnership = false;
319320
return ExceptionAggregator.Wrap(() => {
320321
// Use a local copy so another thread cannot unset this before we use it.
321322
var handler = FirebaseMessagingInternal.MessageReceivedInternal;
322323
if (handler != null) {
324+
// Take ownership, and track it so that the caller of this knows, even
325+
// if an exception is thrown, since the C# object will still delete it.
323326
FirebaseMessageInternal messageInternal = new FirebaseMessageInternal(message, true);
327+
tookOwnership = true;
324328
handler(null, new Firebase.Messaging.MessageReceivedEventArgs(
325329
FirebaseMessage.FromInternal(messageInternal)));
326330
messageInternal.Dispose();
327-
return 1;
331+
return true;
328332
}
329-
return 0;
330-
}, 0);
333+
return false;
334+
}, tookOwnership);
331335
}
332336

333337
// Called from ListenerImpl::TokenReceived() via the

0 commit comments

Comments
 (0)