我想在我的应用中使用 Firebase ,并在调用服务时显示令牌并Broadcast
!我写了下面的代码,但它在textview
中没有显示!
MyFCMInstance:
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);
}
}
SharedPrefsManager代码:
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
中显示令牌?
答案 0 :(得分:1)
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();
}
}
答案 1 :(得分:0)
您需要在保存共享pref呼叫后设置广播呼叫。
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;
}