Firebase Notifications集成onTokenRefresh未被调用

时间:2018-01-11 16:37:14

标签: android firebase firebase-cloud-messaging

我正在尝试启用firebase通知,而我似乎无法使其正常运行。

如果觉得我在这里错过了一步,但我不确定是什么。

我在同一个应用程序中集成了firebase auth和firebase数据库,这两个数据库都按预期工作。

我在我的Android清单中添加了2项服务:

<service
    android:name="tld.domain.app.lib.firebase.MyFirebaseMessagingService">
    <intent-filter>
        <action android:name="com.google.firebase.MESSAGING_EVENT"/>
    </intent-filter>
</service>

<service
    android:name="tld.domain.app.lib.firebase.MyFirebaseInstanceIDService">
    <intent-filter>
        <action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
    </intent-filter>
</service>

我可以按住Ctrl并单击这些类,这样我就可以将名称指向正确的位置。

我还在类的onTokenRefresh方法中添加了一个断点:

public class MyFirebaseInstanceIDService extends FirebaseInstanceIdService {

    private static final String TAG = "MyFirebaseIIDService";

    /**
     * Called if InstanceID token is updated. This may occur if the security of
     * the previous token had been compromised. Note that this is called when the InstanceID token
     * is initially generated so this is where you would retrieve the token.
     */
    // [START refresh_token]
    @Override
    public void onTokenRefresh() {
        // Get updated InstanceID token.
        String refreshedToken = FirebaseInstanceId.getInstance().getToken();
        Log.d(TAG, "Refreshed token: " + refreshedToken);

        // If you want to send messages to this application instance or
        // manage this apps subscriptions on the server side, send the
        // Instance ID token to your app server.
        sendRegistrationToServer(refreshedToken);
    }
    // [END refresh_token]

    /**
     * Persist token to third-party servers.
     *
     * Modify this method to associate the user's FCM InstanceID token with any server-side account
     * maintained by your application.
     *
     * @param token The new token.
     */
    private void sendRegistrationToServer(String token) {
        Log.d(TAG, "sendRegistrationToServer: " + token);

        // TODO: Implement this method to send token to your app server.
    }

}

为什么没有被召唤?我在这里缺少什么?

1 个答案:

答案 0 :(得分:1)

只有在刷新令牌时才会触发/调用此接收器。对于初始注册,您需要明确调用注册方法。

应用程序需要调用以下方法来获取推送令牌。令牌不会自行生成

FirebaseInstanceId.getInstance().getToken()

有关详细信息,请参阅official example。确切的代码snippet

相关问题