微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

android – 在应用更新时更新Firebase实例ID

回到GCM时代,建议您每次向应用程序发布更新时,都应该检查是否有新的注册ID(因为在更新后不能保证相同)应用程序启动时.

FCM的情况仍然如此吗?我找不到任何关于此的文档

最佳答案
您应该在应用程序运行时检查当前令牌并监视onTokenRefresh()以检测令牌何时更改.

通过调用FirebaseInstanceID.getToken()检查app / main活动启动时的当前令牌:

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ...
        String token = FirebaseInstanceId.getInstance().getToken();
        if (token != null) {
            sendRegistrationToServer(refreshedToken);
        }
    }
    ...

您在此代码段中调用的sendRegistrationToServer()方法是您将注册令牌发送到您自己的服务器(或Firebase数据库之类的无服务器解决方案)的方法.

通过继承FirebaseInstanceIdService并覆盖onTokenRefresh()来监视令牌的更改:

@Override
public void onTokenRefresh() {
    String refreshedToken = FirebaseInstanceId.getInstance().getToken();

    // If you want to send messages to this application instance or
    // manage this apps subscriptions on the server side,send the
    // Instance ID token to your app server.
    sendRegistrationToServer(refreshedToken);
}

请参阅此处的文档:https://firebase.google.com/docs/cloud-messaging/android/client#sample-register

原文地址:https://www.jb51.cc/android/430061.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐