4

I am trying to implement Firebase cloud messaging push notifications in my Django project. Firebase would look for a js file named firebase-messaging-sw.js in a project' root directory (no matter, what sort of project it is).

So, the problem is that I can't figure out what is my project's root directory (sorry, for being stupid), and how to make Firebase see this file. Just as an experiment, I copy-pasted the js file to each and every folder of my project, and still no success (Firebase service can't see the file).

Here's my settings.py file (with relevant content):

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

ROOT_URLCONF = 'android_blend.urls'

WSGI_APPLICATION = 'android_blend.wsgi.application'
PROJECT_ROOT = os.path.abspath(os.path.dirname(__file__))

STATIC_URL = '/static/'

if DEBUG:
    MEDIA_URL = '/media/'
    STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR),"static","static-only")
#STATIC_ROOT = [os.path.join(BASE_DIR,"static-only")]
    MEDIA_ROOT = os.path.join(os.path.dirname(BASE_DIR),"static","media")
#MEDIA_ROOT = [os.path.join(BASE_DIR,"media")]
    STATICFILES_DIRS = (
        #os.path.join(os.path.dirname(BASE_DIR),"static","static"),
        os.path.join(BASE_DIR,"static"),
    )

My Django project layout is like this:

android_blend
   android_blend
      settings.py
      url.py
      wsgi.py
   app1
   app2
   ...
   appN
   manage.py

So the question is, what should I do in order for an outside service app (Google Firebase) be able to see the javascript file ?

In my browser, I get the following error:

A bad HTTP response code (404) was received when fetching the script.

1 Answer 1

9

I was also facing the same problem.

I tried it in the following way:

Add the following line in urls.py

url(r'^firebase-messaging-sw.js', views.firebase_messaging_sw_js),

Now add the function in view.py file

@csrf_exempt
def firebase_messaging_sw_js(request):
    filename = '/static/firebase-messaging-sw.js'
    jsfile = open(absAppPath + filename, 'rb')
    response = HttpResponse(content=jsfile)
    response['Content-Type'] = 'text/javascript'
    response['Content-Disposition'] = 'attachment; filename="%s"' % (absAppPath + filename)
    return response

So localhost:8000/firebase-messaging-sw.js will work properly...

Hope this will help you...

Sign up to request clarification or add additional context in comments.

3 Comments

Excellent ! Made my day.
@Edgar Navasardyan I need some help...I got the browser token ID..But what next...?
Shreejay, I got stuck on this point either. I did everything according to youtube.com/watch?v=BsCBCudx58g, but no success to see the message appear. If you have any success, please let me know ))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.