简体   繁体   English

Android Firebase云消息传递控制onTokenRefresh()上的服务

[英]Android Firebase cloud messaging Control the services onTokenRefresh()

I am new to firebase, I have a login screen. 我是Firebase的新手,我有一个登录屏幕。 Post successful login, i would like to invoke the getToken method cause along with the Token i am also saving User loginid in centralised server. 成功登录后,我想调用getToken方法以及Token,我也将User loginid保存在集中式服务器中。 Hence after login i am calling these two lines. 因此,登录后我呼叫这两行。

FirebaseMessaging.getInstance().subscribeToTopic("test");
FirebaseInstanceId.getInstance().getToken();

My services is as follows 我的服务如下

public class FirebaseInstanceIDService extends FirebaseInstanceIdService {

    public static  String busid;


    @Override
    public void onTokenRefresh() {
        String token = FirebaseInstanceId.getInstance().getToken();
        registerToken(token);
    }

    private void registerToken(String token) {


     OkHttpClient client = new OkHttpClient();
     business_login_id=Lib.GetPref(FirebaseInstanceIDService.this, "KEY_USER_ID", "");


        RequestBody body = new FormBody.Builder()
                .add("token",token)
                .add("business_login_id", business_login_id)
                .build();

  Lib.Debug("Calling URL with body "+body.toString());

        Request request = new Request.Builder()
                .url("http://mywebsite.com/gcm/register.php")
                .post(body)
                .build();

        try {
            client.newCall(request).execute();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

My question is : Why Service FirebaseInstanceIDService is executing irrespective of Post Login screen and creating the Token id instantly for the app (in my case business_login_id is null). 我的问题是:为什么无论登录后屏幕如何,都执行Service FirebaseInstanceIDService,并立即为该应用程序创建令牌ID(在我的情况下,business_login_id为null)。

At this time my user id is not logged in yet. 目前,我的用户ID尚未登录。 Is there any way to control exactly POST login with the right credential create the Token NOW? 有什么方法可以使用正确的凭证创建Token NOW来完全控制POST登录?

A FirebaseInstanceIdService is automatically started when you app is first installed. 首次安装应用程序时, FirebaseInstanceIdService将自动启动。 It does not wait until the user is signed in, nor does it have to: the token can be gotten without the user being signed in. 它不会等待,直到用户已登录,也不需要:令牌可以在没有用户签字来得到

But it is often the case that you cannot save the token (eg to a hosted database) until the user is authenticated. 但是通常情况下,在用户通过身份验证之前,您无法保存令牌(例如, 保存到托管数据库)。 So you'll need to either hang on to the token somewhere in the app OR request the token later. 因此,您需要挂在应用程序中某个地方的令牌上,或者稍后再请求该令牌。

Hang on to the token 挂在令牌上

You can store the token in Shared Preferences until you app is ready to consume it: 您可以将令牌存储在“共享首选项”中,直到您的应用可以使用它为止:

SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putString("token", token);
editor.commit();

And then once the user signs in: 然后,用户登录后:

SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
String token = sharedPref.getInt(token, null);
registerToken(token);

Request the token later 稍后请求令牌

Firebase Cloud Messaging will generate a token as soon as the app is installed. 安装该应用程序后,Firebase Cloud Messaging将立即生成令牌。 So it is very likely that by the time your user signs in, the token has long been generated. 因此,很可能在您的用户登录时,很早就生成了令牌。 In that case, you can also simply request the token after the user has signed in. 在这种情况下,您也可以在用户登录后简单地请求令牌。

If you'd be using Firebase Authentication, that'd be something like: 如果您使用的是Firebase身份验证,则类似于:

FirebaseAuth.getInstance().addAuthStateListener(new FirebaseAuth.AuthStateListener() {
    public void onAuthStateChanged(FirebaseAuth firebaseAuth) {
        FirebaseUser user = firebaseAuth.getCurrentUser();
        if (user != null) {
            String token = FirebaseInstanceId.getInstance().getToken();
            registerToken(token);
        }
    }
};

For Firebase no need to check more references. 对于Firebase,无需检查更多参考。 Just go through this sample code and it's 100% working. 只需阅读此示例代码,它就可以100%工作。

https://github.com/firebase/quickstart-android https://github.com/firebase/quickstart-android

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

相关问题 (Android)Firebase云消息传递-从不在特定设备上调用ontokenrefresh - (Android) Firebase cloud messaging - ontokenrefresh never called on specific device 从未调用过Android Firebase Cloud Messaging(FCM)onTokenRefresh()并且getToken()返回null - Android Firebase Cloud Messaging (FCM) onTokenRefresh() never called and getToken() returning null Android Firebase云消息传递 - Android Firebase cloud messaging 正确使用 Firebase 消息和 Flutter 的 onTokenRefresh() 方法 - Correct way to use onTokenRefresh() of Firebase Messaging and Flutter Android的Firebase通知,onTokenRefresh方法 - Firebase Notification for Android, onTokenRefresh Method Android Studio Firebase /云消息传递 - android studio firebase / cloud messaging Android,FireBase 云消息传递,(FCM) - Android, FireBase Cloud Messaging, (FCM) 与Firebase相关的Google Play服务更新后,Android Google Cloud Messaging(GCM)令牌生成崩溃 - Android Google Cloud Messaging (GCM) token generation crash after Firebase-related Google Play Services update Firebase Cloud Messaging,无法将插件“ com.google.gms.google-services”添加到Android Studio中 - Firebase Cloud Messaging,Can not add the Plugin 'com.google.gms.google-services' to gradle in android Studio Google Cloud Messaging与Android Services - Google Cloud Messaging vs Android Services
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM