{"id":13396,"date":"2024-09-06T09:34:46","date_gmt":"2024-09-06T01:34:46","guid":{"rendered":"https:\/\/blog.hoyo.idv.tw\/?p=13396"},"modified":"2024-09-06T09:34:47","modified_gmt":"2024-09-06T01:34:47","slug":"python-firebase-cloud-messaging-api-v1-%e6%8e%a8%e6%92%ad","status":"publish","type":"post","link":"https:\/\/blog.hoyo.idv.tw\/?p=13396","title":{"rendered":"Python - Firebase Cloud Messaging API V1 \u63a8\u64ad"},"content":{"rendered":"<p>--<\/p>\n<h2>\u53c3\u8003\u8cc7\u6e90<\/h2>\n<ul>\n<li><a href=\"https:\/\/medium.com\/@prabha.ochetty\/mastering-firebase-push-notifications-with-python-elevate-your-apps-engagement-with-firebase-2b5affa6022c\" target=\"_blank\" rel=\"noopener\">Mastering Firebase Push Notifications with Python: Elevate Your App\u2019s Engagement with Firebase\u2026 | by Prabha Obulichetty | Medium<\/a><\/li>\n<li><a href=\"https:\/\/github.com\/firebase\/firebase-admin-python\/issues\/158\" target=\"_blank\" rel=\"noopener\">Not sound option for messaging.Notification \u00b7 Issue #158 \u00b7 firebase\/firebase-admin-python \u00b7 GitHub<\/a><\/li>\n<li><a href=\"https:\/\/stackoverflow.com\/questions\/53771076\/python-firebase-admin-sdk-appears-successful-but-i-never-receive-the-notificatio\" target=\"_blank\" rel=\"noopener\">ios - Python Firebase Admin SDK appears successful but I never receive the notification - Stack Overflow<\/a><\/li>\n<\/ul>\n<p><a href=\"https:\/\/blog.hoyo.idv.tw\/wp-content\/uploads\/2024\/09\/2024-09-06-09-20-40.png\" data-rel=\"lightbox-image-0\" data-rl_title=\"\" data-rl_caption=\"\" title=\"\"><img loading=\"lazy\" class=\"alignnone size-large wp-image-13402\" src=\"https:\/\/blog.hoyo.idv.tw\/wp-content\/uploads\/2024\/09\/2024-09-06-09-20-40-1024x779.png\" alt=\"\" width=\"640\" height=\"487\" srcset=\"https:\/\/blog.hoyo.idv.tw\/wp-content\/uploads\/2024\/09\/2024-09-06-09-20-40-1024x779.png 1024w, https:\/\/blog.hoyo.idv.tw\/wp-content\/uploads\/2024\/09\/2024-09-06-09-20-40-300x228.png 300w, https:\/\/blog.hoyo.idv.tw\/wp-content\/uploads\/2024\/09\/2024-09-06-09-20-40-768x584.png 768w, https:\/\/blog.hoyo.idv.tw\/wp-content\/uploads\/2024\/09\/2024-09-06-09-20-40.png 1111w\" sizes=\"(max-width: 640px) 100vw, 640px\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p>--<\/p>\n<h2>Python 3 \u7bc4\u4f8b\u7a0b\u5f0f\u78bc<\/h2>\n<p>\u76f8\u8f03\u65bc PHP Google API \u7cbe\u7c21\u8a31\u591a\uff0c\u5982\u679c\u4e0d\u9700\u8981\u63a8\u64ad\u63d0\u793a\u97f3\u5c31\u628a android, apns \u79fb\u9664\u5373\u53ef<\/p>\n<pre class=\"lang:python decode:true\">import firebase_admin\r\nfrom firebase_admin import credentials\r\nfrom firebase_admin import messaging\r\n\r\n\r\n# Replace with the path to your service account key JSON file\r\nserver_key = 'upad12-xxxxx-xxxxx.json'\r\ncred = credentials.Certificate(server_key)\r\nfirebase_admin.initialize_app(cred)\r\n\r\ndef send_push_notification(device_tokens, title, body):\r\n    message = messaging.Message(\r\n        notification=messaging.Notification(\r\n            title=title,\r\n            body=body\r\n        ),\r\n        token=device_tokens,\r\n        android=messaging.AndroidConfig(priority='high', notification=messaging.AndroidNotification(sound='default'), ),\r\n        apns=messaging.APNSConfig(payload=messaging.APNSPayload(aps=messaging.Aps(sound='default'), ), )\r\n    )\r\n    response = messaging.send(message)\r\n    print(\"Successfully sent notification:\", response)\r\n\r\ndevice_tokens = 'token'\r\n\r\n# Notification details\r\ntitle = 'aaaa1'\r\nbody = 'bbbb2'\r\n\r\n# Send push notification\r\nsend_push_notification(device_tokens, title, body)\r\n<\/pre>\n<p>--<\/p>\n<div class=\"pvc_clear\"><\/div>\n<p class=\"pvc_stats all \" data-element-id=\"13396\" style=\"\"><i class=\"pvc-stats-icon medium\" aria-hidden=\"true\"><svg aria-hidden=\"true\" focusable=\"false\" data-prefix=\"far\" data-icon=\"chart-bar\" role=\"img\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" viewBox=\"0 0 512 512\" class=\"svg-inline--fa fa-chart-bar fa-w-16 fa-2x\"><path fill=\"currentColor\" d=\"M396.8 352h22.4c6.4 0 12.8-6.4 12.8-12.8V108.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v230.4c0 6.4 6.4 12.8 12.8 12.8zm-192 0h22.4c6.4 0 12.8-6.4 12.8-12.8V140.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v198.4c0 6.4 6.4 12.8 12.8 12.8zm96 0h22.4c6.4 0 12.8-6.4 12.8-12.8V204.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v134.4c0 6.4 6.4 12.8 12.8 12.8zM496 400H48V80c0-8.84-7.16-16-16-16H16C7.16 64 0 71.16 0 80v336c0 17.67 14.33 32 32 32h464c8.84 0 16-7.16 16-16v-16c0-8.84-7.16-16-16-16zm-387.2-48h22.4c6.4 0 12.8-6.4 12.8-12.8v-70.4c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v70.4c0 6.4 6.4 12.8 12.8 12.8z\" class=\"\"><\/path><\/svg><\/i> &nbsp;1,080&nbsp;total views, &nbsp;4&nbsp;views today<\/p>\n<div class=\"pvc_clear\"><\/div>\n","protected":false},"excerpt":{"rendered":"<p>-- \u53c3\u8003\u8cc7\u6e90 Masteri...<\/p>\n<div class=\"pvc_clear\"><\/div>\n<p class=\"pvc_stats all \" data-element-id=\"13396\" style=\"\"><i class=\"pvc-stats-icon medium\" aria-hidden=\"true\"><svg aria-hidden=\"true\" focusable=\"false\" data-prefix=\"far\" data-icon=\"chart-bar\" role=\"img\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" viewBox=\"0 0 512 512\" class=\"svg-inline--fa fa-chart-bar fa-w-16 fa-2x\"><path fill=\"currentColor\" d=\"M396.8 352h22.4c6.4 0 12.8-6.4 12.8-12.8V108.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v230.4c0 6.4 6.4 12.8 12.8 12.8zm-192 0h22.4c6.4 0 12.8-6.4 12.8-12.8V140.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v198.4c0 6.4 6.4 12.8 12.8 12.8zm96 0h22.4c6.4 0 12.8-6.4 12.8-12.8V204.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v134.4c0 6.4 6.4 12.8 12.8 12.8zM496 400H48V80c0-8.84-7.16-16-16-16H16C7.16 64 0 71.16 0 80v336c0 17.67 14.33 32 32 32h464c8.84 0 16-7.16 16-16v-16c0-8.84-7.16-16-16-16zm-387.2-48h22.4c6.4 0 12.8-6.4 12.8-12.8v-70.4c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v70.4c0 6.4 6.4 12.8 12.8 12.8z\" class=\"\"><\/path><\/svg><\/i> &nbsp;1,080&nbsp;total views, &nbsp;4&nbsp;views today<\/p>\n<div class=\"pvc_clear\"><\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[334],"tags":[],"_links":{"self":[{"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=\/wp\/v2\/posts\/13396"}],"collection":[{"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=13396"}],"version-history":[{"count":5,"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=\/wp\/v2\/posts\/13396\/revisions"}],"predecessor-version":[{"id":13403,"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=\/wp\/v2\/posts\/13396\/revisions\/13403"}],"wp:attachment":[{"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=13396"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=13396"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=13396"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}