Package: async-firebase
async-firebase is a lightweight asynchronous client to interact with Firebase Cloud Messaging for sending push notification to Android and iOS devicesFree software: MIT licenseRequires: Python 3.8+FeaturesExtremely lightweight and does not rely onfirebase-adminwhich is heftySend push notifications to Android and iOS devicesSend Multicast push notification to Android and iOS devicesSend Web push notificationsSet TTL (time to live) for notificationsSet priority for notificationsSet collapse-key for notificationsDry-run mode for testing purposeInstallationTo installasync-firebase, simply execute the following command in a terminal:$pipinstallasync-firebaseGetting startedasync-firebase < 3.0.0To send push notification to Android:importasynciofromasync_firebaseimportAsyncFirebaseClientasyncdefmain():client=AsyncFirebaseClient()client.creds_from_service_account_file("secret-store/mobile-app-79225efac4bb.json")# or using dictionary object# client.creds_from_service_account_info({...}})device_token="..."android_config=client.build_android_config(priority="high",ttl=2419200,collapse_key="push",data={"discount":"15%","key_1":"value_1","timestamp":"2021-02-24T12:00:15"},title="Store Changes",body="Recent store changes",)response=awaitclient.push(device_token=device_token,android=android_config)print(response.success,response.message_id)if__name__=="__main__":asyncio.run(main())To send push notification to iOS:importasynciofromasync_firebaseimportAsyncFirebaseClientasyncdefmain():client=AsyncFirebaseClient()client.creds_from_service_account_file("secret-store/mobile-app-79225efac4bb.json")# or using dictionary object# client.creds_from_service_account_info({...}})device_token="..."apns_config=client.build_apns_config(priority="normal",ttl=2419200,apns_topic="store-updated",collapse_key="push",title="Store Changes",alert="Recent store changes",badge=1,category="test-category",custom_data={"discount":"15%","key_1":"value_1","timestamp":"2021-02-24T12:00:15"})response=awaitclient.push(device_token=device_token,apns=apns_config)print(response.success)if__name__=="__main__":asyncio.run(main())This prints:"projects/mobile-app/messages/0:2367799010922733%7606eb557606ebff"To manual construct message:importasynciofromdatetimeimportdatetimefromasync_firebase.messagesimportAPNSConfig,APNSPayload,ApsAlert,Apsfromasync_firebaseimportAsyncFirebaseClientasyncdefmain():apns_config=APNSConfig(**{"headers":{"apns-expiration":str(int(datetime.utcnow().timestamp())+7200),"apns-priority":"10","apns-topic":"test-topic","apns-collapse-id":"something",},"payload":APNSPayload(**{"aps":Aps(**{"alert":ApsAlert(title="some-title",body="alert-message"),"badge":0,"sound":"default","content_available":True,"category":"some-category","mutable_content":False,"custom_data":{"link":"https://link-to-somewhere.com","ticket_id":"YXZ-655512",},})})})device_token="..."client=AsyncFirebaseClient()client.creds_from_service_account_info({...})response=awaitclient.push(device_token=device_token,apns=apns_config)print(response.success)if__name__=="__main__":asyncio.run(main())async-firebase >= 3.0.0To send push notification to Android:importasynciofromasync_firebaseimportAsyncFirebaseClientfromasync_firebase.messagesimportMessageasyncdefmain():client=AsyncFirebaseClient()client.creds_from_service_account_file("secret-store/mobile-app-79225efac4bb.json")# or using dictionary object# client.creds_from_service_account_info({...}})device_token:str="..."android_config=client.build_android_config(priority="high",ttl=2419200,collapse_key="push",data={"discount":"15%","key_1":"value_1","timestamp":"2021-02-24T12:00:15"},title="Store Changes",body="Recent store changes",)message=Message(android=android_config,token=device_token)response=awaitclient.send(message)print(response.success,response.message_id)if__name__=="__main__":asyncio.run(main())To send push notification to iOS:importasynciofromasync_firebaseimportAsyncFirebaseClientfromasync_firebase.messagesimportMessageasyncdefmain():client=AsyncFirebaseClient()client.creds_from_service_account_file("secret-store/mobile-app-79225efac4bb.json")# or using dictionary object# client.creds_from_service_account_info({...}})device_token:str="..."apns_config=client.build_apns_config(priority="normal",ttl=2419200,apns_topic="store-updated",collapse_key="push",title="Store Changes",alert="Recent store changes",badge=1,category="test-category",custom_data={"discount":"15%","key_1":"value_1","timestamp":"2021-02-24T12:00:15"})message=Message(apns=apns_config,token=device_token)response=awaitclient.send(message)print(response.success)if__name__=="__main__":asyncio.run(main())This prints:"projects/mobile-app/messages/0:2367799010922733%7606eb557606ebff"To manual construct message:importasynciofromdatetimeimportdatetimefromasync_firebase.messagesimportAPNSConfig,APNSPayload,ApsAlert,Aps,Messagefromasync_firebaseimportAsyncFirebaseClientasyncdefmain():apns_config=APNSConfig(**{"headers":{"apns-expiration":str(int(datetime.utcnow().timestamp())+7200),"apns-priority":"10","apns-topic":"test-topic","apns-collapse-id":"something",},"payload":APNSPayload(**{"aps":Aps(**{"alert":ApsAlert(title="some-title",body="alert-message"),"badge":0,"sound":"default","content_available":True,"category":"some-category","mutable_content":False,"custom_data":{"link":"https://link-to-somewhere.com","ticket_id":"YXZ-655512",},})})})device_token:str="..."client=AsyncFirebaseClient()client.creds_from_service_account_info({...})message=Message(apns=apns_config,token=device_token)response=awaitclient.send(message)print(response.success)if__name__=="__main__":asyncio.run(main())Licenseasync-firebaseis offered under the MIT license.Source codeThe latest developer version is available in a GitHub repository:https://github.com/healthjoy/async-firebase