Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
431 views
in Technique[技术] by (71.8m points)

javascript - Firebase Service worker not found while using GWT (404 Error)

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
            }
        }
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...