xtrinch / fcm-django

FCM Django: Send push notifications via django to websites, iOS & android mobile devices through FCM (Firebase Cloud Messaging)
http://fcm-django.readthedocs.io/
MIT License
774 stars 195 forks source link

Send additional fields in kwargs #12

Closed monicaphalswal closed 7 years ago

monicaphalswal commented 7 years ago

extra_kwargs = { 'content_available': True } devices.send_message( title="Syncup", body=".", icon="appicon", data={ "notification type" : 20 }, kwargs=extra_kwargs )

I am trying to send additional fields in kwargs but I am getting notify_multiple_devices() got an unexpected keyword argument 'kwargs'. Please help. Also, I don't want to send the key "body" but when I skip it or make it None, the payload gets messed up. So, is it possible it skip this key entirely?

xtrinch commented 7 years ago

'kwargs' is not a keyword argument, so don't name it, just pass it as the last argument.

Also please be more specific as to how the payload gets 'messed up'.

monicaphalswal commented 7 years ago

I changed the code to:

kwargs = {
        'content_available': True
}
devices.send_message(
                title="Syncup", 
                body=".", 
                icon="appicon", 
                data={
                    "notification type" : 20
                },
                kwargs
           )

But now I am getting error: SyntaxError: non-keyword arg after keyword arg

And for the missing 'body' part, the payload changes from {"data":{"notification type":20},"notification":{"body":".","icon":"appicon","title":"Syncup"},"priority":"high","to":"cF3VXFqliZQ:APA91bG3xXHeuoIx2TMYXLcTXooqQyRPiKzfGL4l4h8Cfxpl3vFBVmYY2YuqV6odipSnET2LNv3q4th5qxstNmonf_w2MnUrP77TNWfLV4iDlmNc_gCQPoR_w6F4U-3hJkVAM3ujNKMY"} to(title goes missing) {"data":{"notification type":20},"notification":{"icon":"appicon"},"priority":"high","to":"cF3VXFqliZQ:APA91bG3xXHeuoIx2TMYXLcTXooqQyRPiKzfGL4l4h8Cfxpl3vFBVmYY2YuqV6odipSnET2LNv3q4th5qxstNmonf_w2MnUrP77TNWfLV4iDlmNc_gCQPoR_w6F4U-3hJkVAM3ujNKMY"} (when I remove key "body" from above code).

xtrinch commented 7 years ago

http://stackoverflow.com/questions/1496346/passing-a-list-of-kwargs for passing kwargs

xtrinch commented 7 years ago

For the second part - pyfcm's github page is where that question would be answered best, because that part is handled in this file.

monicaphalswal commented 7 years ago

Thank you for the help. But, I'm still not able to send "mutable_content": True even in kwargs. I am getting notify_single_device() got an unexpected keyword argument 'mutable_content' error.

ipoogleduck commented 9 months ago

How did you end up fixing this? @monicaphalswal

merwok commented 9 months ago

The initial problem was that the function call had kwargs=extra_kwargs instead of the correct **kwargs (dictionary unpacking, to add more param=value parameters to the call).

The second problem is different (passing an unknown parameter to a function).

xtrinch commented 9 months ago

The question is for an obsolete version which used the legacy API, so this question is most likely not relevant anymore

ipoogleduck commented 9 months ago

Ah okay. I want to add channel_id from https://firebase.google.com/docs/reference/fcm/rest/v1/projects.messages#androidnotification to my notification payload so I was looking at this to see how it would be possible. I was thinking this would work but maybe I am on the wrong track?

fcm_send_topic_message(message_title=messageTitle,
                                   message_body=messageBody,
                                   condition=favoritesConditions, 
                                   data_message=notif_data,
                                   sound="Default",
                                   extra_kwargs={"android": "{\"notification\": {\"channel_id\": \"" + time_topic + "\"}}"})
xtrinch commented 9 months ago

@ipoogleduck

from firebase_admin.messaging import Message, Notification
topic = "A topic"
FCMDevice.objects.handle_subscription(True, topic)
FCMDevice.send_topic_message(Message(data={...}), "TOPIC NAME")

And the channel ID you would pass into the Message constructor under android_config, see https://firebase.google.com/docs/reference/fcm/rest/v1/projects.messages#resource:-message