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

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

 


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

 


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

 


在应用图标上的数字标记

 


iPAD上的通知横幅

 


通知提醒框

 

 

 

在iPad上的通知中心

 


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

 

 

 

 

 

 

 


访问通知服务

 


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

 


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

 

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

 


 

  1. uses  
  2.   
  3.   FMX.Platform, FMX.Notification;  
uses

  FMX.Platform, FMX.Notification;


 

 

 


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

 


 

  1. var  
  2.   
  3.   NotificationService: IFMXNotificationCenter;  
  4.   
  5. begin  
  6.   
  7.   if TPlatformServices.Current.SupportsPlatformService(IFMXNotificationCenter) then  
  8.   
  9.     NotificationService := TPlatformServices.Current.GetPlatformService(IFMXNotificationCenter) as IFMXNotificationCenter;  
  10.   
  11.    
  12.   
  13.   // Use the Notification Service   
  14.   
  15. end;  
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方法来定义图标标记的数字:

 


 

  1. procedure TForm1.SerIconBadgeNumber;  
  2.   
  3. var  
  4.   
  5.   NotificationService: IFMXNotificationCenter;  
  6.   
  7. begin  
  8.   
  9.   if TPlatformServices.Current.SupportsPlatformService(IFMXNotificationCenter) then  
  10.   
  11.     NotificationService := TPlatformServices.Current.GetPlatformService(IFMXNotificationCenter) as IFMXNotificationCenter;  
  12.   
  13.    
  14.   
  15.   // Reset Icon Badge Number   
  16.   
  17.   if Assigned(NotificationService) then  
  18.   
  19.     NotificationService.SetIconBadgeNumber(18);  
  20.   
  21. end;  
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主屏上看得到:

 


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

 


 

  1. procedure TForm1.ResetIconBadgeNumber;  
  2.   
  3. var  
  4.   
  5.   NotificationService: IFMXNotificationCenter;  
  6.   
  7. begin  
  8.   
  9.   if TPlatformServices.Current.SupportsPlatformService(IFMXNotificationCenter) then  
  10.   
  11.     NotificationService := TPlatformServices.Current.GetPlatformService(IFMXNotificationCenter) as IFMXNotificationCenter;  
  12.   
  13.    
  14.   
  15.   // Set Icon Badge Number   
  16.   
  17.   if Assigned(NotificationService) then  
  18.   
  19.     NotificationService.ResetIconBadgeNumber;  
  20.   
  21. end;  
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:

 


 

  1. procedure TForm1.ScheduleNotification;  
  2.   
  3. var  
  4.   
  5.   NotificationService: IFMXNotificationCenter;  
  6.   
  7.   Notification: TNotification;  
  8.   
  9. begin  
  10.   
  11.   if TPlatformServices.Current.SupportsPlatformService(IFMXNotificationCenter) then  
  12.   
  13.     NotificationService := TPlatformServices.Current.GetPlatformService(IFMXNotificationCenter) as IFMXNotificationCenter;  
  14.   
  15.   if Assigned(NotificationService) then  
  16.   
  17.   begin  
  18.   
  19.     Notification := TNotification.Create;  
  20.   
  21.     try  
  22.   
  23.       Notification.Name := 'MyNotification';  
  24.   
  25.       Notification.AlertBody := 'Delphi for iOS is here!';  
  26.   
  27.    
  28.   
  29.       // Fired in 10 second   
  30.   
  31.       Notification.FireDate := Now + EncodeTime(0,0,10,0);  
  32.   
  33.    
  34.   
  35.       // Send notification in Notification Center   
  36.   
  37.       NotificationService.ScheduleNotification(Notification);  
  38.   
  39.     finally  
  40.   
  41.       Notification.Free;  
  42.   
  43.     end;  
  44.   
  45.   end  
  46.   
  47. end;  
  48.   
  49.    
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主屏顶部看到:

 

 

 

 

 

 

 

 

 

 

 

 

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

 


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

 


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

 


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

 


 

  1. procedure TForm1.CancelNotification;  
  2.   
  3. var  
  4.   
  5.   NotificationService: IFMXNotificationCenter;  
  6.   
  7. begin  
  8.   
  9.   if TPlatformServices.Current.SupportsPlatformService(IFMXNotificationCenter) then  
  10.   
  11.     NotificationService := TPlatformServices.Current.GetPlatformService(IFMXNotificationCenter) as IFMXNotificationCenter;  
  12.   
  13.   if Assigned(NotificationService) then  
  14.   
  15.     NotificationService.CancelNotification('MyNotification');  
  16.   
  17. end;  
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:

 


 

  1. procedure TForm1.PresentNotification;  
  2.   
  3. var  
  4.   
  5.   NotificationService: IFMXNotificationCenter;  
  6.   
  7.   Notification: TNotification;  
  8.   
  9. begin  
  10.   
  11.   if TPlatformServices.Current.SupportsPlatformService(IFMXNotificationCenter) then  
  12.   
  13.     NotificationService := TPlatformServices.Current.GetPlatformService(IFMXNotificationCenter) as IFMXNotificationCenter;  
  14.   
  15.   if Assigned(NotificationService) then  
  16.   
  17.   begin  
  18.   
  19.     Notification := TNotification.Create;  
  20.   
  21.     try  
  22.   
  23.       Notification.Name := 'MyNotification';  
  24.   
  25.       Notification.AlertBody := 'Delphi for iOS is here!';  
  26.   
  27.    
  28.   
  29.       // Set Icon Badge Number as well   
  30.   
  31.       Notification.ApplicationIconBadgeNumber := 18;  
  32.   
  33.    
  34.   
  35.       // Show Notification Message   
  36.   
  37.       NotificationService.PresentNotification(Notification);  
  38.   
  39.     finally  
  40.   
  41.       Notification.Free;  
  42.   
  43.     end;  
  44.   
  45.   end;  
  46.   
  47. end;  
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上的通知横幅


•通知提醒框

 

 

 

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

 

 

 

 

 

 

 


添加操作到通知提醒框

 


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

 


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

 

  1. Notification := TNotification.Create;  
  2.   
  3. try  
  4.   
  5.   Notification.Name := 'MyNotification';  
  6.   
  7.   Notification.AlertBody := 'Delphi for iOS is here!';  
  8.   
  9.   
  10.   
  11.   Notification.AlertAction := 'Code Now!';  
  12.   
  13.   Notification.HasAction := True;  
  14.   
  15.   
  16.   
  17.   // Fired in 10 seconds   
  18.   
  19.   Notification.FireDate := Now + EncodeTime(0,0,10,0);  
  20.   
  21.   
  22.   
  23.   // Show Notification Message   
  24.   
  25.   NotificationService.ScheduleNotification(Notification);  
  26.   
  27. finally  
  28.   
  29.   Notification.Free;  
  30.   
  31. end;  
    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;


 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值