我正在关注 this 在 Firebase 中注册我的设备
I am following this to register my deivce in Firebase
这里我尝试显示并保存通知令牌
Here I am trying to display and save the notification token
public class MyFirebaseInstanceIDService extends FirebaseInstanceIdService {
private static final String TAG = "MyFirebaseIIDService";
@Override
public void onTokenRefresh() {
String refreshedToken = FirebaseInstanceId.getInstance().getToken();
Log.d(TAG, "Refreshed token: " + refreshedToken);
storeToken(refreshedToken);
}
private void storeToken(String token) {
//saving the token on shared preferences
SharedPrefManager.getInstance(getApplicationContext()).saveDeviceToken(token);
}
}
当我尝试注册时,总是说 Token Token not generated
表单MainActivity
When I try to register it Always Says Token Token not generated
form
MainActivity
所以这里我的应用程序连接到 Firebase.. 我知道 FirebaseInstanceIdService
已弃用我也尝试过这个
So Here My Application is connected to Firebase.. and I know that FirebaseInstanceIdService
is Deprecated I also tried with this
public class MyFirebaseInstanceIDService extends FirebaseMessagingService {
private static final String TAG = "MyFirebaseIIDService";
@Override
public void onNewToken(String refreshedToken) {
refreshedToken = FirebaseInstanceId.getInstance().getInstanceId().getResult().getToken();
Log.d(TAG, "Refreshed token: " + refreshedToken);
storeToken(refreshedToken);
}
private void storeToken(String token) {
//saving the token on shared preferences
SharedPrefManager.getInstance(getApplicationContext()).saveDeviceToken(token);
}
}
但仍然没有生成相同的令牌
But Still same token is not generated
只需在你的代码中添加这个..
Just add this in your code..
public class MyActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
SharedPreferences prefs = getSharedPreferences("TOKEN_PREF", MODE_PRIVATE);
String token = prefs.getString("token", "");
Log.e("NEW_INACTIVITY_TOKEN", token);
if (TextUtils.isEmpty(token)) {
FirebaseInstanceId.getInstance().getInstanceId().addOnSuccessListener(MyActivity.this, new OnSuccessListener<InstanceIdResult>() {
@Override
public void onSuccess(InstanceIdResult instanceIdResult) {
String newToken = instanceIdResult.getToken();
Log.e("newToken", newToken);
SharedPreferences.Editor editor = getSharedPreferences("TOKEN_PREF", MODE_PRIVATE).edit();
if (token!=null){
editor.putString("token", newToken);
editor.apply();
}
}
});
}
}
}
Firebase 令牌在应用首次安装和运行时调用一次,因此忽略保存数据或在使用共享首选项保存数据后运行此活动/服务
Firebase Tokens are called once when the app is installed and runned for the first time so ignore the Saving the data or running this Activity/Service once the Data is Saved with Shared Prefs
这篇关于如何在 Android 中保存 FCM 令牌?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!