简体   繁体   English

模拟器上未收到 Firebase 消息

[英]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.我按照https://stackoverflow.com/a/38626398/565212中的说明将 SNS 连接到 FCM 到 Android 应用程序。 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.相同的应用程序在我的实际 Nexus 6 设备上正常运行并接收消息。 Why this difference?为什么会有这种差异?

Does the emulator has Google Play Services installed? 模拟器是否安装了Google Play服务?

source: https://firebase.google.com/docs/cloud-messaging/android/client#sample-play 来源: 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. 依赖Play服务SDK的应用在访问Google Play服务功能之前,应始终检查设备是否有兼容的Google Play服务APK。 It is recommended to do this in two places: in the main activity's onCreate() method, and in its onResume() method. 建议在两个位置执行此操作:在主活动的onCreate()方法及其onResume()方法中。 The check in onCreate() ensures that the app can't be used without a successful check. 在onCreate()中进行检查可确保未经成功检查就无法使用该应用程序。 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. 在onResume()中进行检查可确保,如果用户通过其他方式(例如通过“后退”按钮)返回到正在运行的应用程序,则仍会执行检查。

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. 如果设备没有兼容版本的Google Play服务,则您的应用可以调用GoogleApiAvailability.makeGooglePlayServicesAvailable(),以允许用户从Play商店下载Google Play服务。

Because emulator doesn't have google api's. 因为模拟器没有Google API。 So to check notification or message you have to check on a real device which has google services installed in that device. 因此,要检查通知或消息,您必须在安装了Google服务的真实设备上进行检查。

For Google Services like GCM, use a "Google APIs" (any version) target to receive push notifications or messages from fcm 对于GCM之类的Google服务,请使用“ Google API”(任何版本)目标来接收来自fcm的推送通知或消息

This answer is written in Sep, 2021 with these gradle dependencies:这个答案写于 2021 年 9 月,具有以下 gradle 依赖项:

    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:有时,模拟器停止接收 FCM 通知,这是我解决它的方法:

1- Chose Emulator with Google API. 1- 选择带有 Google API 的模拟器。

在此处输入图片说明

2-wipe data: 2-擦除数据:

在此处输入图片说明

3-Use below function to get multicast ID, or to get the reason for failure registration. 3-使用下面的函数来获取组播ID,或者获取注册失败的原因。

  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. FCM 无需 gmail 登录即可工作。

Worked after creating a new emulator in Android Studio > AVD Manager with Android SDK that has `google APIs installedAndroid Studio > AVD Manager中使用Android SDK创建一个新的模拟器后工作

Also make sure that SDK Manager > SDK Tools > Google Play Services is installed还要确保安装了SDK Manager > SDK Tools > Google Play Services

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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