Skip to content

onNewIntent override crashes sometimes when calling startService #543

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

Closed
igor84 opened this issue Dec 23, 2019 · 13 comments
Closed

onNewIntent override crashes sometimes when calling startService #543

igor84 opened this issue Dec 23, 2019 · 13 comments

Comments

@igor84
Copy link

igor84 commented Dec 23, 2019

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.

@igor84 igor84 added the new New issue. label Dec 23, 2019
@google-oss-bot
Copy link

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

@paulinon
Copy link
Contributor

paulinon commented Jan 8, 2020

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?

@paulinon paulinon added needs-info Need information for the developer and removed new New issue. labels Jan 8, 2020
@stewartmiles
Copy link
Contributor

@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

@igor84
Copy link
Author

igor84 commented Jan 10, 2020

@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 :)

@google-oss-bot google-oss-bot added needs-attention Need Googler's attention and removed needs-info Need information for the developer labels Jan 10, 2020
@igor84
Copy link
Author

igor84 commented Jan 31, 2020

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

@chkuang-g chkuang-g removed the needs-attention Need Googler's attention label Feb 6, 2020
@qbit86
Copy link

qbit86 commented Jun 4, 2020

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()

@qbit86
Copy link

qbit86 commented Jun 4, 2020

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

@binouze
Copy link

binouze commented Nov 17, 2020

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 ?

@sanuzzi
Copy link

sanuzzi commented Dec 21, 2020

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

@MartinGonzalez
Copy link

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

@alexames
Copy link

alexames commented Jan 9, 2021

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.

@vimanyu
Copy link
Contributor

vimanyu commented Feb 17, 2021

Hi, while it might not be related but we just pushed out some changes in Unity SDK 7.1.0 that had some work done to fix some issues with the transition to JobIntentService (#877). It will be very helpful if someone can try out the latest version and let us know if you are still noticing this issue.

@chkuang-g
Copy link
Contributor

chkuang-g commented Mar 4, 2021

I think this should have been fixed in 7.1.0.

See also #939 (comment)

I will close this issue for now. Please report to #939 if you experience something similar or a new ticket if your ANR event is different.

@firebase firebase locked and limited conversation to collaborators Apr 4, 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