FirebaseInstanceIdService不会被调用

asm*_*mgx 4 android firebase firebase-cloud-messaging

我按照互联网上的例子,在这里和其他网站上回顾了很多问题

我构建了我的应用程序,因此它处理FCM通知,但我仍然无法找出为什么不调用FirebaseInstanceIdService.

我宣布它 AndroidManifest.xml

<service android:name=".InstanceService">
    <intent-filter>
        <action android:name="com.google.firebase.INSTANCE_ID_EVENT"></action>
    </intent-filter>
</service>

<service android:name=".MessageService">
    <intent-filter>
        <action android:name="com.google.firebase.MESSAGING_EVENT"></action>
    </intent-filter>
</service>
Run Code Online (Sandbox Code Playgroud)

我的服务是

public class InstanceService extends FirebaseInstanceIdService {
    private static final String REG_Token="REG_Token";

    @Override
    public void onTokenRefresh() {

        String recent_token= FirebaseInstanceId.getInstance().getToken(); // get token from the server

        Log.d(REG_Token,recent_token); // show device token in Logcat View

    }
Run Code Online (Sandbox Code Playgroud)

我看看Logcat,但那里没有REG_Token.

我试图调试代码并停止它Log.d(REG_Token,recent_token);,但它并没有就此止步.

我认为该服务根本没有被调用,我找不到原因.

Wil*_*lik 9

这份文件

注册令牌可能会在以下时间更改

  • 该应用程序删除实例ID
  • 该应用程序将在新设备上恢复
  • 用户卸载/重新安装应用程序
  • 用户清除应用数据.

这意味着onTokenRefresh()每次打开应用程序时都不会调用该方法,而只会在发生这4个事件中的一个时调用该方法.

最简单的方法是卸载应用程序,然后再次安装应用程序.安装完成后,打开应用程序,然后查看logcat(根据您的代码).

希望这可以帮助 :)