firebase / quickstart-unity

Firebase Quickstart Samples for Unity
https://firebase.google.com/games
Apache License 2.0
822 stars 425 forks source link

[Bug] Can't get userData on iOS when user tapped on custom notification action #1035

Open nikita600 opened 3 years ago

nikita600 commented 3 years ago

[REQUIRED] Please fill in the following fields:

[REQUIRED] Please describe the issue here:

Unity didn't get any message data if user tapped on custom notification action button if application was fully closed. I'm not quite sure is this issue valid on iOS 12.0 and lower, but potentially this issue happened on iOS 13.0 and higher. My device working on iOS 14.4

Steps to reproduce:

  1. Add NotificationExtensionsService to your Xcode project.
  2. Declare custom actions. UNNotificationAction should have options setted to UNNotificationActionOptions.
  3. Send notification payload with categoryIdentifier, that add custom action buttons in presented notification.
  4. Check if application fully closed.
  5. Tap on custom notification action button.
  6. When application was fully loaded you didn't get any message data.

Relevant Code:

As I understand, this issue happend just because on iOS 13.0 and higher, function application:didFinishLaunchingWithOptions: no longer gets launchOptions when user launches application from custom notification action button. So on iOS 13.0+ we never gets any message data on Unity side.

I'm not quite sure, but I see only one solution of this issue. Add else brach handling in firebase-cpp-sdk/messaging/src/ios/messaging.mm inside function:

- (void)userNotificationCenter:(UNUserNotificationCenter *)notificationCenter
didReceiveNotificationResponse:(UNNotificationResponse *)response
         withCompletionHandler:(void (^)())completionHandler {
  if (::firebase::messaging::MessagingIsInitialized()) {
    NSDictionary *userInfo = [[[[response notification] request] content] userInfo];
#if !defined(NDEBUG)
    ::firebase::LogInfo("FCM: userInfo: %s.", userInfo.description.UTF8String);
#endif
    ::firebase::messaging::g_message_notification_opened = true;
    ::firebase::messaging::NotifyApplicationAndServiceOfMessage(userInfo);
    id<UNUserNotificationCenterDelegate> user_delegate = ::firebase::messaging::g_user_delegate;
    [user_delegate userNotificationCenter:notificationCenter
           didReceiveNotificationResponse:response
                    withCompletionHandler:completionHandler];
  }
  else
  {
      if (@available(iOS 13.0, *))
      {
          ::firebase::messaging::g_launch_notification = userInfo;
      }
  }
}
patm1987 commented 3 years ago

To confirm: does this else branch functionally fix the issue for you?

google-oss-bot commented 3 years ago

Hey @nikita600. We need more information to resolve this issue but there hasn't been an update in 5 weekdays. I'm marking the issue as stale and if there are no new updates in the next 5 days I will close it automatically.

If you have more information that will help us get to the bottom of this, just add a comment!

nikita600 commented 3 years ago

Sorry, I completely forgot to answer in this thread. I still can't check this issue on my environment, but I try to check this issue on this week.

google-oss-bot commented 3 years ago

Hey @nikita600. We need more information to resolve this issue but there hasn't been an update in 5 weekdays. I'm marking the issue as stale and if there are no new updates in the next 5 days I will close it automatically.

If you have more information that will help us get to the bottom of this, just add a comment!

nikita600 commented 3 years ago

I finally try to compile sources of this library with potential fix. I import compiled library into the Unity project, generated Xcode project and gets a lot of errors that looks like: "Undefined symbols SWIGRegisterStringCallback_FirebaseMessaging". And then I realize that I can't test this fix by myself, because this repository doesn't contains any Unity code.

What I can do in this case?

alexames commented 3 years ago

One way to test it would be to build the C++ library on its own. Then install the Firebase Unity package as normal and locate the C++ library that's bundled with it. Swap out the bundled library with your custom built one, and that should work and hopefully unblock you.

In the mean time I'll schedule some time to look at this so we can test it out your fix ourselves and get it merged in.

chkuang-g commented 3 years ago

@nikita600 I think you mentioned Unity in your first post.

Let me move this to quickstart-unity for management purpose, even though this may be issue in C++ level.

chkuang-g commented 3 years ago

Hey folks,

I did an experiment awhile ago and did notice some different behavior between Firebase Unity SDK and Firebase iOS SDK when NotificationExtensionsService is in use.

The internal bug to track this:

b/182008240

While the issue you reported is when the app is fully closed, the inconsistent behavior I found was when the app is in foreground though. But I suspect that is related to the issue you reported

SDK/Sample Foreground Behavior Background Behavior
iOS Advanced Sample Notification in system tray Notification in system tray
Unity quickstart with modification OnMessageReceived, no notification in system tray Notification in system tray

I will mark it as a bug for further investigation.