#!/usr/bin/env python3
"""
Sherlock's Push Notification Sender
===================================
A simple Python script to send push notifications to mobile devices using Firebase Cloud Messaging (FCM).
This works for both Android and iOS devices registered with FCM.
Prerequisites:
1. Create a Firebase project at https://console.firebase.google.com/
2. Enable Cloud Messaging (FCM).
3. Download the Service Account JSON key from Project Settings > Service Accounts > Generate new private key.
4. Register your device/app with FCM and obtain a device token (e.g., via Firebase SDK in your app).
5. Install dependencies: `pip install firebase-admin`
Usage:
python push_notifier.py --service-account /path/to/service-account.json \
--device-token YOUR_DEVICE_TOKEN \
--title "Hello" \
--body "This is a test notification!"
Ethical Note: Only use this for legitimate purposes, like your own apps or with explicit user consent.
Do not spam or harass users, as that violates Firebase ToS and laws.
"""
import argparse
import firebase_admin
from firebase_admin import credentials, messaging
def send_push_notification(cred_path, device_token, title, body):
"""
Send a push notification to a single device token.
Args:
cred_path (str): Path to Firebase service account JSON.
device_token (str): FCM registration token for the device.
title (str): Notification title.
body (str): Notification body.
Returns:
bool: True if sent successfully.
"""
# Initialize Firebase Admin if not already done
if not firebase_admin._apps:
cred = credentials.Certificate(cred_path)
firebase_admin.initialize_app(cred)
# Create message
message = messaging.Message(
notification=messaging.Notification(
title=title,
body=body,
),
token=device_token,
)
# Send message
response = messaging.send(message)
print(f"Successfully sent notification with response ID: {response}")
return True
def main():
parser = argparse.ArgumentParser(description="Send push notifications via FCM")
parser.add_argument("--service-account", required=True, help="Path to service account JSON")
parser.add_argument("--device-token", required=True, help="FCM device token")
parser.add_argument("--title", required=True, help="Notification title")
parser.add_argument("--body", required=True, help="Notification body")
args = parser.parse_args()
try:
success = send_push_notification(
args.service_account,
args.device_token,
args.title,
args.body
)
if success:
print("Notification sent! Check your device.")
else:
print("Failed to send notification.")
except Exception as e:
print(f"Error: {e}")
print("Troubleshooting:")
print("- Verify service account JSON path and permissions.")
print("- Ensure device token is valid and not expired.")
print("- Check Firebase project quotas and setup.")
if __name__ == "__main__":
main()
How to Use
Save as push_notifier.py.
Get your device token: In your mobile app, use Firebase SDK to print/log the token (e.g., FirebaseMessaging.instance.getToken() in Flutter).
Run the command as shown in the usage doc.
Test on your device!
Extensions
Multiple devices: Replace token=device_token with tokens=[token1, token2] in Message.
Topics: Use topic="your-topic" instead of token.
Data payload: Add data={"key": "value"} to Message.
iOS-specific: FCM handles APNs automatically if the token is from iOS.
If you need APNs-only (no Firebase) or other tweaks, provide more details! 🔍