Firemonkey TNotificationCenter在C++Builder 10.4 Android平台失效求助
Hey there! Let’s work through this scheduled notification problem you’re hitting. I’ve dealt with similar FireMonkey Android quirks before, so let’s break down the fixes step by step:
1. Fix Notification Permissions & Channel Setup (Critical for Android 8.0+)
Android 8.0 (API 26+) requires all notifications to be tied to a notification channel, and Android 13+ adds the POST_NOTIFICATIONS permission. Here’s how to set this up:
- Add the permission to your
AndroidManifest.xml:<uses-permission android:name="android.permission.POST_NOTIFICATIONS" /> - Manually create a notification channel in your Form’s
OnCreateevent (FireMonkey’sNotificationCenterdoesn’t always auto-create channels reliably in 10.4):if (AndroidApi::JBuild_VERSION::SDK_INT >= 26) { TJNotificationChannel* Channel = TJNotificationChannel::JavaClass->init( StringToJString("default_alert_channel"), StringToJString("Scheduled Alerts"), TJNotificationManager::JavaClass->IMPORTANCE_DEFAULT ); Channel->setDescription(StringToJString("Timed notifications from my app")); // Register the channel with Android TJNotificationManager::JavaClass->cast( TAndroidHelper::Context->getSystemService(TJContext::JavaClass->NOTIFICATION_SERVICE) )->createNotificationChannel(Channel); // Link your NotificationCenter to this channel NotificationCenter1->ChannelID = "default_alert_channel"; } - Don’t forget to assign the channel to your notification object:
myNotification->ChannelID = "default_alert_channel";
2. Correct Android Service Configuration
C++Builder 10.4’s Android Service template has a few gotchas—let’s fix those:
- When creating an Android Service via Add -> New -> Other -> Android -> Android Service, make sure the auto-generated
AndroidManifest.xmlentry matches your service’s full class name (it should look like this):<service android:name="com.yourcompany.YourAppName.TMyService" /> - When starting the service, use the full qualified class name (not just your project name):
TLocalServiceConnection::StartService("com.yourcompany.YourAppName.TMyService"); - Verify the service is running by adding debug logs in its
OnCreateevent:
Check Android Studio’s Logcat to confirm the service starts without crashes.#include <Androidapi.Log.hpp> void __fastcall TMyService::ServiceCreate(TObject *Sender) { __android_log_print(ANDROID_LOG_INFO, "MyService", "Service initialized successfully!"); }
3. Debug Your Scheduled Notification Code
Let’s rule out code-level issues:
- Add logs to confirm your date/time parsing works correctly:
If__android_log_print(ANDROID_LOG_INFO, "NotificationDebug", "Parsed Date: %d-%d-%d | Time: %d:%d:%d", y, m, d, h, n, s);FireDateis set to a time in the past, the notification won’t trigger at your desired time. - Ensure
NotificationCenter1is initialized before scheduling:// Add this in your Form's OnCreate event NotificationCenter1->Connect(); - For testing, simplify your code first: remove the repeat interval, set a
FireDate1 minute in the future, and see if it triggers. Once that works, add back the repeat and custom sound.
4. Bypass Android Background Restrictions
Android’s Doze mode and App Standby can block scheduled tasks for background apps. For testing:
- Add your app to the battery optimization whitelist (Settings > Battery > Battery Optimization > Select your app > Set to "Don’t optimize").
- In Developer Options, disable "Doze mode" and "App Standby" (only for testing—you’ll need to adapt to these restrictions for production builds).
If you work through these steps and still hit issues, try stripping down your code to the absolute minimum scheduled notification example (no custom sound, no repeat) to isolate the problem.
内容的提问来源于stack exchange,提问作者DarkSpy

