本文介绍了Android:第一次运行时,Firebase标记为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用FCM在我的应用程序中提供通知。一切运行良好,但现在我意识到,当我使用Android Studio(而不是从GooglePlay)安装我的应用程序时,标记在第一次运行时为空。当我关闭我的应用程序并重新启动它时,令牌不再为空。是什么导致这个问题,我怎么能避免它?



InstanceIDService:

  $ b $ @Override $ b $ public void onTokenRefresh(){

String token = FirebaseInstanceId.getInstance()。getToken();
registerToken(token);


private void registerToken(String token){

OkHttpClient client = new OkHttpClient();
RequestBody body = new FormBody.Builder()
.add(Token,token)
.build();

Request request = new Request.Builder()
.url(url_to_registration_script)
.post(body)
.build();

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





在MainActivity中:

  FirebaseMessaging.getInstance()。subscribeToTopic(topic); 
String token = FirebaseInstanceId.getInstance()。getToken();
Log.d(TOKEN,token);

第一次安装和启动应用程序时,上次记录返回null



注册脚本:

 <?php 
if(isset($ _ POST [ Token])){

$ _uv_Token = $ _ POST [Token];
$ conn = mysqli_connect(localhost,user,pass,db)或die(Error connecting);
$ q =INSERT INTO users(Token)VALUES('$ _uv_Token')
。ON DUPLICATE KEY UPDATE Token ='$ _uv_Token';;
$ b $ mysqli_query($ conn,$ q)或死(mysqli_error($ conn));
mysqli_close($ conn);
}
?>


解决方案

标记在第一次应用程序启动时被异步获取。您必须等待 onTokenRefresh()在您的 FirebaseInstanceIdService 中被调用才能访问该令牌。


I am using FCM to provide notifications in my app. Everything worked well, but now I realised that, when I install my application using Android Studio (not from GooglePlay) the token is null at first run. When I close my app and restart it, the token is not null anymore. What cause this problem and how can I avoid it?

InstanceIDService:

public class InstanceIDService extends FirebaseInstanceIdService {

    @Override
    public void onTokenRefresh() {

        String token = FirebaseInstanceId.getInstance().getToken();
        registerToken(token);
    }

    private void registerToken(String token) {

        OkHttpClient client = new OkHttpClient();
        RequestBody body = new FormBody.Builder()
                .add("Token",token)
                .build();

        Request request = new Request.Builder()
                .url("url_to_registration_script")
                .post(body)
                .build();

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

In MainActivity:

FirebaseMessaging.getInstance().subscribeToTopic("topic");
String token = FirebaseInstanceId.getInstance().getToken();
Log.d("TOKEN", token);

Last log returns null when app is installed and started for the first time

Registration script:

 <?php
 if (isset($_POST["Token"])) {

       $_uv_Token=$_POST["Token"];
       $conn = mysqli_connect("localhost","user","pass","db") or die("Error connecting");
       $q="INSERT INTO users (Token) VALUES ( '$_uv_Token') "
          ." ON DUPLICATE KEY UPDATE Token = '$_uv_Token';";

  mysqli_query($conn,$q) or die(mysqli_error($conn));
  mysqli_close($conn);
}
?>
解决方案

The token is fetched asynchronously on first app start. You have to wait for onTokenRefresh() to be called in your FirebaseInstanceIdService before the token can be accessed.

这篇关于Android:第一次运行时,Firebase标记为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-02 20:16