Skip to content
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

MessageReceived event doesn't fire when app is in background #877

Closed
ErnSur opened this issue Nov 26, 2020 · 19 comments
Closed

MessageReceived event doesn't fire when app is in background #877

ErnSur opened this issue Nov 26, 2020 · 19 comments
Assignees
Milestone

Comments

@ErnSur
Copy link

ErnSur commented Nov 26, 2020

[REQUIRED] Please fill in the following fields:

  • Unity editor version: 2018.4.29f1
  • Firebase Unity SDK version: 6.16.x
  • Source you installed the SDK: Unity Package Manager
  • Problematic Firebase Component: CloudMessaging
  • Other Firebase Components in use: RemoteConfig, Analytics
  • Additional SDKs you are using: GoogleMobileAds
  • Platform you are using the Unity editor on: Mac
  • Platform you are targeting: Android
  • Scripting Runtime: Mono

[REQUIRED] Please describe the issue here:

FirebaseMessaging.MessageReceived does not fire when I click on notification and the app is opened from the background.
I experience the issue in SDK versions of 6.16.0 and 6.16.1, I see no problem in version 6.15.2.

Steps to reproduce:

  1. Open app.
  2. Send notification.
  3. Notification is received. OK.
  4. Hide app (so it stays in the background)
  5. Send notification.
  6. Click on the notification to open the app.
  7. Callbacks registered to FirebaseMessaging.MessageReceived do not fire.

reproducible with the Firebase Unity quickstarts, 100% of the time.

@ErnSur ErnSur added the new New issue. label Nov 26, 2020
@vimanyu
Copy link
Contributor

vimanyu commented Dec 4, 2020

Hi @ErnSur,
Thanks for the question. This is the expected behavior outlined in the docs here,
https://firebase.google.com/docs/cloud-messaging/android/receive

There is some more discussion on this in this post,
https://stackoverflow.com/questions/37358462/firebase-onmessagereceived-not-called-when-app-in-background

Hope this helps.

@vimanyu vimanyu closed this as completed Dec 4, 2020
@ErnSur
Copy link
Author

ErnSur commented Dec 4, 2020

Hi @vimanyu,
I see that you linked some references to Android side of the API and while I understand some technical limitations that may cause this behavior this still does not resolve my problem.

  1. As described in my issue, behavior is different between SDK versions.
  2. Android section of the documentation that you provided tells me to use Android intent - API that is not available from Unity's C# API.
  3. Unity section of the documentation does not mention any of discussed problems.

@vimanyu
Copy link
Contributor

vimanyu commented Dec 4, 2020

Hi @ErnSur,
I apologize I missed the part where you mentioned that it behaves differently on 6.15.1.
I tried it with the messaging unity quickstart testapp and it is indeed different but I dont see any information about the notification being passed to that event.

    public virtual void OnMessageReceived(object sender, Firebase.Messaging.MessageReceivedEventArgs e) {
}      DebugLog("Received a new message");
      var notification = e.Message.Notification;
      if (notification != null) {
        DebugLog("title: " + notification.Title);
        DebugLog("body: " + notification.Body);
        var android = notification.Android;
        if (android != null) {
            DebugLog("android channel_id: " + android.ChannelId);
        }
      

6.16.1/6.15.1 foreground, I can see all the information from the notification.
6.16.1 background, onMessageReceived isn't called at all
6.15.1 background, onMessageReceived seems to be called but notification has no information.

Are you observing something similar on 6.15.1?

I will get in touch with the team to figure out what the expected behavior is here.

@vimanyu vimanyu reopened this Dec 4, 2020
@vimanyu vimanyu added needs-attention Need Googler's attention and removed new New issue. labels Dec 4, 2020
@vimanyu vimanyu self-assigned this Dec 4, 2020
@ErnSur
Copy link
Author

ErnSur commented Dec 5, 2020

I experienced the same behavior, from what I can remember the title and body was missing but not the FirebaseMessage.Data.

@vimanyu
Copy link
Contributor

vimanyu commented Dec 12, 2020

Hi @ErnSur,
Thanks for flagging this. We found the problem and it was because we updated our messaging service to use JobIntentService instead of IntentService (recommended way introduced in Android O). I have queued the fix up for the next release but dont have an estimate of when that will be going out yet.

If you are curious, the change that caused the difference in behavior from 6.15.1 to 6.16.+.
https://github.com/firebase/firebase-cpp-sdk/pull/152/files#diff-7c5c8af611cc405196971b004848f71aef21053d9ebf196914548e488641c639
Look for changes in the file MessageForwardingService.java.

onHandleWork and consequently onHandleIntent was not being called after we moved to using JobIntentService. This meant that "data payloads" from notification didn't make it to the queue.

If you absolutely need the latest and dont mind building from our source, let me know and I can provide you a workaround before our next release.

@npicouet
Copy link

Hi @ErnSur,
Thanks for flagging this. We found the problem and it was because we updated our messaging service to use JobIntentService instead of IntentService (recommended way introduced in Android O). I have queued the fix up for the next release but dont have an estimate of when that will be going out yet.

If you are curious, the change that caused the difference in behavior from 6.15.1 to 6.16.+.
https://github.com/firebase/firebase-cpp-sdk/pull/152/files#diff-7c5c8af611cc405196971b004848f71aef21053d9ebf196914548e488641c639
Look for changes in the file MessageForwardingService.java.

onHandleWork and consequently onHandleIntent was not being called after we moved to using JobIntentService. This meant that "data payloads" from notification didn't make it to the queue.

If you absolutely need the latest and dont mind building from our source, let me know and I can provide you a workaround before our next release.

I'd like some info on that possible workaround, because our app relies heavily on notifications when app is in the background.

@vimanyu
Copy link
Contributor

vimanyu commented Dec 22, 2020

Hi @npicouet,
I apologize but the workaround is more tricky than I expected.
My workaround works for C++ SDK if you are building that from source but you won't be able to build the corresponding Unity SDK as we don't have the open source instructions for that yet. Unfortunately, you will have to wait until the next release. This fix is pending review and should make it into our codebase internally and then we will push out a release with this fix. Until then, you might have to work with the older version. Sorry about that.

@novavision
Copy link

novavision commented Jan 10, 2021

I experience the same problem on Unity 2020.1.17f1. Tried all Firebase Cloud Messaging starting from 6.15.x ending with 7.0.2 - everywhere the same result.
Our API form the message like this:

    notification = {
    	to: registration_token,
    	data: {
    		extra: payload['data']
    	}
    }

    if payload['title']
    notification[: notification] = {}
    notification[: notification][: title] = payload['title']
    notification[: notification][: body] = payload['body']
    notification[: notification][: sound] = 'default'
    notification[: notification][: badge] = payload['badge_count'] || '1'

What means it follows the requirement in documentation, because it has data and notification json objects.
The issue 100% reproducable on quickstart project following the instructions of @ErnSur

@vimanyu any way to make it work finally?

@vimanyu
Copy link
Contributor

vimanyu commented Jan 11, 2021

Are you seeing the problem in 6.15.1?
The issue that I was looking into is the one that was originally reported in this thread, specifically the difference in behavior between 6.15 and 6.16.+.
As far as that difference in behavior is concerned, I can provide a way to fix it in C++ open source SDK but that won't fix the issue in Unity SDK. The fix is ready in our internal systems and will roll out with the next release.

@novavision
Copy link

Are you seeing the problem in 6.15.1?

As I said I tested all SDKs starting from 6.15.0. So yes, it exists. Now I am on 7.0.2, and it still here

The fix is ready in our internal systems and will roll out with the next release.

Did I correctly understand, that this issue existed for almost 1 year (or when that 6.15.0 was released) and only next Unity SDK will include the fix?

To be honest, I can't figure out how such important bugs are

  1. were generally admitted
  2. were not resolved in a timely manner

In my opinion, this functionality is one of the most important - activation push notification with data transfer. Looks like that was important only to me and the creator of this ticket.

@jmmdk
Copy link

jmmdk commented Jan 19, 2021

@npicouet, this very severe bug was reported 2 months ago and 6 weeks ago you found the problem. We experience it, too. When do you think this fix will be included in an update for Unity? It's really needed!

@RobBrooks
Copy link

I've just hit this issue myself.
'Data-only' messages behave just fine but if I combine my data with a Notification, I risk losing my data if the app is backgrounded.
I guess until this is fixed I'll have no option but to send two messages to perform the dual function of alerting my client and sending data to the application, right?
Unity 2020.1.13f1
Firebase Unity SDK 7.0.2

jonsimantov pushed a commit to firebase/firebase-cpp-sdk that referenced this issue Feb 4, 2021
…roduced in Android O.

We moved from using `IntentService` to `JobIntentService` in preparation for this change but the call to schedule jobs remained the same which meant that our custom IntentService was not doing any work at all.

Using `JobIntentService.enqueueWork`, when running on Android O or later, the work will be dispatched as a job via JobScheduler.enqueue. When running on older versions of the platform, it will use Context.startService.
https://developer.android.com/reference/androidx/core/app/JobIntentService

Fixes
firebase/quickstart-unity#877
firebase/quickstart-unity#883

PiperOrigin-RevId: 350176964
@vimanyu
Copy link
Contributor

vimanyu commented Feb 17, 2021

@npicouet, we just released Unity 7.1.0 which should have a fix for the original issue in this ticket.
If you have some time, could you verify if things are working as expected.

@RobBrooks
Copy link

RobBrooks commented Feb 17, 2021 via email

@novavision
Copy link

Can confirm @RobBrooks words: 7.1.0 "fix"doesn't work.
Unbelievable...

@vimanyu
Copy link
Contributor

vimanyu commented Feb 18, 2021

Hi @RobBrooks and @novavision, I am sorry to hear that but just wanted to clarify some thing. The original issue filed by @ErnSur was a change in behavior of a callback function introduced in 6.16.x (it worked in 6.15.2).

"FirebaseMessaging.MessageReceived does not fire when I click on notification and the app is opened from the background. I experience the issue in SDK versions of 6.16.0 and 6.16.1, I see no problem in version 6.15.2"

It might be a different problem from what you are running into, please correct me if I am wrong but 6.15.x does not work for you at all.

If that is the case, we should create a new issue and we will look into that specifically. Are you able to reproduce the problem in any of the quickstart examples? That will speed up the debugging too.

1 similar comment
@vimanyu
Copy link
Contributor

vimanyu commented Feb 18, 2021

Hi @RobBrooks and @novavision, I am sorry to hear that but just wanted to clarify some thing. The original issue filed by @ErnSur was a change in behavior of a callback function introduced in 6.16.x (it worked in 6.15.2).

"FirebaseMessaging.MessageReceived does not fire when I click on notification and the app is opened from the background. I experience the issue in SDK versions of 6.16.0 and 6.16.1, I see no problem in version 6.15.2"

It might be a different problem from what you are running into, please correct me if I am wrong but 6.15.x does not work for you at all.

If that is the case, we should create a new issue and we will look into that specifically. Are you able to reproduce the problem in any of the quickstart examples? That will speed up the debugging too.

@novavision
Copy link

@vimanyu I am having exactly this case.
Here is the issue I created before finding this one: #883

Everything is pretty clear - clicking the received Push Notification and opening the app (fresh start or from background) MessageReceived not called, and there is no way to get the data from playload.

@vimanyu
Copy link
Contributor

vimanyu commented Mar 11, 2021

Hi everyone,
Apologies for the late response. I had some time to look into this and I think I know what is going on. The underlying problem was in our C++ SDK which was fixed in the latest release but Unity SDK has its own Native Custom Activity which is still using the old way of doing things.

I tested the following workaround on my end and now the functionality seems to match what happens in 6.15.1. It will be really helpful if we can get a confirmation this also works on your end.

Tested on Messaging unity quickstart in Unity 2018.4.29f1, Firebase Unity SDK 7.1.0, Android 8.0.
For the workaround, there is a modified Unity Native Activity file and a manifest in the attached zip file
fcm_877.zip

  1. Copy FCM_workaround directory from the attached zip file into your Assets directory (this will place the required java code for the modified Unity native activity). Make sure to click on that file in Unity and ensure only "Android" platform is enabled for it in the Inspector panel.

  2. Update the block for Messaging in Assets/Plugins/Android/AndroidManifest.xml (there is a file AndroidManifest.xml in the zip file for reference but you might not want to use it as you have other plugins in your project). The important sections to look for are repointing to our modified activity and adding a permission to the messaging service.

# Replace 
    <activity android:name="com.google.firebase.MessagingUnityPlayerActivity" 
# with
    <activity android:name="com.google.firebase_workaround.MessagingUnityPlayerActivityWorkaround" 

AND

# Replace
   <service android:name="com.google.firebase.messaging.MessageForwardingService"  android:exported="false">

# with
    <service android:name="com.google.firebase.messaging.MessageForwardingService"
         android:permission="android.permission.BIND_JOB_SERVICE"
         android:exported="false" >
    </service>

Let me know if there are any questions.

@cynthiajoan cynthiajoan removed the needs-attention Need Googler's attention label Mar 16, 2021
@chkuang-g chkuang-g added this to the 7.2.0 milestone Apr 23, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

9 participants