Delphi专题 ·

delphi手机通知

功能

在手机上推送通知,显示通知内容,取消通知

主要代码

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.StdCtrls,
  FMX.Controls.Presentation, FMX.Layouts, FMX.Objects, System.Notification,
  DateUtils;

type
  TForm1 = class(TForm)
    Layout1: TLayout;
    Label1: TLabel;
    SpeedButton1: TSpeedButton;
    SpeedButton2: TSpeedButton;
    Label2: TLabel;
    Layout2: TLayout;
    Image1: TImage;
    Label3: TLabel;
    Image2: TImage;
    Layout3: TLayout;
    Label4: TLabel;
    Label5: TLabel;
    Image3: TImage;
    Label6: TLabel;
    NotificationCenter: TNotificationCenter;
    procedure Image3Click(Sender: TObject);
    procedure NotificationCenterReceiveLocalNotification(Sender: TObject;
      ANotification: TNotification);
    procedure FormShow(Sender: TObject);
    procedure Image2Click(Sender: TObject);
    procedure SpeedButton2Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.fmx}

procedure Send_Notification(NotiName, Notititle, Notitext: string);  //发送通知
var
  MyNotification: TNotification;
begin
  with Form1 do
  begin
    MyNotification := NotificationCenter.CreateNotification;

    try
      MyNotification.Name := NotiName;
      MyNotification.title := Notititle;
      MyNotification.AlertBody := Notitext;

      NotificationCenter.PresentNotification(MyNotification);
    finally
      MyNotification.DisposeOf;
    end;
  end;
end;

procedure Send_Scheduled_Notification(NotiName, Notititle, Notitext: string; Seconds: integer); //预定通知、延迟显示通知
var
  MyNotification: TNotification;
begin
  with Form1 do
  begin
    MyNotification := NotificationCenter.CreateNotification;

    try
      MyNotification.Name := NotiName;
      MyNotification.title := Notititle;
      MyNotification.AlertBody := Notitext;
      MyNotification.FireDate := IncSecond(now, Seconds);

      // NotificationCenter.PresentNotification(MyNotification);
      NotificationCenter.ScheduleNotification(MyNotification);
    finally
      MyNotification.DisposeOf;
    end;
  end;
end;

procedure Cancel_Notification(NotiName: string); // 取消通知
begin
  with Form1 do
  begin
    if Name = '' then
      NotificationCenter.CancelAll
    else
      NotificationCenter.CancelNotification(NotiName);
  end;
end;

procedure TForm1.FormShow(Sender: TObject);
begin
  Send_Scheduled_Notification('tongzhi1', '优惠马上到期','一项特大优惠只剩最后一天!', 10); //打开软件10秒后显示通知
end;

procedure TForm1.Image2Click(Sender: TObject);
begin
  Cancel_Notification('tongzhi2'); //取消通知2
end;

procedure TForm1.Image3Click(Sender: TObject);
begin
  Send_Notification('tongzhi2', '第二次通知','玩游戏,获取10000元折扣卷.');
  Send_Notification('tongzhi3', '第三次通知','玩游戏,获取10000元折扣卷.');
  Send_Notification('tongzhi4', '第四次通知','玩游戏,获取10000元折扣卷.');
end;

procedure TForm1.NotificationCenterReceiveLocalNotification(Sender: TObject;
  ANotification: TNotification);
begin
  if ANotification.Name = 'tongzhi1' then //点击通知1 弹出消息
    ShowMessage('10000元优惠券领取时间只剩最后一天了!');

end;

procedure TForm1.SpeedButton2Click(Sender: TObject);
begin
  Cancel_Notification('');
end;

end.

相关下载

点击下载

参与评论