delphi App给自己推送消息  
官方Delphi 学习QQ群: 682628230(三千人)
频道

delphi 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;


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


367202-20190401092311358-601681702.png


上图,定义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的文章。


来源:https://blog.51cto.com/kinglandsoft/3224645



推荐分享
图文皆来源于网络,内容仅做公益性分享,版权归原作者所有,如有侵权请告知删除!
 

Copyright © 2014 DelphiW.com 开发 源码 文档 技巧 All Rights Reserved
晋ICP备14006235号-8 晋公网安备 14108102000087号

执行时间: 0.071063995361328 seconds