2

I'm using this plugin to send push notifications from my Django REST app.

https://github.com/xtrinch/fcm-django

It works fine for the android side but IOs are unable to receive any notifications. Can anybody please tell me what I'm I missing here.

Following are my fcm_django configurations:

FCM_DJANGO_SETTINGS = {
    "APP_VERBOSE_NAME": "app-name",
    "FCM_SERVER_KEY": "<firebase-server-key>",
    "ONE_DEVICE_PER_USER": True,
    "DELETE_INACTIVE_DEVICES": False,
}

Following is my code which I use to send a notification to a device:

data = {
        "title": 'New Notification fired',
        "body": 'I just fired a new notification'}
devices.send_message(data=data)

It results in the following success response:

{'multicast_ids': [1212322313231212], 'success': 1, 'failure': 0, 'canonical_ids': 0, 'results': [{'message_id': '0:1579690926842318%a93f219bf9fd7ecd'}], 'topic_message_id': None}

Any help in this regard is highly appreciated. Thank you for your time.

Haroon Khalid
  • 155
  • 2
  • 8

1 Answers1

3

I have faced the same problem , there was an issue in this repo I managed to try some solution from it

this solution works good for me

data = {
    "title": 'New Notification fired',
    "body": 'I just fired a new notification'
}
kwargs = {
        "content_available": True,
        'extra_kwargs': {"priority": "high", "mutable_content": True, 'notification': data },
}
for device in devices:
        if device.type == 'ios':
            device.send_message(sound='default', **kwargs)
        else:
            device.send_message(data=data)

try this I am sure it will work as I am using in all of my projects

then enhance it with this

devices.objects.filter(type='ios').send_message(sound='default', **kwargs)
devices.objects.exclude(type='ios').send_message(data=data)

edit "more clarification"

In iOS, to provide a background notification, the JSON sent to firebase MUST have a key "content_available" : true and other issue there is no sound on notification. this is my working json with sound and background notification for iOS.

{ 
   "data":{  
      "key":"...firebaseserverkey..." 
   },
   "content_available" : true,
   "notification":{ 
       "sound": "default",
       "title": "...",
       "body":"..."
   },
 "to":"...devicetoken..." 
}

just try to send a post request with that body using postman with this url https://fcm.googleapis.com/fcm/send this will do what fcm-django do

content_available - On iOS, use this field to represent content-available in the APNs payload. When a notification or message is sent and this is set to true, an inactive client app is awoken, and the message is sent through APNs as a silent notification and not through the FCM connection server. Note that silent notifications in APNs are not guaranteed to be delivered, and can depend on factors such as the user turning on Low Power Mode, force quitting the app, etc. On Android, data messages wake the app by default. On Chrome, currently not supported.

priority (also from the docs):

Sets the priority of the message. Valid values are "normal" and "high." On iOS, these correspond to APNs priorities 5 and 10.

By default, notification messages are sent with high priority, and data messages are sent with normal priority. Normal priority optimizes the client app's battery consumption and should be used unless immediate delivery is required. For messages with normal priority, the app may receive the message with unspecified delay.

When a message is sent with high priority, it is sent immediately, and the app can display a notification.

as mentioned here Firebase messaging - whats "content_available" : true also you can read the docs for more information

Community
  • 1
  • 1
  • Thank you for your time. Can you please explain why do we need to send those extra flags e.g "content_available" & "mutable_content"? I understand that Android can better receive data-object as notification while IOs can receive notification-object as a notification. – Haroon Khalid Jan 24 '20 at 10:20
  • I will edit my answer but I need to know does it works for you first of all , but as a brief what send function really do is doing a post request with server key and device token to fire base , In fire base docs its obvious when sending to ios device you should replace data key with notification key , the change I do removes the default behavior sending with the one that works , I will edit the answer to more clarification – Mohamed Abd El-hameed Jan 24 '20 at 22:58
  • I have been edited my answer , also as a good resource to make fcm notification you can use https://github.com/jazzband/django-push-notifications – Mohamed Abd El-hameed Jan 25 '20 at 02:41
  • That solution worked for me. Thank you for your time. Really appreciated. – Haroon Khalid Jan 27 '20 at 09:30
  • I'm still having trouble with fcm-django . Is there an updated solution for the no iOS sound? – Karan V Aug 24 '22 at 21:59
  • @KaranV did you find any solution? Still having the issue with no sound... – Khris Vandal Feb 01 '23 at 02:48
  • 1
    @KhrisVandal i didn't find any solution. i think it has to do with the package. so might be better to go a different route. – Karan V Feb 01 '23 at 18:33