Search code examples
iosfiremonkeydelphi-xe8delphi-10-seattle

How can I get firemonkey to display a local notification on iOS?


I've pulled the following code from Embarcadero site (here) to generate a local notification.

procedure TForm1.SetNumberClick(Sender: TObject);
var
  MyNotification: TNotification;
begin
  // Create an instance of TNotification
  MyNotification := NotificationCenter1.CreateNotification;
  try
      MyNotification.Number :=18;
      MyNotification.AlertBody := 'Delphi for your mobile device is here!';
      NotificationCenter1.PresentNotification(MyNotification);
  finally
    MyNotification.DisposeOf;
  end;
end;

It compiles and runs. As expected, I did have to create NotificationCenter1 as a TNotificationCenter on my form. It works find under Android, but under iOS I get butkus. No local notification, no count on the icon, not even an error.

Did it ever work under XE8?

Has something changed with respect to local notification between XE8 and 10/Seattle?

My phone is running iOS 9.2. Did something change in iOS between 8.x and 9.x that breaks local notifications for Firemonkey?


Solution

  • The ultimate solution was two fold:

    1. You must have FMLocalNotificationPermission set to true (Project -> Options -> Version Info)
    2. The notifications will only show up if the app is NOT running in the foreground (e.g., you've gone back to you home screen, shut down the application, etc.). This is with both the ScheduleNotification and PresentNotification methods.