ktu-papers-api / test_push.py
GitHub Action
Deploy clean API instance with LFS and README metadata
7f6b634
Raw
History Blame Contribute Delete
2.07 kB
import os
import json
import firebase_admin
from firebase_admin import credentials, messaging, db
print("Starting Push Notification Test from GitHub Actions...")
firebase_creds_json = os.environ.get('FIREBASE_CREDENTIALS')
if not firebase_creds_json:
print("❌ ERROR: FIREBASE_CREDENTIALS secret not found in environment variables!")
exit(1)
try:
print("1. Authenticating with Firebase...")
cred_dict = json.loads(firebase_creds_json)
cred = credentials.Certificate(cred_dict)
firebase_admin.initialize_app(cred, {
'databaseURL': 'https://kerala-timetable-db-default-rtdb.asia-southeast1.firebasedatabase.app'
})
print("2. Fetching subscribers from database...")
ref = db.reference('subscribers')
subscribers = ref.get()
if not subscribers:
print("❌ No subscribers found! Did you click 'Allow' on your website?")
else:
tokens = list(subscribers.keys())
print(f"βœ… Found {len(tokens)} subscriber(s). Sending test message...")
message = messaging.MulticastMessage(
notification=messaging.Notification(
title="🚨 KTU Dashboard Test (from GitHub)!",
body="Your GitHub Actions secret is perfectly wired up!"
),
tokens=tokens
)
try:
print("Sending message to Firebase...")
response = messaging.send_each_for_multicast(message)
print(f"🎯 Push Results -> Success: {response.success_count} | Failed: {response.failure_count}")
# ⚠️ NEW: Print the exact reason why Firebase rejected it!
if response.failure_count > 0:
for idx, resp in enumerate(response.responses):
if not resp.success:
print(f"πŸ›‘ REASON FOR FAILURE: {resp.exception}")
except Exception as inner_e:
print(f"❌ Failed specifically during sending: {inner_e}")
except Exception as e:
print(f"❌ General Error: {e}")