使用 GWT 时未找到 Firebase 服务工作人员(404 错误)

我想在我的GWT Web应用程序中使用firebase云消息传递服务,但我遇到了一些问题。应用程序应该能够注册 firebase 服务工作线程,并使用其特定令牌连接到服务。在我的 GWT Java 代码中,应该可以访问此令牌、接收到的消息以及令牌更改时的事件。

当我尝试使用 handle.getToken() 创建令牌时,会发生错误。我收到此错误消息:

A bad HTTP response code (404) was received when fetching the script.
Failed to load resource: net::ERR_INVALID_RESPONSE

browserErrorMessage: "Failed to register a ServiceWorker: A bad HTTP response code (404) was received when fetching the script." 
code: "messaging/failed-serviceworker-registration"
message: "Messaging: We are unable to register the default service worker. Failed to register a ServiceWorker: A bad HTTP response code (404) was received when fetching the script. (messaging/failed-serviceworker-registration)."
stack: "FirebaseError: Messaging: We are unable to register the default service worker. Failed to register a ServiceWorker: A bad HTTP response code (404) was received when fetching the script. (messaging/failed-serviceworker-registration).↵    at https://www.gstatic.com/firebasejs/3.5.2/firebase-messaging.js:41:225"__proto__: Error

它尝试访问以注册服务工作线程的 URL 是:http://127.0.0.1:11111/firebase-messaging-sw.js

所以很明显,问题在于它试图从我的本地主机地址访问javascript,而不是从其他firebase JS文件所在的位置访问javascript。所以我的问题是,我该如何更改它,以便从正确的来源加载文件?

这些脚本包含在我的 HTML 文件中:

<script src="https://www.gstatic.com/firebasejs/3.6.1/firebase.js"></script>
<script src="https://www.gstatic.com/firebasejs/3.5.2/firebase-app.js"></script>
<script src="https://www.gstatic.com/firebasejs/3.5.2/firebase-auth.js"></script>
<script src="https://www.gstatic.com/firebasejs/3.5.2/firebase-database.js"></script>
<script src="https://www.gstatic.com/firebasejs/3.5.2/firebase-messaging.js"></script>
<script>
  // Initialize Firebase
  var config = {
    apiKey: "AIzaSyBxdZNXHiLR1IC8Wrw3Y6q_5DFoN8hn_UY",
    authDomain: "fir-test-848de.firebaseapp.com",
    databaseURL: "https://fir-test-848de.firebaseio.com",
    storageBucket: "fir-test-848de.appspot.com",
    messagingSenderId: "974661154941"
  };
  firebase.initializeApp(config);
</script>

这是我的Java代码:

public class FireBase
{

    private JavaScriptObject _messagingHandle;
    private String _token;

    public FireBase()
    {
        _messagingHandle = createMessagingHandle();
        requestPermission(_messagingHandle, this);
    }

    private native JavaScriptObject createMessagingHandle()
    /*-{
        return $wnd.firebase.messaging();
    }-*/;

    private native void listenTokenRefresh(final JavaScriptObject handle)
    /*-{
        handle.onTokenRefresh(function()
        {
            instance.@x.client.miscellaneous.FireBase::onTokenRefresh()();
        });
    }-*/;

    private void onTokenRefresh()
    {
        getToken(_messagingHandle, this);
    }

    private native void requestPermission(final JavaScriptObject handle, final Object instance)
    /*-{
        handle.requestPermission().then(function()
        {
            $wnd.console.log('Notification permission granted.');
            instance.@x.client.miscellaneous.FireBase::onPermission(Z)(true);
        })
    }-*/;

    private void onPermission(final boolean granted)
    {
        if (granted)
        {
            getToken(_messagingHandle, this);
        }
    }

    private native void getToken(final JavaScriptObject handle, final Object instance)
    /*-{
        handle.getToken().then(function(currentToken)
        {
            if (currentToken)
            {
                instance.@x.client.miscellaneous.FireBase::onTokenReceived(Ljava/lang/String;)(currentToken);
            }
            else
            {
                // Show permission request.
                $wnd.console.log('No Instance ID token available. Request permission to generate one.');
                instance.@x.client.miscellaneous.FireBase::onTokenReceived(Ljava/lang/String;)(null);
            }
        })

    }-*/;

    private void onTokenReceived(final String token)
    {
        if (token != null)
        {
            GWT.log("Received Token: " + token);
            if (!token.equals(_token))
            {
                // Send/Update token to server
            }
        }
    }
}

答案 1

只需在根文件夹 firebase-messaging-sw 中创建一个空文件.js


答案 2

Firebase SDK 会尝试在特定位置注册服务工作线程。

位置是 + /firebase-messaging-sw.js。有两个选项:

1.) 使 + /firebase-messaging-sw.js在浏览器中工作(即确保它返回有效的响应),然后再次尝试使用 SDK(如果浏览器可以访问该文件,它应该可以工作)。

2.) 使用该方法传入自定义服务工作线程。这仍将要求你具有有效的服务工作线程 URL。useServiceworker(<service worker registration>)