在应用程序更新时更新Firebase实例ID [英] Update Firebase Instance ID on app update

查看:54
本文介绍了在应用程序更新时更新Firebase实例ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在GCM时代,建议您每次对应用程序进行更新时,都应检查是否给出了新的注册ID(因为不能保证更新后的注册ID相同).该应用程序启动.

Back in the GCM days it was recommended that every time you put out an update to your app you should check to see if a new registration id was given (since it is was not guaranteed to be the same after an update) when the app starts.

FCM是否仍然如此?我找不到关于此的任何文档

Is that still the case with FCM? I couldn't find any documentation about this

推荐答案

您应在应用运行时检查当前令牌,并监视onTokenRefresh()来检测令牌何时更改.

You should check both the current token when your app runs and monitor onTokenRefresh() to detect when the token changes.

通过调用FirebaseInstanceID.getToken()来检查应用程序/主要活动启动时的当前令牌:

Check the current token on app/main activity startup by calling FirebaseInstanceID.getToken():

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数据库之类的无服务器解决方案).

The sendRegistrationToServer() method that is called in this snippet is something you implement to send the registration token to your own server (or a serverless solution like the Firebase Database).

通过子类化FirebaseInstanceIdService并覆盖onTokenRefresh()来监视令牌的更改:

Monitor for changes to the token by subclassing an FirebaseInstanceIdService and overriding 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

这篇关于在应用程序更新时更新Firebase实例ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆