使用 GWT 时找不到 Firebase Service Worker(404 错误)
我想在我的 GWT Web 应用程序中使用 firebase 云消息服务,但遇到了一些问题.应用程序应该能够注册 firebase 服务工作者并使用其特定令牌连接到服务.在我的 GWT Java 代码中应该可以访问此令牌、接收到的消息和令牌更改时的事件.
当我尝试使用 handle.getToken() 创建令牌时发生错误.我收到此错误消息:
<上一页>获取脚本时收到错误的 HTTP 响应代码 (404).加载资源失败:net::ERR_INVALID_RESPONSEbrowserErrorMessage:无法注册 ServiceWorker:获取脚本时收到错误的 HTTP 响应代码 (404)."代码:消息/失败的服务工作者注册"消息:消息:我们无法注册默认服务工作者.无法注册服务工作者:获取脚本时收到错误的 HTTP 响应代码 (404).(消息/失败的服务工作者注册)."堆栈:FirebaseError:消息:我们无法注册默认服务工作者.无法注册服务工作者:获取脚本时收到错误的 HTTP 响应代码(404).(消息/失败的服务工作者注册).↵ athttps://www.gstatic.com/firebasejs/3.5.2/firebase-messaging.js:41:225"__proto__: 错误它尝试访问以注册 Service Worker 的 URL 是: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><脚本>//初始化 Firebase变量配置 = {apiKey: "AIzaSyBxdZNXHiLR1IC8Wrw3Y6q_5DFoN8hn_UY",authDomain: "fir-test-848de.firebaseapp.com",databaseURL: "https://fir-test-848de.firebaseio.com",storageBucket: "fir-test-848de.appspot.com",消息发送者ID:974661154941"};firebase.initializeApp(config);</脚本>
这是我的 Java 代码:
<上一页>公共类 FireBase{私有 JavaScriptObject _messagingHandle;私有字符串_token;公共火力基地(){_messagingHandle = createMessagingHandle();requestPermission(_messagingHandle, this);}私有原生 JavaScriptObject createMessagingHandle()/*-{返回 $wnd.firebase.messaging();}-*/;private native void listenTokenRefresh(最终 JavaScriptObject 句柄)/*-{处理.onTokenRefresh(函数(){instance.@x.client.miscellaneous.FireBase::onTokenRefresh()();});}-*/;私人无效 onTokenRefresh(){getToken(_messagingHandle, 这个);}private native void requestPermission(最终的 JavaScriptObject 句柄,最终的 Object 实例)/*-{handle.requestPermission().then(function(){$wnd.console.log('通知权限已授予');instance.@x.client.miscellaneous.FireBase::onPermission(Z)(true);})}-*/;私有无效 onPermission(最终布尔值授予){如果(准许){getToken(_messagingHandle, 这个);}}private native void getToken(final JavaScriptObject handle, final Object instance)/*-{handle.getToken().then(函数(currentToken){如果(当前令牌){instance.@x.client.miscellaneous.FireBase::onTokenReceived(Ljava/lang/String;)(currentToken);}别的{//显示权限请求.$wnd.console.log('没有可用的实例 ID 令牌.请求生成一个.');instance.@x.client.miscellaneous.FireBase::onTokenReceived(Ljava/lang/String;)(null);}})}-*/;私人无效onTokenReceived(最终字符串令牌){如果(令牌!= null){GWT.log("收到的令牌:" + 令牌);如果(!token.equals(_token)){//发送/更新令牌到服务器}}}} 解决方案Firebase SDK 尝试在特定位置注册 Service Worker.
位置是 +/firebase-messaging-sw.js.有两种选择:
1.) 使 +/firebase-messaging-sw.js 在浏览器中工作(即确保它返回有效响应),然后再次尝试使用 SDK(如果浏览器可以访问该文件,它应该可以工作).
2.) 使用 useServiceworker(
方法传入自定义的 service worker.这仍然需要您拥有一个有效的 Service Worker URL.
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() { 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 } } } }
解决方案
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.
相关文章