在 Android 中获取 FCM 消息令牌的正确方法是什么?

What is the correct way to get the messaging token for FCM in Android?

晚上好!

我希望将 Firebase 云消息传递添加到我的项目中 (Android、Java)。我已遵循文档 https://firebase.google.com/docs/cloud-messaging/android/client#retrieve-the-current-registration-token 到发球区,但在检索消息传递令牌时仍然遇到问题。 Android studio 声称 无法解析 'FirebaseMessaging' 中的方法 'getToken' 并且不会编译。我假设我需要编写一个方法 getToken(),但找不到任何关于该方法应该做什么的文档。

这是我的 class,它如文档所述扩展了 FirebaseMessagingService。

public class PushNotificationManager extends FirebaseMessagingService {

    public String MT;

    public PushNotificationManager() {
        FirebaseMessaging.getInstance().getToken()
                .addOnCompleteListener(new OnCompleteListener<String>() {
            @Override
            public void onComplete(@NonNull Task<String> task) {
                if (!task.isSuccessful()) {
                    System.out.println("Failed to get token");
                    return;
                }
                MT = task.getResult();

            }
        });
    }

    @Override
    public void onNewToken(String token) {
        super.onNewToken(token);
        MT =  token;
        System.out.println("Refreshed token: " + token);
    }
}

根据文档,我也重写了 onNewToken(),但我从未打印到日志中。

这是我添加到清单中的服务:

        <service android:name=".PushNotificationManager">
            <intent-filter>
                <action android:name="com.google.firebase.MESSAGING_EVENT" />
            </intent-filter>
        </service>

有很多关于这个主题的旧堆栈溢出文章,但 none 已经奏效,大多数都超过一年,并且使用现在已贬值的 InstanceID()。

有什么建议吗?感谢您的宝贵时间!

更新

感谢我在下面与 Doug 的对话,我已经解决了我的问题。首先,我在这个 class 中专注于 onNewToken(),并在构造函数中删除了我的 getToken() 请求。

public class PushNotificationManager extends FirebaseMessagingService {

    public String MT;

    public PushNotificationManager() {

    }

    @Override
    public void onNewToken(String token) {
        super.onNewToken(token);
        MT =  token;
        System.out.println("Refreshed token: " + token);
    }
}

这与清单文件的更新相结合,在应用程序启动时记录消息令牌完全重新安装后。现在,要将令牌存储在 Firebase RTB 中,我需要在初始化数据库后访问它。所以我在我的数据库中添加了一个方法class,在数据库初始化之后调用。它看起来像这样:

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

                        // Get new Instance ID token
                        String MT = (String) task.getResult().getToken();
                        System.out.println("Messaging token: "+MT);
//Now I can save it to the RTB
                    }
                });
    }

现在可以正常使用了!感谢道格的帮助!

如果您在 FirebaseMessagingService 的注册子 class 中覆盖 onNewToken,那么您不需要任何其他东西来获得令牌。您可以使用 getToken 按需获取它,或者使用 onNewToken 通过回调接收它。几乎没有理由同时执行这两项操作(尤其是在同一个 class 中),除非您绝对知道自己需要这样做。

如果您没有收到 onNewToken 的回调,那么您可能没有完全 follow the instructions 并将 FirebaseMessagingService 的子 class 添加为 Android清单中的服务。