简体   繁体   中英

Firebase message not received on emulator

I followed the instructions in https://stackoverflow.com/a/38626398/565212 to connect SNS to FCM to an Android app. When deployed to an emulator, the app initializes but does not receive any messages. The same app works correctly on my actual Nexus 6 device and receives messages. Why this difference?

Does the emulator has Google Play Services installed?

source: https://firebase.google.com/docs/cloud-messaging/android/client#sample-play

Apps that rely on the Play Services SDK should always check the device for a compatible Google Play services APK before accessing Google Play services features. It is recommended to do this in two places: in the main activity's onCreate() method, and in its onResume() method. The check in onCreate() ensures that the app can't be used without a successful check. The check in onResume() ensures that if the user returns to the running app through some other means, such as through the back button, the check is still performed.

If the device doesn't have a compatible version of Google Play services, your app can call GoogleApiAvailability.makeGooglePlayServicesAvailable() to allow users to download Google Play services from the Play Store.

Because emulator doesn't have google api's. So to check notification or message you have to check on a real device which has google services installed in that device.

For Google Services like GCM, use a "Google APIs" (any version) target to receive push notifications or messages from fcm

This answer is written in Sep, 2021 with these gradle dependencies:

    implementation 'com.google.firebase:firebase-messaging:22.0.0'
implementation 'com.google.firebase:firebase-iid:21.1.0'

Some times, Emulator stops to receive the FCM notification, here is what I do to solve it:

1- Chose Emulator with Google API.

在此处输入图片说明

2-wipe data:

在此处输入图片说明

3-Use below function to get multicast ID, or to get the reason for failure registration.

  private void check_token() {
    String TAG = "_testing";
    FirebaseInstanceId.getInstance().getInstanceId()
            .addOnCompleteListener(new OnCompleteListener<InstanceIdResult>() {
                @Override
                public void onComplete(@NonNull Task<InstanceIdResult> task) {
                    if (!task.isSuccessful()) {
                        Log.e(TAG, "getInstanceId failed", task.getException());
                        return;
                    }

                    // Get new Instance ID token
                    String token = task.getResult().getToken();

                    // Log and toast
                    //  String msg = getString(R.string.msg_token_fmt, token);
                    Log.e(TAG, token);
                    Toast.makeText(MainActivity3.this, token, Toast.LENGTH_SHORT).show();
                }
            });
}

Once, you got the token , you can try to send the notification message.

Note: No need to login with google account. FCM is working without gmail login.

Worked after creating a new emulator in Android Studio > AVD Manager with Android SDK that has `google APIs installed

Also make sure that SDK Manager > SDK Tools > Google Play Services is installed

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM