红鱼儿

App给自己推送消息
procedure TMainDataModule.SendNotification(const ATitle, AAlertBody, AData: string);
var
   mNotice: TNotification;
begin
  if NotificationCenter1.Supported then
  begin
    mNotice := NotificationCenter1.CreateNotification;
    try
      mNotice.Name := kbmMWGlobal.kbmMWGenerateGUID;//这里我用了唯一的名子,让每条消息都是新的,如果用固定的名子,那么新消息来时会覆盖旧的消息,即在消息栏中不会产生新的一条消息。
      mNotice.Title := ATitle;//消息栏中的标题
      mNotice.AlertBody := AAlertBody;//在消息栏中显示的具体内容
      mNotice.HasAction:=True;
      mNotice.AlertAction:=AData;//透传的Json,业务逻辑可以通过Json来定义
      // 通知
      NotificationCenter1.PresentNotification(mNotice);
    finally
      mNotice.DisposeOf;
    end;
  end;
end;

 上面是一个通用的方法,客户端调用,向app自己发出一个消息,为了接收这个消息,我们还要处理NotificationCenter1的OnReceiveLocalNotification事件:

procedure TForm10.NotificationCenter1ReceiveLocalNotification(Sender: TObject;
    ANotification: TNotification);
begin
  //这里处理接收的消息
end;

为了更美观,我们还要定义消息的图标:

上图,定义Notification的图标。

如果你已经成为DPush的用户,那上面的方法就不用我们去实现了,作者已经封装了上面实现到DPush中,与DPush的处理逻辑合二为一,事情将变的简单!

首先,通过TLeoDPushService.SendLocalNotification方法来为自己发送消息,下是是我进一步封装到MainDataModule中的方法:

procedure TMainDataModule.SendLocalNotification(const ATitle, ASubject, AData: string);
begin
  FPushService.SendLocalNotification(ATitle,ASubject,SO(AData),kbmMWGenerateGUID);
end;

其次,接收消息时,自动触发TLeoDPushService.OnGetPushDataEvent事件。与离线消息是一样的,这非常爽了!关于收到离线消息的处理,可以参考前面写的关于DPush的文章。

 

posted on 2021-06-08 11:48  红鱼儿  阅读(388)  评论(0编辑  收藏  举报