0
votes

Following the Embarcadero docs at this link i'm testing notifications on iOS (in FMX app built with C++). I've done the follownig:

  • Added #include <System.Notification.hpp> to the header file
  • Set FMLocalNotificationPermission to true
  • Dropped TNotificationCenter component on the form

Then, i put the following code in a button click:

void __fastcall TForm1::ScheduleNotificationClick(TObject *Sender)
{
    if (NotificationCenter1->Supported()) {
            TNotification *myNotification = NotificationCenter1->CreateNotification();
            __try {
                    myNotification->Name = "MyNotification";
                    myNotification->AlertBody = "C++ for your mobile device is here!";
                    // Fire in 10 seconds
                    myNotification->FireDate = Now() + EncodeTime(0, 0, 10, 0);
                    // Send notification to the notification center
                    NotificationCenter1->ScheduleNotification(myNotification);
            }
            __finally {
                    myNotification->DisposeOf();
            }
    }
}

Once in a while it works...but rarely and never more than once. Most of the time it doesn't at all (repeated deleting and reinstall of app).

Next, i tried the "Present the Notification Message Immediately" code they provide:

void __fastcall TForm1::PresentNotificationClick(TObject *Sender)
{
if (NotificationCenter1->Supported()) {
       TNotification *myNotification = NotificationCenter1->CreateNotification();
       __try { 
               myNotification->Name = "MyNotification";
               myNotification->AlertBody = "C++ for your mobile device is here!";
               // Set Icon Badge Number (for iOS) or message number (for Android) as well
               myNotification->Number = 18;
               myNotification->EnableSound = False;
               // Send notification to the notification center
               NotificationCenter1->PresentNotification(myNotification);
      }
      __finally {  
               myNotification->DisposeOf();
      }
 }
}

Nothing happens at all with this code. I've tried this from scratch several times and i'm as sure as i can be that i'm coding it per their examples. I'm using 10.3 (Embarcadero® C++Builder 10.3 Version 26.0.32429.4364). I would think my code has a problem except once in blue moon it works.

My target is iPhone running 12.1.4 and i've tried building with SDK11.4 and SDK12.0, no difference. When i first run app i get the "allow or don't allow" popup and my app subsequently shows up in the Notification settings - just doesn't work.

russ

UPDATE 3-25-2019: If I run that top block of code (from a button click on iPhone) it will now run everytime - but ONLY IF i immediately kill the app after clicking. 10 seconds later it fires the notification. Why won't the notification appear if i leave my app running??

1
I use notifications with similar code and have not experienced problems. However, the difference is that I use notifications to remind user to use the app, i.e. only when app is not running. I remove all notifications in TApplicationEvent.BecameActive and add all notifications in TApplicationEvent.EnteredBackground - Hans
You should NOT be disposing of the object that you schedule. Embarcadero's example is wrong in that regard. Get rid of the try..finally altogether and let ARC manage the object lifetime for you. - Remy Lebeau

1 Answers

0
votes

Are you sure you are calling "PresentNotificationClick" from the TButton when you click it?