Skip to content

Add null checks, and improve swig error handling #1101

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

Merged
merged 3 commits into from
Sep 4, 2024
Merged
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
5 changes: 5 additions & 0 deletions docs/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 4 additions & 0 deletions messaging/src/FirebaseNotification.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down
16 changes: 10 additions & 6 deletions messaging/src/swig/messaging.i
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -315,19 +315,23 @@ void SendPendingEvents() {
// Called from ListenerImpl::MessageReceived() via the
// messageReceivedDelegate.
[MonoPInvokeCallback(typeof(MessageReceivedDelegate))]
private static int MessageReceivedDelegateMethod(System.IntPtr message) {
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;
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 = true;
handler(null, new Firebase.Messaging.MessageReceivedEventArgs(
FirebaseMessage.FromInternal(messageInternal)));
messageInternal.Dispose();
return 1;
return true;
}
return 0;
}, 0);
return false;
}, tookOwnership);
}

// Called from ListenerImpl::TokenReceived() via the
Expand Down
Loading