本文介绍了在使用GWT时未找到Firebase服务工作人员(404错误)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



当我尝试使用句柄创建令牌.getToken()。
我得到这个错误信息:

 
提取脚本时收到错误的HTTP响应代码(404)。
无法加载资源:net :: ERR_INVALID_RESPONSE
$ b browserErrorMessage:无法注册ServiceWorker:提取脚本时收到错误的HTTP响应代码(404)。
代码:messaging / failed-serviceworker-registration
message:消息:我们无法注册默认的服务工作者无法注册ServiceWorker:收到错误的HTTP响应代码(404) (消息/失败的服务人员注册)。
stack:FirebaseError:Messaging:我们无法注册默认服务工作者。注册ServiceWorker失败:提取脚本时收到错误的HTTP响应代码(404)(messaging / failed-serviceworker-registration )。在https://www.gstatic.com/firebasejs/3.5.2/firebase-messaging.js:41:225\"__proto__:错误

它尝试访问以注册服务工作者的URL是:



所以很明显,问题是它试图访问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>
//初始化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;
私人字符串_token;
$ b $ public FireBase()
{
_messagingHandle = createMessagingHandle();
requestPermission(_messagingHandle,this);

$ b私有本地JavaScriptObject createMessagingHandle()
/ * - {
return $ wnd.firebase.messaging();
} - * /;

private void void listenTokenRefresh(final JavaScriptObject handle)
/ * - {
handle.onTokenRefresh(function()
{
instance。@ x。 client.miscellaneous.FireBase :: onTokenRefresh()();
});
} - * /;
$ b $ private void onTokenRefresh()
{
getToken(_messagingHandle,this);

$ b $ private private 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);


$ b $ private private void getToken(final JavaScriptObject handle,final Object instance)
/ * - {
handle.getToken() (function(currentToken)
{
if(currentToken)
{
instance。@ x.client.miscellaneous.FireBase :: onTokenReceived(Ljava / lang / String;)(currentToken );
}
else
{
//显示权限请求
$ wnd.console.log('没有实例ID令牌可用请求许可生成一个。');
instance。@ x.client.miscellaneous.FireBase :: onTokenReceived(Ljava / lang / String;)(null);
}
})

} - * /;
$ b private void onTokenReceived(final String token)
{
if(token!= null)
{
GWT.log(Received Token: +令牌);
if(!token.equals(_token))
{
//发送/更新令牌到服务器
}
}
}
}


解决方案

Firebase SDK尝试在特定位置注册服务工作人员。 >

位置是+ /firebase-messaging-sw.js。有两种选择:
$ b $ 1)Make + /firebase-messaging-sw.js在浏览器中工作(即确保它返回一个有效的响应),然后尝试再次使用SDK(如果浏览器可以访问该文件,它应该可以工作)。
$ b 使用 useServiceworker(< service worker registration> ;)方法在自定义服务工作者中传递。这仍然需要你有一个有效的服务工作者的网址。


I'd like to use the firebase cloud messaging service in my GWT webapplication, but I'm stuck with some problems.The application should be able to register the firebase service worker and connect to the service with it's specific token. This token, the received messages and the event when a token changes should be accessable in my GWT Java code.

The error occurs when I try to create the token by using handle.getToken().I get this error message:

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

The URL it tries to access to register the service worker is:http://127.0.0.1:11111/firebase-messaging-sw.js

So it's obvious the problem is it tries to access the javascript from my local host address instead of the location where the other firebase JS files are. So my question is how can I change that so it loads the file from the correct source?

These scripts are included in my HTML file:

<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>

This is my Java code:

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]::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]::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]::onTokenReceived(Ljava/lang/String;)(currentToken);
            }
            else
            {
                // Show permission request.
                $wnd.console.log('No Instance ID token available. Request permission to generate one.');
                [email protected]::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
            }
        }
    }
}
解决方案

The Firebase SDK attempts to register the service worker at a specific location.

The location is + /firebase-messaging-sw.js. There are two options:

1.) Make + /firebase-messaging-sw.js work in the browser (i.e. make sure it returns a valid response) and then try using the SDK again (it should work if the browser can access the file).

2.) Use the useServiceworker(<service worker registration>) method to pass in a custom service worker. This will still require you to have a valid service worker URL.

这篇关于在使用GWT时未找到Firebase服务工作人员(404错误)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-24 12:13