我想在我的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
所以很明显问题是它试图从我的本地主机地址而不是其他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 } } } }
答案 0 :(得分:26)
只需在根文件夹firebase-messaging-sw.js
中创建一个空文件答案 1 :(得分:10)
Firebase SDK会尝试在特定位置注册服务工作者。
位置为+ / firebase-messaging-sw.js。有两种选择:
1。)让+ / firebase-messaging-sw.js在浏览器中工作(即确保它返回有效的响应),然后再次尝试使用SDK(如果浏览器可以访问该文件,它应该可以工作)。
2.。)使用useServiceworker(<service worker registration>)
方法传入自定义服务工作者。这仍然需要您拥有有效的服务工作者URL。
答案 2 :(得分:0)
检查你的角度cli文件...... 添加
<强> “火力的消息传递-sw.js”, “manifest.json”
“资产”:[ “资产”, “应用程序/主/内容/组件/角的材料”, “favicon.ico的”, “火力点的消息,sw.js” “的manifest.json” ],
答案 3 :(得分:0)
不仅是一个空文件,还是添加如下内容的好主意:
importScripts('https://www.gstatic.com/firebasejs/7.14.2/firebase-app.js');
importScripts('https://www.gstatic.com/firebasejs/7.14.2/firebase-messaging.js');
/*Update this config*/
var config = {
apiKey: "",
authDomain: "",
databaseURL: "",
projectId: "",
storageBucket: "",
messagingSenderId: ""
};
firebase.initializeApp(config);
const messaging = firebase.messaging();
messaging.setBackgroundMessageHandler(function(payload) {
console.log('[firebase-messaging-sw.js] Received background message ', payload);
// Customize notification here
const notificationTitle = payload.data.title;
const notificationOptions = {
body: payload.data.body,
icon: 'http://localhost/gcm-push/img/icon.png',
image: 'http://localhost/gcm-push/img/d.png'
};
return self.registration.showNotification(notificationTitle,
notificationOptions);
});
// [END background_handler]