您的位置:首页 > 编程语言 > Delphi

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

2013-09-03 09:24 597 查看
这篇教程讲述了在iOS Device上使用通知中心的基本步骤。
 

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

 

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

 

在应用图标上的数字标记

 


iPAD上的通知横幅

 


通知提醒框

 


 

 

在iPad上的通知中心

 

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

 


 

 

 

 

 

 

访问通知服务

 

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

 

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

 

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

 

 

[delphi]
view plaincopyprint?

uses  
  
  FMX.Platform, FMX.Notification;  

uses

FMX.Platform, FMX.Notification;


 

 

 

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

 

 

[delphi]
view plaincopyprint?

var  
  
  NotificationService: IFMXNotificationCenter;  
  
begin  
  
  if TPlatformServices.Current.SupportsPlatformService(IFMXNotificationCenter) then  
  
    NotificationService := TPlatformServices.Current.GetPlatformService(IFMXNotificationCenter) as IFMXNotificationCenter;  
  
   
  
  // Use the Notification Service
  
  
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方法来定义图标标记的数字:

 

 

[delphi]
view plaincopyprint?

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;  

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方法:

 

 

[delphi]
view plaincopyprint?

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;  

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:

 

 

[delphi]
view plaincopyprint?

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;  
  
   

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方法,使用你上次使用的标识符:

 

 

[delphi]
view plaincopyprint?

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;  

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:

 

 

[delphi]
view plaincopyprint?

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;  

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,如下:

 

[delphi]
view plaincopyprint?

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;  

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;


 

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  delphi ios开发