You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Firemonkey TNotificationCenter在C++Builder 10.4 Android平台失效求助

Fixing Scheduled Notification Issues in FireMonkey (C++Builder 10.4 Sydney for 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 OnCreate event (FireMonkey’s NotificationCenter doesn’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.xml entry 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 OnCreate event:
    #include <Androidapi.Log.hpp>
    
    void __fastcall TMyService::ServiceCreate(TObject *Sender)
    {
      __android_log_print(ANDROID_LOG_INFO, "MyService", "Service initialized successfully!");
    }
    
    Check Android Studio’s Logcat to confirm the service starts without crashes.

3. Debug Your Scheduled Notification Code

Let’s rule out code-level issues:

  • Add logs to confirm your date/time parsing works correctly:
    __android_log_print(ANDROID_LOG_INFO, "NotificationDebug", "Parsed Date: %d-%d-%d | Time: %d:%d:%d", y, m, d, h, n, s);
    
    If FireDate is set to a time in the past, the notification won’t trigger at your desired time.
  • Ensure NotificationCenter1 is 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 FireDate 1 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.08 11:53:11