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

          <bdo id='1TBcs'></bdo><ul id='1TBcs'></ul>
        <legend id='1TBcs'><style id='1TBcs'><dir id='1TBcs'><q id='1TBcs'></q></dir></style></legend>

      2. <small id='1TBcs'></small><noframes id='1TBcs'>

      3. Firebase FCM 强制 onTokenRefresh() 被调用

        时间:2023-07-30

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

        <tfoot id='NQw99'></tfoot>

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

              2. <legend id='NQw99'><style id='NQw99'><dir id='NQw99'><q id='NQw99'></q></dir></style></legend>

                  本文介绍了Firebase FCM 强制 onTokenRefresh() 被调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在将我的应用从 GCM 迁移到 FCM.

                  I am migrating my app from GCM to FCM.

                  当新用户安装我的应用程序时,会自动调用 onTokenRefresh().问题是用户还没有登录(没有用户ID).

                  When a new user installs my app, the onTokenRefresh() is automatically being called. The problem is that the user is not logged in yet (No user id).

                  用户登录后如何触发onTokenRefresh()?

                  推荐答案

                  onTokenRefresh() 方法将在生成新令牌时被调用.应用程序安装后,它将立即生成(正如您所发现的那样).当令牌发生变化时也会调用它.

                  The onTokenRefresh() method is going to be called whenever a new token is generated. Upon app install, it will be generated immediately (as you have found to be the case). It will also be called when the token has changed.

                  根据 FirebaseCloudMessaging 指南:

                  您可以将通知定位到单个特定设备.在初始启动您的应用程序时,FCM SDK 会生成一个注册令牌客户端应用实例.

                  You can target notifications to a single, specific device. On initial startup of your app, the FCM SDK generates a registration token for the client app instance.

                  来源链接:https://firebase.google.com/docs/notifications/android/console-device#access_the_registration_token

                  这意味着令牌注册是针对每个应用程序的.听起来您想在用户登录后使用令牌.我建议您将 onTokenRefresh() 方法中的令牌保存到内部存储或共享首选项.然后,在用户登录后从存储中检索令牌,并根据需要向您的服务器注册令牌.

                  This means that the token registration is per app. It sounds like you would like to utilize the token after a user is logged in. What I would suggest is that you save the token in the onTokenRefresh() method to internal storage or shared preferences. Then, retrieve the token from storage after a user logs in and register the token with your server as needed.

                  如果您想手动强制 onTokenRefresh(),您可以创建一个 IntentService 并删除令牌实例.然后,当你调用getToken时,onTokenRefresh()方法会再次被调用.

                  If you would like to manually force the onTokenRefresh(), you can create an IntentService and delete the token instance. Then, when you call getToken, the onTokenRefresh() method will be called again.

                  示例代码:

                  public class DeleteTokenService extends IntentService
                  {
                      public static final String TAG = DeleteTokenService.class.getSimpleName();
                  
                      public DeleteTokenService()
                      {
                          super(TAG);
                      }
                  
                      @Override
                      protected void onHandleIntent(Intent intent)
                      {
                          try
                          {
                              // Check for current token
                              String originalToken = getTokenFromPrefs();
                              Log.d(TAG, "Token before deletion: " + originalToken);
                  
                              // Resets Instance ID and revokes all tokens.
                              FirebaseInstanceId.getInstance().deleteInstanceId();
                  
                              // Clear current saved token
                              saveTokenToPrefs("");
                  
                              // Check for success of empty token
                              String tokenCheck = getTokenFromPrefs();
                              Log.d(TAG, "Token deleted. Proof: " + tokenCheck);
                  
                              // Now manually call onTokenRefresh()
                              Log.d(TAG, "Getting new token");
                              FirebaseInstanceId.getInstance().getToken();
                          }
                          catch (IOException e)
                          {
                              e.printStackTrace();
                          }
                      }
                  
                      private void saveTokenToPrefs(String _token)
                      {
                          // Access Shared Preferences
                          SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
                          SharedPreferences.Editor editor = preferences.edit();
                  
                          // Save to SharedPreferences
                          editor.putString("registration_id", _token);
                          editor.apply();
                      }
                  
                      private String getTokenFromPrefs()
                      {
                          SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
                          return preferences.getString("registration_id", null);
                      }
                  }
                  

                  编辑

                  FirebaseInstanceIdService

                  公共类 FirebaseInstanceIdService 扩展服务

                  public class FirebaseInstanceIdService extends Service

                  此类已弃用.赞成压倒一切FirebaseMessagingService 中的 onNewToken.一旦那已经实施后,可以安全地删除此服务.

                  This class is deprecated. In favour of overriding onNewToken in FirebaseMessagingService. Once that has been implemented, this service can be safely removed.

                  onTokenRefresh() 已弃用.在 MyFirebaseMessagingService

                  onTokenRefresh() is deprecated. Use onNewToken() in MyFirebaseMessagingService

                  public class MyFirebaseMessagingService extends FirebaseMessagingService {
                  
                  @Override
                  public void onNewToken(String s) {
                      super.onNewToken(s);
                      Log.e("NEW_TOKEN",s);
                      }
                  
                  @Override
                  public void onMessageReceived(RemoteMessage remoteMessage) {
                      super.onMessageReceived(remoteMessage);
                      }
                  } 
                  

                  这篇关于Firebase FCM 强制 onTokenRefresh() 被调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:在收到 Firebase 通知时打开应用 (FCM) 下一篇:Firebase (FCM):打开活动并在通知点击时传递数据.安卓

                  相关文章

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

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

                      <tfoot id='nhfKZ'></tfoot>
                      <legend id='nhfKZ'><style id='nhfKZ'><dir id='nhfKZ'><q id='nhfKZ'></q></dir></style></legend>
                    1. <small id='nhfKZ'></small><noframes id='nhfKZ'>