6

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?

Tapomay
  • 191
  • 1
  • 2
  • 9
  • What image are you using for the emulation? Did you make sure the image came up with a Google API? You can check it from the SDK manager and verify the installed packages. – Soulslayer Sep 28 '17 at 08:37

4 Answers4

9

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.

Community
  • 1
  • 1
Idanatz
  • 397
  • 2
  • 8
2

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

Mayank Bhatnagar
  • 1,316
  • 2
  • 13
  • 21
  • 2
    It depends, Google provides system images with or without Google APIS. Newer system images are available with Google APIS only. – user1209216 Sep 28 '17 at 08:32
1

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.

enter image description here

2-wipe data:

enter image description here

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.

1

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

Hend El-Sahli
  • 6,268
  • 2
  • 25
  • 42