From 5adfa694c13fe44337938c10c9a2db70d12d34c8 Mon Sep 17 00:00:00 2001 From: Claude Date: Fri, 6 Mar 2026 14:04:33 -0800 Subject: [PATCH] Migrate FCM from legacy v1 API to HTTP v2 with service account auth Replace deprecated FCM server key authentication with Google service account OAuth2 flow. The class now creates a signed JWT from the service account credentials, exchanges it for a short-lived access token (cached via WordPress transients), and sends messages to the FCM v2 endpoint (projects/{id}/messages:send). Settings page updated: FCM Server Key field replaced with Firebase Project ID + Service Account JSON textarea with validation. Co-Authored-By: Claude Opus 4.6 --- admin/mobile-app-settings.php | 63 ++++++++--- includes/class-twp-fcm.php | 196 +++++++++++++++++++++++++++------- 2 files changed, 208 insertions(+), 51 deletions(-) diff --git a/admin/mobile-app-settings.php b/admin/mobile-app-settings.php index 1c9891a..cc92f58 100644 --- a/admin/mobile-app-settings.php +++ b/admin/mobile-app-settings.php @@ -36,7 +36,19 @@ if (isset($_POST['twp_test_notification']) && check_admin_referer('twp_mobile_se // Save settings if (isset($_POST['twp_save_mobile_settings']) && check_admin_referer('twp_mobile_settings')) { - update_option('twp_fcm_server_key', sanitize_text_field($_POST['twp_fcm_server_key'])); + update_option('twp_fcm_project_id', sanitize_text_field($_POST['twp_fcm_project_id'])); + // Service account JSON — validate it parses as JSON before saving + $sa_json_raw = isset($_POST['twp_fcm_service_account_json']) ? wp_unslash($_POST['twp_fcm_service_account_json']) : ''; + if (!empty($sa_json_raw)) { + $sa_parsed = json_decode($sa_json_raw, true); + if ($sa_parsed && isset($sa_parsed['client_email'], $sa_parsed['private_key'])) { + update_option('twp_fcm_service_account_json', $sa_json_raw); + } else { + $sa_json_error = 'Invalid service account JSON — must contain client_email and private_key fields.'; + } + } else { + update_option('twp_fcm_service_account_json', ''); + } update_option('twp_auto_update_enabled', isset($_POST['twp_auto_update_enabled']) ? '1' : '0'); update_option('twp_gitea_repo', sanitize_text_field($_POST['twp_gitea_repo'])); update_option('twp_gitea_token', sanitize_text_field($_POST['twp_gitea_token'])); @@ -48,7 +60,9 @@ if (isset($_POST['twp_save_mobile_settings']) && check_admin_referer('twp_mobile } // Get current settings -$fcm_server_key = get_option('twp_fcm_server_key', ''); +$fcm_project_id = get_option('twp_fcm_project_id', ''); +$fcm_service_account_json = get_option('twp_fcm_service_account_json', ''); +$fcm_sa_configured = !empty($fcm_service_account_json) && !empty($fcm_project_id); $auto_update_enabled = get_option('twp_auto_update_enabled', '1') === '1'; $gitea_repo = get_option('twp_gitea_repo', 'wp-plugins/twilio-wp-plugin'); $gitea_token = get_option('twp_gitea_token', ''); @@ -90,6 +104,12 @@ $total_sessions = $wpdb->get_var("SELECT COUNT(*) FROM $sessions_table"); + +
+

+
+ +
@@ -118,26 +138,45 @@ $total_sessions = $wpdb->get_var("SELECT COUNT(*) FROM $sessions_table");
-

Firebase Cloud Messaging (FCM)

-

Configure FCM to enable push notifications for the mobile app.

+

Firebase Cloud Messaging (FCM) — HTTP v2 API

+

Configure FCM using a service account for push notifications. The legacy server key API has been retired by Google.

+ + + +
- + + placeholder="my-project-12345">

- Get your server key from Firebase Console > Project Settings > Cloud Messaging > Server Key + Found in Firebase Console > Project Settings > General > Project ID

+ + + +

+ Generate in Firebase Console > Project Settings > Service Accounts > Generate New Private Key. + Paste the entire JSON file contents here. Must contain client_email and private_key fields. +

+ +

✓ Service account configured

+ +
@@ -181,13 +220,13 @@ $total_sessions = $wpdb->get_var("SELECT COUNT(*) FROM $sessions_table"); class="regular-text" placeholder="CR...">

- Twilio Push Credential SID. Create in Twilio Console > Messaging > Push Credentials using your FCM server key. Required for incoming call push notifications. + Twilio Push Credential SID. Create in Twilio Console > Messaging > Push Credentials using your FCM service account JSON. Required for incoming call push notifications.

- +