[译]Delphi for iOS开发指南(13):在iOS Device中使用通知中心

  • 主题发起人 DelphiTeacher的专栏
  • 开始时间
D

DelphiTeacher的专栏

Unregistered / Unconfirmed
GUEST, unregistred user!
Delphi for iOS开发指南(13):在iOS Device中使用通知中心




这篇教程讲述了在iOS Device上使用通知中心的基本步骤。




三种基本的通知或提醒方式




当用户在他们的iOS Device上为某些应用设置了通知,通知可以以三种基本的方式传递给用户。




在应用图标上的数字标记

o8FtlqpgVz4FC2ku-WJ0yNuGkvLenJ40.png


iPAD上的通知横幅

o8FtlqpgVz4FC2ku-WJ0yNuGkvLenJ40.png


通知提醒框

o8FtlqpgVz4FC2ku-WJ0yNuGkvLenJ40.png





在iPad上的通知中心




下图显示了iPad的通知中心,用户可以下拉弹出这个最近所有通知的列表。

o8FtlqpgVz4FC2ku-WJ0yNuGkvLenJ40.png














访问通知服务




通知服务接口(IFMXNotificationCenter)定义为一个FireMonkey Platform Service。




要访问通知服务,需要做两件事件:



•如果在Uses子句中不存在下面这两个单元,那么添加:






uses

FMX.Platform, FMX.Notification;










•使用下面的代码来运行一个FireMonkey Platform Services的查询:






var

NotificationService: IFMXNotificationCenter;

begin

if TPlatformServices.Current.SupportsPlatformService(IFMXNotificationCenter) then

NotificationService := TPlatformServices.Current.GetPlatformService(IFMXNotificationCenter) as IFMXNotificationCenter;



// Use the Notification Service

end;











IFMXNotificationCenter接口提供了使用图标数字标记来作为通知的基本服务。


































在代码中设置图标数字标记




IFMXNotificationCenter有SetIconBadgeNumber方法来定义图标标记的数字:






procedure TForm1.SerIconBadgeNumber;

var

NotificationService: IFMXNotificationCenter;

begin

if TPlatformServices.Current.SupportsPlatformService(IFMXNotificationCenter) then

NotificationService := TPlatformServices.Current.GetPlatformService(IFMXNotificationCenter) as IFMXNotificationCenter;



// Reset Icon Badge Number

if Assigned(NotificationService) then

NotificationService.SetIconBadgeNumber(18);

end;











在你设置图标标记的数字为18之后,你可以在你的iOS主屏上看得到:

o8FtlqpgVz4FC2ku-WJ0yNuGkvLenJ40.png


你也可以重新设置图标标记的数字,ResetIconBadgeNumber方法:






procedure TForm1.ResetIconBadgeNumber;

var

NotificationService: IFMXNotificationCenter;

begin

if TPlatformServices.Current.SupportsPlatformService(IFMXNotificationCenter) then

NotificationService := TPlatformServices.Current.GetPlatformService(IFMXNotificationCenter) as IFMXNotificationCenter;



// Set Icon Badge Number

if Assigned(NotificationService) then

NotificationService.ResetIconBadgeNumber;

end;























调度通知




你也可以使用ScheduleNotification方法来调度通知消息。




要显示一个通知消息,你需要创建一个TNofication类的实例,然后定义Name(标识符)和Message:






procedure TForm1.ScheduleNotification;

var

NotificationService: IFMXNotificationCenter;

Notification: TNotification;

begin

if TPlatformServices.Current.SupportsPlatformService(IFMXNotificationCenter) then

NotificationService := TPlatformServices.Current.GetPlatformService(IFMXNotificationCenter) as IFMXNotificationCenter;

if Assigned(NotificationService) then

begin

Notification := TNotification.Create;

try

Notification.Name := 'MyNotification';

Notification.AlertBody := 'Delphi for iOS is here!';



// Fired in 10 second

Notification.FireDate := Now + EncodeTime(0,0,10,0);



// Send notification in Notification Center

NotificationService.ScheduleNotification(Notification);

finally

Notification.Free;

end;

end

end;











在你设置通知消息之后,你可以在你的iOS主屏顶部看到:

o8FtlqpgVz4FC2ku-WJ0yNuGkvLenJ40.png























更新或取消一个已经调度过通知消息




每个调度的通知消息通过TNotification对象的Name属性来识别。




要更新一个已经调度过通知,简单地再调用一次ScheduleNotification,使用有相同名称(Name属性)的TNotification实例。




要取消一个已经调度过的通知,你可以简单的调用CancelNotification方法,使用你上次使用的标识符:






procedure TForm1.CancelNotification;

var

NotificationService: IFMXNotificationCenter;

begin

if TPlatformServices.Current.SupportsPlatformService(IFMXNotificationCenter) then

NotificationService := TPlatformServices.Current.GetPlatformService(IFMXNotificationCenter) as IFMXNotificationCenter;

if Assigned(NotificationService) then

NotificationService.CancelNotification('MyNotification');

end;














立即显示通知消息




你也可以使用PresentNotification来立即显示通知消息。




要显示一个通知消息,你需要创建一个TNotification类的一个实例,然后定义它的Name(标识符)和Message:






procedure TForm1.PresentNotification;

var

NotificationService: IFMXNotificationCenter;

Notification: TNotification;

begin

if TPlatformServices.Current.SupportsPlatformService(IFMXNotificationCenter) then

NotificationService := TPlatformServices.Current.GetPlatformService(IFMXNotificationCenter) as IFMXNotificationCenter;

if Assigned(NotificationService) then

begin

Notification := TNotification.Create;

try

Notification.Name := 'MyNotification';

Notification.AlertBody := 'Delphi for iOS is here!';



// Set Icon Badge Number as well

Notification.ApplicationIconBadgeNumber := 18;



// Show Notification Message

NotificationService.PresentNotification(Notification);

finally

Notification.Free;

end;

end;

end;














通知横幅或通知提醒框




默认的,你的应用程序显示通知横幅:



•iPad上的通知横幅

o8FtlqpgVz4FC2ku-WJ0yNuGkvLenJ40.png
•通知提醒框


o8FtlqpgVz4FC2ku-WJ0yNuGkvLenJ40.png





要使用通知提醒框来代替通知横幅,用户需要通过消息中心配置页来更改通知方式:

o8FtlqpgVz4FC2ku-WJ0yNuGkvLenJ40.png














添加操作到通知提醒框




你也可以添加一个操作按钮来自定义一个提醒框。




要自定义一个提醒操作,你需要设置AlterAction属性的操作,然后设置HasAction属性为True,如下:




Notification := TNotification.Create;

try

Notification.Name := 'MyNotification';

Notification.AlertBody := 'Delphi for iOS is here!';



Notification.AlertAction := 'Code Now!';

Notification.HasAction := True;



// Fired in 10 seconds

Notification.FireDate := Now + EncodeTime(0,0,10,0);



// Show Notification Message

NotificationService.ScheduleNotification(Notification);

finally

Notification.Free;

end;





o8FtlqpgVz4FC2ku-WJ0yNuGkvLenJ40.png











翻译的不好,请大家贱谅!

作者:DelphiTeacher 发表于2013/5/23 0:23:13 原文链接
阅读:2130 评论:1 查看评论

查看更多...
 
最后编辑:

Similar threads

D
回复
0
查看
729
DelphiTeacher的专栏
D
D
回复
0
查看
865
DelphiTeacher的专栏
D
D
回复
0
查看
1K
DelphiTeacher的专栏
D
顶部