我想在我的 GWT 网络应用程序中使用 firebase 云消息服务,但我遇到了一些问题。 应用程序应该能够注册 firebase service worker 并使用它的特定 token 连接到服务。在我的 GWT Java 代码中应该可以访问此 token 、接收到的消息和 token 更改时的事件。
当我尝试使用 handle.getToken() 创建 token 时发生错误。 我收到此错误消息:
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>
这是我的 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 } } } }
最佳答案
只需在根文件夹 firebase-messaging-sw.js 中创建一个空文件
关于javascript - 使用 GWT 时找不到 Firebase Service worker(404 错误),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40673265/