FCM with Unity3D and overriding the method onCreate

516 views
Skip to first unread message

Josh

unread,
Feb 27, 2017, 4:08:01 PM2/27/17
to Firebase Google Group
Hello!

I have a question for FCM on Unity. It states that the method onCreate needs to be overwritten with the one provide by Firebase in the UnityPlayerActivity. It seems to be nulling out the reference to mUnityPlayer and quitting it if there is a reference. The website states, "This ensures that when the app starts up again it does not start with stale data." I'm not sure if this is needed because the Unity activity class for onCreate creates a new instance of the mUnityPlayer every time onCreate is called. So my question is do we really need to add this:

if (this.mUnityPlayer != null)
{
    this.mUnityPlayer.quit();
    this.mUnityPlayer = null;
}

Also I am currently using prime31's plugin, which becomes the main Activity and handles distributing the different life cycles of Android. And since FCM's way of overriding it is for onCreate to be the first thing executed, it becomes a problem. Is there a specific reason as to why quitting and nulling out the unity player is needed? Is it to solve an issue where 10% of the notifications aren't get received? Or an issue that doesn't happen that often?
I am thinking of not putting those lines of code in. So I would like to know what the reasoning is and if it's something I need to consider putting in.

Thanks!

Stewart Miles

unread,
Feb 27, 2017, 5:55:11 PM2/27/17
to fireba...@googlegroups.com
Android has separate state for the native (C/C++) component and the Java component of the app.  When a Android activity's onCreate() is called the *Java* side of the app is starting / restarting https://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle . When the activity is killed the whole process is not killed which means all that state in the native component (backed by mUnityPlayer in this case) is not reset with the rest of the app.  Re-entering the app without restarting like this can cause a crash in the Unity player without it being shut down first.  Pull this logic out and you'll end up - in the best case an unstable app - in the worst case something that will simply crash.

onStop() --> onRestart()
- Do not re-create the native component as the activity isn't killed.

onStop() --> onCreate()
- Need to recreate the native components as it's likely the activity has been killed and should be reinitialized.

If I remember correctly, the reason we have this in particular for FCM is that when a user opens a notification FCM's service forces the activity to run through onCreate().

If you're trying to use FCM + Prime31 it sounds like you need to compile the activity logic, FCM has very little glue required, why would you remove this code?  What problems are you running into?

Cheers,
Stewart


--
You received this message because you are subscribed to the Google Groups "Firebase Google Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to firebase-talk+unsubscribe@googlegroups.com.
To post to this group, send email to fireba...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/firebase-talk/6b885ce9-5ada-4c44-ac6b-a518e6531acd%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Josh

unread,
Mar 1, 2017, 10:08:37 AM3/1/17
to Firebase Google Group
Hey Stewart,

Thanks for the quick reply.
One of the reasons why it's problematic is because I'm trying to not change any 3rd party code. In this case, Prime31's "activity logic". In terms of FCM's snippet for onCreate, it requires it to null first then to call the super class's onCreate. Prime31 doesn't do this, so something is going to have to change. That leaves me changing the FCM unity provided jar, which I don't want to since it's a 3rd party plugin, or creating my own. I can create my own, but I don't know what I make will break anything else. Which leads me to another question, which is about the onNewIntent method. There's another plugin (Branch.io) that uses the onNewIntent method and won't that conflict with the FCM one? Meaning, both will try to start the activity, because isn't that what the MessageForwardingService does (tries to start up the activity... unless that's not what happens and the service is just a side loaded thing?)? Sorry, I'm not much of an android dev so I'm not entirely sure if that will happen.
To unsubscribe from this group and stop receiving emails from it, send an email to firebase-tal...@googlegroups.com.

Stewart Miles

unread,
Mar 6, 2017, 5:52:09 PM3/6/17
to fireba...@googlegroups.com
Unfortunately, due to the way the Android framework is structured we - and other SDKs - can't just register for callbacks on Android Activity state changes.  Activity changes are communicated to an app via overridden methods on the activity class for the application.

Since you're trying to minimize the amount of 3rd party SDKs you modify you could simply replace the Plugins/Android/AndroidManifest.xml supplied with your own that includes the components you need.  Then customize it by calling your own application specific activity instead of the one supplied by us (Firebase) or 3rd party SDKs.  From your own application specific activity you can include the logic from 3rd party SDK activities.  You don't need to change any of the 3rd party jars / aars, if they're not referenced by the AndroidManifest.xml then - assuming you have Proguard turned on - they'll be stripped out and they won't be called anyway.

We're open to ideas to improve this situation.  Something we considered were free floating methods that could be called from an application specific Activity class but that isn't possible in the case of "fixing" Unity's "Activity.onCreate" since we need access to mUnityPlayer which is a protected member of the class.  I guess we could try something hokey like try still shipping an Activity that extends UnityPlayerActivity but has static methods that could modify other activities that extend UnityPlayerActivity, TBH it doesn't feel that much better to me.

I hope this helps.

Cheers,
Stewart 

To unsubscribe from this group and stop receiving emails from it, send an email to firebase-talk+unsubscribe@googlegroups.com.

To post to this group, send email to fireba...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages