Delphi 10.2 Tokyo で、iBeacon を使ったアプリの自動起動をやってみました (2017/04/14)
アプリの自動起動は、Android の場合、NFC タグが、簡単で速いのですが、iOS では使えないので、iBeacon を試してみました。
iBeacon のエリアに入ると、通知バナーが表示され、バナーをタップすることで、アプリがバックグラウンドの場合は、フォアグラウンドに。
起動していない場合は、アプリが自動で起動されます。
Beacon コンポーネントと、NotificationCenter コンポーネントを使っています。
※「プロジェクト」 - 「オプション」 - 「バージョン情報」で、FMLocalNotificationPermission キーを true
にして下さい。
iPhone 6s (iOS 10.31) で動作確認しました。
iPhone 5 (iOS 10.31) では、期待通りには、動きませんでした。
「通知」についての詳しい情報は、こちら(docwiki.embarcadero.com)
何かの参考になれば・・・。
// 「プロジェクト」 - 「オプション」 - 「バージョン情報」 で、
// FMLocalNotificationPermission キーを true にすること
unit Unit1;
interface
uses
System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, System.Beacon,
System.Bluetooth, FMX.Controls.Presentation, FMX.ScrollBox, FMX.Memo,
System.Beacon.Components, System.Notification {, FMX.Platform.iOS};
type
TForm1 = class(TForm)
Beacon1: TBeacon;
Memo1: TMemo;
NotificationCenter1: TNotificationCenter;
procedure FormCreate(Sender: TObject);
procedure Beacon1BeaconEnter(const Sender: TObject; const ABeacon: IBeacon;
const CurrentBeaconList: TBeaconList);
procedure Beacon1BeaconExit(const Sender: TObject; const ABeacon: IBeacon;
const CurrentBeaconList: TBeaconList);
procedure Beacon1EnterRegion(const Sender: TObject; const UUID: TGUID;
AMajor, AMinor: Integer);
procedure Beacon1ExitRegion(const Sender: TObject; const UUID: TGUID;
AMajor, AMinor: Integer);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
private
{ private 宣言 }
public
{ public 宣言 }
procedure SendNotification(const AlertMessage : string);
end;
var
Form1: TForm1;
implementation
{$R *.fmx}
procedure TForm1.SendNotification(const AlertMessage : string);
var
ANotification : TNotification;
begin
ANotification := NotificationCenter1.CreateNotification;
try
// 単なる名前
ANotification.Name := 'ANotification';
// iOS でサポートされていない
// アプリケーション名がタイトルとして使用される
// ANotification.Title := 'Title';
// 通知メッセージ
ANotification.AlertBody := AlertMessage;
// 10秒後に発生
// FireDate の値を変更しなければ通知がすぐに発生
ANotification.FireDate := Now + EncodeTime(0, 0, 10, 0);
// 繰り返しなし
ANotification.RepeatInterval := TRepeatInterval.None;
// アイコン バッジ番号
MyNotification.Number := 18;
// 通知を送信したアプリケーションがフォアグラウンドに移動
// バックグラウンドで動作していた場合でも完全に終了していた場合でも同じ
ANotification.HasAction := True;
ANotification.AlertAction := 'Open App';
// スケジュールで通知を送信
// NotificationCenter1.ScheduleNotification(ANotification);
// 即座に通知(若干速い)
NotificationCenter1.PresentNotification(ANotification);
finally
ANotification.DisposeOf;
end;
end;
procedure TForm1.Beacon1BeaconEnter(const Sender: TObject;
const ABeacon: IBeacon; const CurrentBeaconList: TBeaconList);
begin
Memo1.Lines.Insert(0, 'BeaconEnter');
end;
procedure TForm1.Beacon1BeaconExit(const Sender: TObject;
const ABeacon: IBeacon; const CurrentBeaconList: TBeaconList);
begin
Memo1.Lines.Insert(0, 'BeaconExit');
end;
procedure TForm1.Beacon1EnterRegion(const Sender: TObject; const UUID: TGUID;
AMajor, AMinor: Integer);
begin
SendNotification('Beaconのエリアに入りました.');
end;
procedure TForm1.Beacon1ExitRegion(const Sender: TObject; const UUID: TGUID;
AMajor, AMinor: Integer);
begin
SendNotification('Beaconのエリアから出ました.');
end;
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
Beacon1.Enabled := False;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
Beacon1.Enabled := True;
end;
end.