Quickstart-unity: onNewIntent override crashes sometimes when calling startService

Created on 23 Dec 2019  路  11Comments  路  Source: firebase/quickstart-unity

Please fill in the following fields:

Unity editor version: 2019.1.14f1
Firebase Unity SDK version: 5.7.0
Firebase plugins in use (Auth, Database, etc.): Crashlytics, Messaging
Additional SDKs you are using (Facebook, AdMob, etc.): Facebook
Platform you are using the Unity editor on (Mac, Windows, or Linux): Windows
Platform you are targeting (iOS, Android, and/or desktop): Android
Scripting Runtime (Mono, and/or IL2CPP): IL2CPP

Please describe the issue here:

We followed instructions for configuring a custom entry point Activity at https://firebase.google.com/docs/cloud-messaging/unity/client and added this code to our Activity:

protected void onNewIntent(Intent intent) {
  Intent message = new Intent(this, MessageForwardingService.class);
  message.setAction(MessageForwardingService.ACTION_REMOTE_INTENT);
  message.putExtras(intent);
  message.setData(intent.getData());
  startService(message); // This is line 77 from the stacktrace bellow
}

We noticed on Crashlytics a number of crashes happening with following message and stack trace:
Caused by java.lang.IllegalStateException: Not allowed to start service Intent { act=com.google.android.c2dm.intent.RECEIVE cmp=com.nordeus.heroic/com.google.firebase.messaging.MessageForwardingService }: app is in background uid UidRecord{7abca70 u0a14 LAST bg:+2m48s495ms idle change:idle procs:1 seq(1760,1760,1760)}
at android.app.ContextImpl.startServiceCommon + 1720(ContextImpl.java:1720)
at android.app.ContextImpl.startService + 1675(ContextImpl.java:1675)
at android.content.ContextWrapper.startService + 669(ContextWrapper.java:669)
at eu.nordeus.common.MainPlayerNativeActivity.onNewIntent + 77(MainPlayerNativeActivity.java:77)
at android.app.Activity.performNewIntent + 8080(Activity.java:8080)
at android.app.Instrumentation.callActivityOnNewIntent + 1413(Instrumentation.java:1413)
at android.app.Instrumentation.callActivityOnNewIntent + 1426(Instrumentation.java:1426)
at android.app.ActivityThread.deliverNewIntents + 4002(ActivityThread.java:4002)
at android.app.ActivityThread.handleNewIntent + 4014(ActivityThread.java:4014)
at android.app.servertransaction.NewIntentItem.execute + 53(NewIntentItem.java:53)
at android.app.servertransaction.TransactionExecutor.executeCallbacks + 149(TransactionExecutor.java:149)
at android.app.servertransaction.TransactionExecutor.execute + 103(TransactionExecutor.java:103)
at android.app.ActivityThread$H.handleMessage + 2373(ActivityThread.java:2373)
at android.os.Handler.dispatchMessage + 107(Handler.java:107)
at android.os.Looper.loop + 213(Looper.java:213)
at android.app.ActivityThread.main + 8147(ActivityThread.java:8147)
at java.lang.reflect.Method.invoke(Method.java)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run + 513(RuntimeInit.java:513)
at com.android.internal.os.ZygoteInit.main + 1100(ZygoteInit.java:1100)

Please answer the following, if applicable:

Have you been able to reproduce this issue with just the Firebase Unity quickstarts (this GitHub project)?

We couldn't reproduce this issue, we only observed it happening in the wild.

messaging bug

Most helpful comment

@stewartmiles is there any progress on this? Is there some workaround that we can implement while we wait for a proper fix?

All 11 comments

This issue does not seem to follow the issue template. Make sure you provide all the required information.

Hi @igor84 according to your error message, an IllegalStateException is thrown if your app targeting Android 8.0 tries to use that method in a situation when it isn't permitted to create background services. Could you try updating your Firebase SDK and Unity Editor to their latest versions and see if the issue persists?

@igor84 since we're using an IntentService to route data to the C++ SDK I think we have a legit bug here if you're targeting Android 8 or above in this scenario as the MessageForwardingService is still an IntentService rather than a JobIntentService which would schedule the message forwarding using the Job Scheduler on Android 8 or above. I've forwarded this issue to an engineer on the team to look into migration from IntentService to JobIntentService

@paulinon unfortunately since this is only happening in the wild we would have to do these updates on a live project that is already on a tight schedule, so we couldn't do them any time soon.

@stewartmiles thanks for looking into this and sorry I can't be of more help :)

@stewartmiles is there any progress on this? Is there some workaround that we can implement while we wait for a proper fix?

We have the same issue too. Calling startService(message) leads to java.lang.IllegalStateException. And changing it to ContextCompat.startForegroundService(this, message) results in

android.app.RemoteServiceException: Context.startForegroundService() did not then call Service.startForeground()

I think we have a legit bug here if you're targeting Android 8 or above

@stewartmiles Then please fix the documentation, which suggests calling startService(message): https://firebase.google.com/docs/cloud-messaging/unity/client#configuring_a_custom_entry_point_activity

Hi, I have the same issue using a custom UnityPlayerActivity and following the documentation https://firebase.google.com/docs/cloud-messaging/unity/client#configuring_a_custom_entry_point_activity without extending com.google.firebase.MessagingUnityPlayerActivity

Any workaround to prevent crashing ?

Unity 2020.1.12f1
Latest FirebaseSDK 6.16.1
Android min API level 21
Android target API level 30
Device Samsung Galaxy s10 / Android 10

EDIT:

I added a try / catch bloc surrounding startService(message); and the crash is gone, but what will not work if the service is not started ?

Same here.
The error appears hundreds of times a day. Too much loss of notifications.
It has been almost a year since this issue was opened.
Do you have any update?

Unity 2018.4.16
Firebase Messaging: 6.16.1
Devices: Any
OS: Mostly Android 9 and 10

@stewartmiles
is there a workaround/fix? We are also having crashes

Hi all,

The IntentService that stewartmiles mentioned was converted to a JobIntentService a while back, but it looks like this issue was overlooked. Does this bug still occur with the latest releases? If it's still happening I can investigate further, though remaining crashes would probably have a different root cause.

Was this page helpful?
0 / 5 - 0 ratings