使用 GWT 时未找到 Firebase Service Worker(404 错误)

2024-03-03

我想在我的 GWT Web 应用程序中使用 firebase 云消息服务,但遇到了一些问题。 应用程序应该能够注册 Firebase Service Worker 并使用其特定令牌连接到该服务。该令牌、收到的消息以及令牌更改时的事件应该可以在我的 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
  

它尝试访问以注册 Service Worker 的 URL 是:http://127.0.0.1:11111/firebase-messaging-sw.js http://127.0.0.1:11111/firebase-messaging-sw.js

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

这些脚本包含在我的 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()
        {
            [email protected] /cdn-cgi/l/email-protection::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.');
            [email protected] /cdn-cgi/l/email-protection::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)
            {
                [email protected] /cdn-cgi/l/email-protection::onTokenReceived(Ljava/lang/String;)(currentToken);
            }
            else
            {
                // Show permission request.
                $wnd.console.log('No Instance ID token available. Request permission to generate one.');
                [email protected] /cdn-cgi/l/email-protection::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
            }
        }
    }
}
  

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

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 GWT 时未找到 Firebase Service Worker(404 错误) 的相关文章

随机推荐