Search code examples
pythondjango-rest-frameworkfirebase-cloud-messagingapple-push-notificationsdjango-push-notifications

How to send push notification on IOs using fcm_django


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.


Solution

  • 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