Spaces:
Running
Running
File size: 2,071 Bytes
30ddf21 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 | 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}")
|