FMX.Birthday Reminder Sample

From RAD Studio Code Examples
Jump to: navigation, search

This demo is an FMX application that shows how to use the TAddressBook component to access the Address Book on an Android or iOS device.

Location

You can find the Birthday Reminder sample project at:

  • Start | Programs | Embarcadero RAD Studio Rio | Samples, and then navigate to:
    • Object Pascal\Multi-Device Samples\Device Sensors and Services\Address Book\BirthdayReminder
    • CPP\Multi-Device Samples\Device Sensors and Services\Address Book\BirthdayReminder
  • Subversion Repository:
    • You can find Delphi and C++ code samples in GitHub Repositories. Search by name into the samples repositories according to your RAD Studio version.

Description

This sample demonstrates how to:

  • Request permission to access the Address Book.
  • Retrieve contacts from the Address Book.
  • Show notifications to remember birthdays.

The sample uses a TNotificationCenter component to send notifications.

How to Use the Sample

  1. Navigate to the location given above, and open:
    • Delphi: BirthdayReminderDemo.dproj
    • C++: BirthdayReminderDemo.cbproj
  2. Select Android or iOS Device as the Target Platform.
  3. Select the device.
  4. Press F9 or choose Run > Run.

Implementation

Permission Request

The OnShow event triggers when the TForm is shown.

OnShow calls RequestPermission.

The RequestPermission method of the TAddressBook requests for permission to read and change the Address Book of the mobile device if the Address Book is currently available on the current platform.

Contacts Upload to the Address Book

The OnPermissionRequest event of the TAddressBook triggers when requesting for permission to the mobile device.

  • If permission is granted, the TListView is filled with contacts of the Address Book that have birth date information, and the TProgressBar is animated during the upload process.
  • If permission in not granted, a message appears informing that the user is not allowed to read contacts.

Address Book Changes

The OnExternalChange event of the TAddressBook triggers when there is a change in the Address Book.

OnExternalChange invokes the RevertCurrentChangesAndUpdate procedure.

If the Address Book is currently available on the current platform, RevertCurrentChangesAndUpdate reverts all current changes and updates the instance of the TAddressBook.

Birthday Notifications

When a contact is loaded, the ContactLoaded procedure defines a label next to the contact with the remaining days to the birthday. If the contact has a photo in the Address Book, the photo is shown at the left of the contact.

When the loading process starts, all the notifications are canceled.

  • In Delphi:
  NotificationCenter1.CancelAll;
  • In C++:
  NotificationCenter1->CancelAll();

PostNotification procedure is called in the ContactLoaded procedure.

PostNotification procedure creates and presents the notification when the birthday arrives.

  • In Delphi:
  Notification.RepeatInterval := TRepeatInterval.Year; //The interval for a repeating notification. 
  Notification.FireDate := EventDate; //Date and time when the notification is fired. 
  Notification.AlertBody := Format(NotificationMessage, [ADisplayName]); //The notification message text. 
  Notification.EnableSound := True; //An alert is played when the notification appears. 
  NotificationCenter1.ScheduleNotification(Notification); //Schedules a local notification for delivery at a specific date and time.
  • In C++:
  notification->RepeatInterval = TRepeatInterval::Year; //The interval for a repeating notification. 
  notification->FireDate = eventDate; //Date and time when the notification is fired. 
  notification->AlertBody = "Don't forget to congratulate " + displayName; //The notification message text. 
  notification->EnableSound = true; //An alert is played when the notification appears.
  NotificationCenter1->ScheduleNotification(notification); //Schedules a local notification for delivery at a specific date and time

Uses

See Also