相关资料:
1.官网实例:http://docwiki.embarcadero.com/RADStudio/XE5/en/Mobile_Tutorial:_Using_the_Notification_Center_(iOS_and_Android)
结果:
1.二个按钮可以新建消息提醒,最小化也是新建消息提醒。
2.程序必须最小化后才能点击消息提醒Label2才会有反映。
实例代码:
unit Unit1; interface uses
System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs,
FMX.Notification, FMX.Controls.Presentation, FMX.StdCtrls,
FMX.Platform;//需要引入 type
TForm1 = class(TForm)
NotificationCenter1: TNotificationCenter;
Button1: TButton;
Button2: TButton;
Label1: TLabel;
Label2: TLabel;
Button3: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure NotificationCenter1ReceiveLocalNotification(Sender: TObject;
ANotification: TNotification);
private
flag: Boolean;
function HandleAppEvent(AAppEvent: TApplicationEvent; AContext: TObject): Boolean;
{ Private declarations }
public
{ Public declarations }
end; var
Form1: TForm1; implementation
{$R *.fmx}
{$R *.NmXhdpiPh.fmx ANDROID} //延时新建一个消息提醒
procedure TForm1.Button1Click(Sender: TObject);
var
MyNotification: TNotification;
begin
//通过消息中心创建消息
MyNotification := NotificationCenter1.CreateNotification;
try
MyNotification.Name := '消息的名称'; //设置消息的名称
MyNotification.AlertBody := '消息的内容'; //设置消息的内容
MyNotification.Number := ;//设置图标标号
MyNotification.FireDate := Now + EncodeTime(,, , ); //设置 10 秒后触发消息
//将消息提交消息中心,并于指定时间触发
NotificationCenter1.ScheduleNotification(MyNotification);
Label2.Text := '';
finally
MyNotification.DisposeOf; //释放消息接口
end;
end; //即时新建消息提醒
procedure TForm1.Button2Click(Sender: TObject);
var
MyNotification: TNotification;
begin
MyNotification :=NotificationCenter1.CreateNotification; //通过消息中心创建消息
try
MyNotification.Name:= '消息的名称'; //设置消息的名称
MyNotification.AlertBody := '消息的内容'; //设置消息的内容
MyNotification.Number := ;//设置图标标号
MyNotification.EnableSound := True;//有提示音
NotificationCenter1.PresentNotification(MyNotification); //将消息提交消息中心
Label2.Text := '';
finally
MyNotification.DisposeOf; //释放消息接口
end;
end; //取消消息提醒
procedure TForm1.Button3Click(Sender: TObject);
begin
NotificationCenter1.CancelNotification('消息的名称');
end; //主要是为程序挂接事件
procedure TForm1.FormCreate(Sender: TObject);
var
aFMXApplicationEventService: IFMXApplicationEventService;
begin
flag := True;
if TPlatformServices.Current.SupportsPlatformService(IFMXApplicationEventService, IInterface(aFMXApplicationEventService)) then
aFMXApplicationEventService.SetApplicationEventHandler(HandleAppEvent)
else
flag := False;
end; //将要挂接在程序上的事件,此事件是最小化时新建一个消息提醒
function TForm1.HandleAppEvent(AAppEvent: TApplicationEvent;
AContext: TObject): Boolean;
var
MyNotification: TNotification;
begin
if flag = False then
Exit;
case AAppEvent of
TApplicationEvent.aeEnteredBackground://监测,当程序后台运行时执行以下事件
begin
//通过消息中心创建消息
MyNotification := NotificationCenter1.CreateNotification;
try
MyNotification.Name :='消息的名称'; //设置消息的名称
//设置消息的内容
MyNotification.AlertBody := '消息的内容';
MyNotification.Number := ; //设置图标标号
MyNotification.EnableSound := True;
NotificationCenter1.PresentNotification(MyNotification); //将消息提交消息中心
Label2.Text := '';
finally
MyNotification.DisposeOf; //释放消息接口
end;
end;
end;
Result := True;
end; //程序最小化后,点消提醒时,发生此事件
procedure TForm1.NotificationCenter1ReceiveLocalNotification(Sender: TObject;
ANotification: TNotification);
begin
//收到消息后程序的操作
Label2.Text := '收到' + ANotification.Name + '的消息!';
end; end.