Spaces:
Running
Running
| 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}") | |