数据不会显示在使用 Android 的 BroadcastReceiver 中



我想在我的应用程序中使用 Firebase,并在调用服务和Broadcast时显示令牌!我写了下面的代码,但它在textview中没有显示更多!

MyFCMnstance :

public class MyFCMInstance extends FirebaseInstanceIdService {
    public static final String TOKEN_BROADCAST = "fcmtokenbroadcast";
    @Override
    public void onCreate() {
        super.onCreate();
        String CurrentToken = FirebaseInstanceId.getInstance().getToken();
        if (CurrentToken != null) {
            Intent intent = new Intent("Token");
            LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
            Log.d("token", "Refreshed token: " + CurrentToken);
            storeToken(CurrentToken);
            getApplicationContext().sendBroadcast(new Intent(TOKEN_BROADCAST));
        } else {
            onTokenRefresh();
        }
    }
    @Override
    public void onTokenRefresh() {
        String refreshedToken = FirebaseInstanceId.getInstance().getToken();
        Log.e("FCM Instance", "Token : " + refreshedToken);
        // store token
        storeToken(refreshedToken);
        // send broadcast
        getApplicationContext().sendBroadcast(new Intent(TOKEN_BROADCAST));
    }
    private void storeToken(String token) {
        SharedPrefsManager.getInstance(getApplicationContext()).storeToken(token);
    }
}

共享首选项管理器代码:

public class SharedPrefsManager {
    private static final String SHARED_PREF_NAME = "FCMPrefs";
    private static final String KEY_ACCESS_TOKEN = "Token";
    private static Context mContext;
    private static SharedPrefsManager mInstance;
    public SharedPrefsManager(Context context) {
        mContext = context;
    }
    public static synchronized SharedPrefsManager getInstance(Context context) {
        if (mInstance == null) {
            mInstance = new SharedPrefsManager(context);
        }
        return mInstance;
    }
    public boolean storeToken(String token) {
        SharedPreferences preferences = mContext.getSharedPreferences(SHARED_PREF_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = preferences.edit();
        editor.putString(KEY_ACCESS_TOKEN, token);
        editor.apply();
        return true;
    }
    public String getToken() {
        SharedPreferences preferences = mContext.getSharedPreferences(SHARED_PREF_NAME, Context.MODE_PRIVATE);
        String token = preferences.getString(KEY_ACCESS_TOKEN, null);
        return token;
    }
}

主要活动代码:

public class MainActivity extends AppCompatActivity {
    private TextView tokenTV;
    private BroadcastReceiver broadcastReceiver;
    private Context context;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tokenTV = (TextView) findViewById(R.id.tokenText);
        context = MainActivity.this;
        broadcastReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                tokenTV.setText(SharedPrefsManager.getInstance(context).getToken());
            }
        };
        registerReceiver(broadcastReceiver, new IntentFilter(MyFCMInstance.TOKEN_BROADCAST));
    }
}

如何修复它并在TextView中显示令牌

onTokenRefresh()方法仅在安装应用程序后调用一次。因此,在您的代码中,它只会显示一次令牌,而且也只会显示第一次。重写 fcm 服务类中的 onCreate(( 方法并执行以下操作

 @Override
    public void onCreate() {
        super.onCreate();
        String CurrentToken = FirebaseInstanceId.getInstance().getToken();
        if (CurrentToken!=null){
            Intent intent = new Intent("device_id");
            LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
            Log.d("token", "Refreshed token: " + CurrentToken);
            appSharedPreferences.putString("device_id",CurrentToken);
            getApplicationContext().sendBroadcast(new Intent(TOKEN_BROADCAST));
        }
        else {
          onTokenRefresh();

                   }
}

您需要在保存共享首选项呼叫后设置广播呼叫。

public class MyFCMInstance extends FirebaseInstanceIdService {
    public static final String TOKEN_BROADCAST = "fcmtokenbroadcast";
    @Override
    public void onTokenRefresh() {
        String refreshedToken = FirebaseInstanceId.getInstance().getToken();
        Log.e("FCM Instance", "Token : " + refreshedToken);
        // store token
        storeToken(refreshedToken);
        // send broadcast
        getApplicationContext().sendBroadcast(new Intent(TOKEN_BROADCAST));
    }
    private void storeToken(String token){
        SharedPrefsManager.getInstance(getApplicationContext()).storeToken(token);
    }
}

public boolean storeToken(String token) {
        SharedPreferences preferences = mContext.getSharedPreferences(SHARED_PREF_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = preferences.edit();
        editor.putString(KEY_ACCESS_TOKEN, token);
        editor.commit();
        return true;
    }

相关内容

  • 没有找到相关文章

最新更新