How to make Firebase Invites work on emulator?

2019-07-09 02:22发布

问题:

I am testing Firebase Invites from an emulator based on tutorial from official site.

implementation 'com.google.firebase:firebase-invites:16.0.4'

Emulator used:

CPU/ABI: Google Play Intel Atom (x86)

Target: google_apis_playstore [Google Play] (API level 28)

Skin: pixel_2

I try to call activity for result like this.

private void onInviteClicked() {
    Intent intent = new AppInviteInvitation.IntentBuilder(getString(R.string.invitation_title))
            .setMessage(getString(R.string.invitation_message))
            .setDeepLink(Uri.parse(getString(R.string.invitation_deep_link)))
            .setCustomImage(Uri.parse(getString(R.string.invitation_custom_image)))
            .setCallToActionText(getString(R.string.invitation_cta))
            .build();
    startActivityForResult(intent, REQUEST_INVITE);
}

But in activityOnResult I always get Activity.RESULT_CANCELLED as a result code.

if (requestCode == REQUEST_INVITE) {
    if (resultCode == RESULT_OK) {
        // Get the invitation IDs of all sent messages
        String[] ids = AppInviteInvitation.getInvitationIds(resultCode, data);
        for (String id : ids) {
            Log.d(TAG, "onActivityResult: sent invitation " + id);
        }
    } else {
        // Sending failed or it was canceled, show failure message to the user
        // ...
    }
}

It turns out that it is because there is a crash in invites library:

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.google.android.gms.ui, PID: 10555
java.lang.RuntimeException: Error receiving broadcast Intent { act=com.google.android.gms.appinvite.intent.action.INVITE_SENT cat=[android.intent.category.DEFAULT] flg=0x10 (has extras) } in com.google.android.gms.appinvite.AppInviteChimeraActivity$AppInviteResponseReceiver@d32d00e
    at android.app.LoadedApk$ReceiverDispatcher$Args.lambda$getRunnable$0(LoadedApk.java:1401)
    at android.app.-$$Lambda$LoadedApk$ReceiverDispatcher$Args$_BumDX2UKsnxLVrE6UJsJZkotuA.run(Unknown Source:2)
    at android.os.Handler.handleCallback(Handler.java:873)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:193)
    at android.app.ActivityThread.main(ActivityThread.java:6669)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
 Caused by: android.view.WindowManager$BadTokenException: Unable to add window android.view.ViewRootImpl$W@93c43be -- permission denied for window type 2003
    at android.view.ViewRootImpl.setView(ViewRootImpl.java:822)
    at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:356)
    at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:93)
    at fzg.a(:com.google.android.gms@14366040@14.3.66 (100700-213742215):22)
    at fzg.a(:com.google.android.gms@14366040@14.3.66 (100700-213742215):15)
    at fzg.a(:com.google.android.gms@14366040@14.3.66 (100700-213742215):1)
    at com.google.android.gms.appinvite.AppInviteChimeraActivity.a(:com.google.android.gms@14366040@14.3.66 (100700-213742215):91)
    at com.google.android.gms.appinvite.AppInviteChimeraActivity$AppInviteResponseReceiver.a(:com.google.android.gms@14366040@14.3.66 (100700-213742215):2)
    at xbc.onReceive(:com.google.android.gms@14366040@14.3.66 (100700-213742215):1)
    at android.app.LoadedApk$ReceiverDispatcher$Args.lambda$getRunnable$0(LoadedApk.java:1391)
    at android.app.-$$Lambda$LoadedApk$ReceiverDispatcher$Args$_BumDX2UKsnxLVrE6UJsJZkotuA.run(Unknown Source:2) 
    at android.os.Handler.handleCallback(Handler.java:873) 
    at android.os.Handler.dispatchMessage(Handler.java:99) 
    at android.os.Looper.loop(Looper.java:193) 
    at android.app.ActivityThread.main(ActivityThread.java:6669) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858) 

This happens only on emulator and not on physical device.

Anyone knows how to make Firebase Invites work also on emulator?

回答1:

WindowManager.LayoutParams constant 2003 is TYPE_SYSTEM_ALERT, deprecated in API level 26. either build against API 25 (which may be that physical device) or use TYPE_APPLICATION_OVERLAY instead. with a single line from the build.gradle it's difficult to understand against which API the code even runs.

hint: make sure you run this on an emulator image with Play Services installed.