第一次运行应用时,Firebase令牌为空

时间:2017-04-23 17:31:37

标签: android firebase firebase-cloud-messaging android-sharedpreferences

我正在尝试检索firebase令牌,但是当我第一次运行应用时,firebase令牌为空。我不知道为什么。当我第二次运行应用程序时,我得到了令牌。我看到另一篇帖子有类似的问题,但我对此并不了解。

这是我的代码:

public class MyFirebaseInstanceIDService extends FirebaseInstanceIdService {
private static final String LOGTAG = "android-fcm";

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

    storeTokenInPref(refreshedToken);

}


private void storeTokenInPref(String token) {
    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
    SharedPreferences.Editor editor = preferences.edit();
    editor.putString("regId", token);
    editor.apply();
}

我有一个片段,您可以在其中获取首选项的标记,然后我想比较令牌是否在数据库中。

public class Profile extends Fragment implements MainActivity.OnBackPressedListener{

private View view;

private  SharedPreferences preferences;

private String token;

private static final String LOGTAG = "android-fcm";

public Profile(){}

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    view = inflater.inflate(R.layout.profile, container, false);

    ((MainActivity) getActivity()).setOnBackPressedListener(this);

    preferences = PreferenceManager.getDefaultSharedPreferences(getContext());

    displayFirebaseToken();
    return view;
}

private void displayFirebaseToken() {

    token = preferences.getString("regId", null);
    Log.e(LOGTAG, "Firebase reg id: " + token);

    if (!TextUtils.isEmpty(token)) {

        SQLiteHelper database = new SQLiteHelper(getActivity(), "DB", null, 1);

        List<Beacon> beaconList = database.getAllBeacons();

        String classroom = "";

        for (int i = 0; i < beaconList.size(); i++) {

            if (beaconList.get(i).getId().equals(token)) {
                classroom = beaconList.get(i).getClassroom();
                break;
            }
        }
        classroomTextView.setText(classroom);

    }else {
        System.out.println("Firebase token is not received yet!");
    }
}

2 个答案:

答案 0 :(得分:0)

这是因为在尝试使用displayFirebaseToken()中的值时尚未调用onTokenRefresh()和storeTokenInPref()。您需要等待使用令牌,直到它可用。一般而言,没有硬性规则需要多长时间,因为它取决于网络连接等因素。

答案 1 :(得分:0)

试试这个

private void displayFirebaseToken() {

token = preferences.getString("regId", null);
if(token==null)
token = FirebaseInstanceId.getInstance().getToken();
Log.e(LOGTAG, "Firebase reg id: " + token);

if (!TextUtils.isEmpty(token)) {

    SQLiteHelper database = new SQLiteHelper(getActivity(), "DB", null, 1);

    List<Beacon> beaconList = database.getAllBeacons();

    String classroom = "";

    for (int i = 0; i < beaconList.size(); i++) {

        if (beaconList.get(i).getId().equals(token)) {
            classroom = beaconList.get(i).getClassroom();
            break;
        }
    }
    classroomTextView.setText(classroom);

}else {
    System.out.println("Firebase token is not received yet!");
}

由于您试图通过方法onTokenRefresh()保存设备令牌,直到令牌将被刷新才会被调用,您需要使用`

手动获取它
FirebaseInstanceId.getInstance().getToken();

通过使用此代码段,您可以在应用中的任何位置获取设备令牌

`