• <tfoot id='Txtn1'></tfoot>
    1. <i id='Txtn1'><tr id='Txtn1'><dt id='Txtn1'><q id='Txtn1'><span id='Txtn1'><b id='Txtn1'><form id='Txtn1'><ins id='Txtn1'></ins><ul id='Txtn1'></ul><sub id='Txtn1'></sub></form><legend id='Txtn1'></legend><bdo id='Txtn1'><pre id='Txtn1'><center id='Txtn1'></center></pre></bdo></b><th id='Txtn1'></th></span></q></dt></tr></i><div id='Txtn1'><tfoot id='Txtn1'></tfoot><dl id='Txtn1'><fieldset id='Txtn1'></fieldset></dl></div>

        <legend id='Txtn1'><style id='Txtn1'><dir id='Txtn1'><q id='Txtn1'></q></dir></style></legend>

      1. <small id='Txtn1'></small><noframes id='Txtn1'>

          <bdo id='Txtn1'></bdo><ul id='Txtn1'></ul>

        如何在 Android 中保存 FCM 令牌?

        时间:2024-05-10
        <legend id='iKS87'><style id='iKS87'><dir id='iKS87'><q id='iKS87'></q></dir></style></legend>
          <bdo id='iKS87'></bdo><ul id='iKS87'></ul>

          <tfoot id='iKS87'></tfoot>
            1. <i id='iKS87'><tr id='iKS87'><dt id='iKS87'><q id='iKS87'><span id='iKS87'><b id='iKS87'><form id='iKS87'><ins id='iKS87'></ins><ul id='iKS87'></ul><sub id='iKS87'></sub></form><legend id='iKS87'></legend><bdo id='iKS87'><pre id='iKS87'><center id='iKS87'></center></pre></bdo></b><th id='iKS87'></th></span></q></dt></tr></i><div id='iKS87'><tfoot id='iKS87'></tfoot><dl id='iKS87'><fieldset id='iKS87'></fieldset></dl></div>

                <small id='iKS87'></small><noframes id='iKS87'>

                    <tbody id='iKS87'></tbody>
                  本文介绍了如何在 Android 中保存 FCM 令牌?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在关注 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 令牌?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:使用 GWT 时找不到 Firebase Service Worker(404 错误) 下一篇:com.google.android.gms.common.internal.safe parcel.safe parc

                  相关文章

                    <bdo id='rElz0'></bdo><ul id='rElz0'></ul>
                  1. <tfoot id='rElz0'></tfoot>
                    <legend id='rElz0'><style id='rElz0'><dir id='rElz0'><q id='rElz0'></q></dir></style></legend>

                    1. <i id='rElz0'><tr id='rElz0'><dt id='rElz0'><q id='rElz0'><span id='rElz0'><b id='rElz0'><form id='rElz0'><ins id='rElz0'></ins><ul id='rElz0'></ul><sub id='rElz0'></sub></form><legend id='rElz0'></legend><bdo id='rElz0'><pre id='rElz0'><center id='rElz0'></center></pre></bdo></b><th id='rElz0'></th></span></q></dt></tr></i><div id='rElz0'><tfoot id='rElz0'></tfoot><dl id='rElz0'><fieldset id='rElz0'></fieldset></dl></div>
                    2. <small id='rElz0'></small><noframes id='rElz0'>